# compiler by defauft xlc, defaut dotests 1, default_bits 64  

# Perl is compiled with xlc and its options are not compatible with gcc
# %define gcc_compiler 0
%{!?gcc_compiler: %define gcc_compiler 0}
%{!?dotests: %define dotests 1}
%{!?default_bits: %define default_bits 64}

%define postgres_uid 64200
%define postgres_gid 64200

%global TESTUSER pgstbf
%global TESTGROUP staff
%global plpython 1
%global pltcl 1
%global plperl 1
%global ssl 1
%global kerberos 1
%global ldap 1
%global nls 1
%global xml 1
%global pgfts 1

%global test 1
%global tap_tests %{dotests}
%global runselftest  %{dotests}

%define _libdir64 %{_prefix}/lib64

Summary: PostgreSQL client programs
Name: postgresql
%global majorversion 9.6
Version: 9.6.6
Release: 2
# The PostgreSQL license is very similar to other MIT licenses, but the OSI
# recognizes it as an independent license, so we do as well.
License: PostgreSQL
Group: Applications/Databases
URL: http://www.postgresql.org/

Source0: ftp://ftp.postgresql.org/pub/source/v%{version}/%{name}-%{version}.tar.bz2
Source1: ftp://ftp.postgresql.org/pub/source/v%{version}/%{name}-%{version}.tar.bz2.md5

# The PDF file is generated by generate-pdf.sh, which see for comments
Source2: %{name}-%{version}-US.pdf
# Get the doc file from:
#   https://www.postgresql.org/files/documentation/pdf/9.6/postgresql-9.6-US.pdf
# and change the name with exact X.Y.Z version as title says
# generate-pdf.sh is not used during RPM build, but included for documentation

# Sources : http://www.oss4aix.org/download/SRPMS/postgresql/postgresql-9.3.5-1.src.rpm
Source3: %{name}-generate-pdf.sh
Source6: %{name}.aix.init
Source7: %{name}-Makefile.regress
Source8: %{name}-pg_config.h
Source9: %{name}-ecpg_config.h
Source10: %{name}-README.rpm-dist
Source11: %{name}-bashprofile
# Results analysis script
Source13: %{name}-%{version}.resultats.ksh
# Script of passing tests base + Tap + extras ...
Source14: %{name}-%{version}.run_all_tests.ksh
Source20: %{name}-switch-to-32_bit.sh
Source21: %{name}-switch-to-64_bit.sh

Source30: %{name}-%{version}-%{release}.build.log
Source31: %{name}-%{version}-%{release}.perl_64bit_NoXLC.ksh

# /opt/freeware/include/sqltypes.h collides with a local sqltypes.h (from
# unixODBC-devel), thus do not include -I/opt/freeware/include in $CFLAGS.
# However, to detect krb5 we need -I/opt/freeware/include in $CFLAGS.
# Fails with v9.6.0 : --with-krb5 option of configure has disappeared.
#Patch0: %{name}-%{version}-aix.patch

Patch1: %{name}-%{version}-LARGE_FILES.patch
Patch2: %{name}-%{version}-aix_TestLib.pm.patch
# Used for issue with tests: create_aggregate and aggregates with XLC v13 and -O2 in 32bit
Patch3: %{name}-%{version}-32bit-XLCv13-O2-inline-pragma-O0.patch
# Used for removing the remove of inline for AIX/XLC/32bits
Patch4: %{name}-%{version}-32bit-XLC-O2-inline-NO-PG_FORCE_DISABLE_INLINE.patch


BuildRequires: bison, flex, gawk, make, jade
BuildRequires: bzip2 >= 1.0.2
BuildRequires: perl >= 5.8.8

%if %plpython
BuildRequires: python-devel >= 2.6.2
%endif

%if %pltcl
BuildRequires: tcl-devel >= 8.5.8-2
%endif

BuildRequires: readline-devel >= 5.2-3
Requires: readline >= 5.2-3
BuildRequires: zlib-devel >= 1.2.3-3
Requires: zlib >= 1.2.3-3

%if %ssl
BuildRequires: openssl-devel >= 1.0.1f-1
Requires: openssl >= 1.0.1f-1
%endif

%if %kerberos
BuildRequires: krb5-devel >= 1.8.3-1
Requires: krb5-libs >= 1.8.3-1
%endif

%if %ldap
BuildRequires: openldap-devel >= 2.4.23
Requires: openldap >= 2.4.23
%endif

%if %nls
BuildRequires: gettext >= 0.10.40
Requires: gettext >= 0.10.40
%endif

%if %xml
BuildRequires: libxml2-devel >= 2.7.8-3, libxslt-devel >= 1.1.24-1
Requires: libxml2 >= 2.7.8-3, libxslt >= 1.1.24-1
%endif

# main package requires -libs subpackage
Requires: %{name}-libs = %{version}-%{release}

Buildroot: %{_tmppath}/%{name}-%{version}-%{release}-root

%ifos aix5.1
Requires: AIX-rpm >= 5.1.0.0
Requires: AIX-rpm < 5.2.0.0
%endif
%ifos aix5.2
Requires: AIX-rpm >= 5.2.0.0
Requires: AIX-rpm < 5.3.0.0
%endif
%ifos aix5.3
Requires: AIX-rpm >= 5.3.0.0
Requires: AIX-rpm < 5.4.0.0
%endif
%ifos aix6.1
Requires: AIX-rpm >= 6.1.0.0
Requires: AIX-rpm < 6.2.0.0
%endif
%ifos aix7.1
Requires: AIX-rpm >= 7.1.0.0
Requires: AIX-rpm < 7.2.0.0
%endif
%ifos aix7.2
Requires: AIX-rpm >= 7.2.0.0
Requires: AIX-rpm < 7.3.0.0
%endif

%description
PostgreSQL is an advanced Object-Relational database management system
(DBMS) that supports almost all SQL constructs (including
transactions, sub-selects and user-defined types and functions). The
postgresql package includes the client programs and libraries that
you'll need to access a PostgreSQL DBMS server.  These PostgreSQL
client programs are programs that directly manipulate the internal
structure of PostgreSQL databases on a PostgreSQL server. These client
programs can be located on the same machine with the PostgreSQL
server, or may be on a remote machine which accesses a PostgreSQL
server over a network connection. This package contains the docs
in HTML for the whole package, as well as command-line utilities for
managing PostgreSQL databases on a PostgreSQL server. 

If you want to manipulate a PostgreSQL database on a local or remote PostgreSQL
server, you need this package. You also need to install this package
if you're installing the postgresql-server package.

The database is available as 32-bit and 64-bit.

%if %{gcc_compiler} == 1
This version has been compiled with GCC.
%else
This version has been compiled with XLC.
%endif



%package libs
Summary: The shared libraries required for any PostgreSQL clients
Group: Applications/Databases
Provides: libpq.so = %{version}-%{release}
Requires: gettext >= 0.10.40
Requires: krb5-libs >= 1.8.3-1
Requires: openldap >= 2.4.23
Requires: openssl >= 1.0.1f-1

%description libs
The postgresql-libs package provides the essential shared libraries for any 
PostgreSQL client program or interface. You will need to install this package
to use any other PostgreSQL package or any clients that need to connect to a
PostgreSQL server.

The library is available as 32-bit and 64-bit.


%package server
Summary: The programs needed to create and run a PostgreSQL server
Group: Applications/Databases
Requires: %{name} = %{version}-%{release}
Requires: %{name}-libs = %{version}-%{release}

%description server
The postgresql-server package includes the programs needed to create
and run a PostgreSQL server, which will in turn allow you to create
and maintain PostgreSQL databases.  PostgreSQL is an advanced
Object-Relational database management system (DBMS) that supports
almost all SQL constructs (including transactions, sub-selects and
user-defined types and functions). You should install
postgresql-server if you want to create and maintain your own
PostgreSQL databases and/or your own PostgreSQL server. You also need
to install the postgresql package.


%package docs
Summary: Extra documentation for PostgreSQL
Group: Applications/Databases
Requires: %{name} = %{version}-%{release}

%description docs
The postgresql-docs package includes some additional documentation for
PostgreSQL.  Currently, this includes the main documentation in PDF format
and source files for the PostgreSQL tutorial.


%package contrib
Summary: Contributed modules distributed with PostgreSQL
Group: Applications/Databases
Requires: %{name} = %{version}-%{release}
Requires: %{name}-libs = %{version}-%{release}

%description contrib
The postgresql-contrib package contains contributed packages that are
included in the PostgreSQL distribution.


%package devel
Summary: PostgreSQL development header files and libraries
Group: Development/Libraries
Requires: %{name} = %{version}-%{release}
Requires: %{name}-libs = %{version}-%{release}
Requires: readline >= 5.2-3
Requires: gettext >= 0.10.40

%description devel
The postgresql-devel package contains the header files and libraries
needed to compile C or C++ applications which will directly interact
with a PostgreSQL database management server and the ecpg Embedded C
Postgres preprocessor. You need to install this package if you want to
develop applications which will interact with a PostgreSQL server.

If you are compiling a 32-bit program, no special compiler options are
needed.

If you are compiling a 64-bit program, you have to compile and link your
application with "xlc_r -q64" or "gcc -maix64".


%if %plperl
%package plperl
Summary: The Perl procedural language for PostgreSQL
Group: Applications/Databases
Requires: %{name}-server = %{version}-%{release}
Requires: perl(:MODULE_COMPAT_%(eval "`/opt/freeware/bin/perl -V:version`"; echo $version))
Requires: perl >= 5.8.8

%description plperl
PostgreSQL is an advanced Object-Relational database management
system.  The postgresql-plperl package contains the PL/Perl
procedural language for the backend.
%endif


%if %plpython
%package plpython
Summary: The Python procedural language for PostgreSQL
Group: Applications/Databases
Requires: %{name}-server = %{version}-%{release}
Requires: python >= 2.6.2
Requires: gettext >= 0.10.40

%description plpython
PostgreSQL is an advanced Object-Relational database management
system.  The postgresql-plpython package contains the PL/Python
procedural language for the backend.
%endif


%if %pltcl
%package pltcl
Summary: The Tcl procedural language for PostgreSQL
Group: Applications/Databases
Requires: %{name}-server = %{version}-%{release}
Requires: tcl >= 8.5.8-2

%description pltcl
PostgreSQL is an advanced Object-Relational database management
system.  The postgresql-pltcl package contains the PL/Tcl
procedural language for the backend.
%endif


%if %test
%package test
Summary: The test suite distributed with PostgreSQL
Group: Applications/Databases
Requires: %{name}-server = %{version}-%{release}
Requires: readline >= 5.2-3
Requires: gettext >= 0.10.40

%description test
PostgreSQL is an advanced Object-Relational database management
system. The postgresql-test package includes the sources and pre-built
binaries of various tests for the PostgreSQL database management
system, including regression tests and benchmarks.
%endif


############################################################################
#                                                                          #
#                                %prep                                     #
#                                                                          #
############################################################################

%prep
export PATH=/opt/freeware/bin:$PATH

echo "dotests=%{dotests}"
echo "tap_tests=%{tap_tests}"
echo "runselftest=%{runselftest}"
echo "gcc_compiler=%{gcc_compiler}"
echo "default_bits=%{default_bits}"
%if %{gcc_compiler} == 1
echo "GCC version=`/opt/freeware/bin/gcc --version | head -1`"
%endif

echo "TESTUSER:%{TESTUSER}.%{TESTGROUP}"
# ulimit -m unlimited  is required for test "regex"
chuser rss=-1 data=-1 stack=-1 %{TESTUSER}

%setup -q 
#%patch0
%patch1
%patch2
%patch3
%patch4

cp -p %{SOURCE2} .

# remove .gitignore files to ensure none get into the RPMs
find . -type f -name .gitignore | xargs rm

# Duplicate source for 32 & 64 bits

rm -rf   /tmp/%{name}-%{version}-32bit
cp -pr . /tmp/%{name}-%{version}-32bit
rm -fr *
mv       /tmp/%{name}-%{version}-32bit 32bit
cp -pr 32bit 64bit


############################################################################
#                                                                          #
#                                %build                                    #
#                                                                          #
############################################################################

%build
rpm -qa
cp $0 %{name}-%{version}_script_build.ksh

# This line (use for tracing) sometimes generates the issue: "--trace:  not found" during tests
export MAKE="gmake "

export PATH=/opt/freeware/bin:/opt/freeware/bin:/usr/linux/bin:/usr/local/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java5/jre/bin:/usr/java5/bin

export AR=/usr/bin/ar

# fail quickly and obviously if user tries to build as root
# %if %runselftest
#     if [ x"`id -u`" = x0 ] ; then
#         echo "postgresql's regression tests fail if run as root."
#         echo "If you really need to build the RPM as root, use"
#         echo "--define='runselftest 0' to skip the regression tests."
#         exit 1
#     fi
# %endif

# we need GNU make before the AIX make

echo "CC: %{gcc_compiler}"

%if %{gcc_compiler} == 1
export CC__="/opt/freeware/bin/gcc"
export CXX__="/opt/freeware/bin/g++"
export FLAG32="-maix32"
export FLAG64="-maix64"

echo "CC Version:"
$CC__ --version

%else

# XLC specific (do NOT compile yet...)
export CC__="/usr/vac/bin/xlc"
export CC__="/opt/IBM/xlc/13.1.3/bin/xlc"
#export CC__="/usr/vac/bin/xlc"            # Version: 12.01.0000.0000


export CXX__="/usr/vacpp/bin/xlC"
export CXX__="/opt/IBM/xlC/13.1.0/bin/xlC"
#export CXX__="/usr/vac/bin/xlc"            # Version: 12.01.0000.0000

export FLAG32="-q32"
export FLAG64="-q64"

echo "CC Version:"
$CC__ -qversion

%endif

export CC32=" ${CC__}  ${FLAG32}"
#export CXX32="${CXX__} ${FLAG32}"
export CC64=" ${CC__}  ${FLAG64}"
#export CXX64="${CXX__} ${FLAG64}"

#export GLOBAL_CC_OPTIONS="-O2 "

############################### 64-bit BEGIN ##############################
env

cd 64bit
export LIBPATH=${RPM_BUILD_DIR}/%{name}-%{version}/64bit/tmp_install/opt/freeware/lib64/:${RPM_BUILD_DIR}/%{name}-%{version}/64bit/tmp_install/opt/freeware/lib64/postgresql:/opt/freeware/lib64:/opt/freeware/lib

# first build the 64-bit version
export OBJECT_MODE=64
export CC="${CC64}   $GLOBAL_CC_OPTIONS"
#export CXX="${CXX64} $GLOBAL_CC_OPTIONS"

export PERL=/opt/freeware/bin/perl_64bit
export PERL=%{SOURCE31}
# /opt/freeware/bin/perl_64bit_NoXLC :
# PERLVERSION=`/usr/bin/rpm -qa | grep perl-`
# PERLV=`echo $PERLVERSION | awk -F- '{print $2}'`
# PERLVV=`echo $PERLV | awk -F. '{print $1}'`
# PERLVERSION="perl$PERLVV/$PERLV"
# 
# echo $* | grep ldopts > /dev/null
# if [ $? -eq 0 ]
# then
#         echo "-Wl,-bE:/opt/freeware/lib/$PERLVERSION/ppc-aix-thread-multi-64all/CORE/perl.exp -s -L/opt/freeware/lib64 -L/usr/lib64 -L/opt/freeware/lib -Wl,-brtl -Wl,-bdynamic -L/usr/local/lib -Wl,-b64 -L/opt/freeware/lib/$PERLVERSION/ppc-aix-thread-multi-64all/CORE -lperl -lpthread -lbind -lnsl -ldl -lld -lm -lcrypt -lpthreads -lc"
# else
#         /opt/freeware/bin/perl_64bit "$@"
# fi


export PYTHON=/opt/freeware/bin/python_64
export LDFLAGS="-L/opt/freeware/lib64 -L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib64:/opt/freeware/lib:/usr/lib:/lib -lintl"

# unixODBC-devel contains a sqltypes.h which is different from:
#   /src/interfaces/ecpg/include/sqltypes.h
# ==> CCPFLAGS : -I`pwd`/src/include -I`pwd`/src/interfaces/ecpg/include

export CPPFLAGS="-I`pwd`/src/include -I`pwd`/src/interfaces/ecpg/include -I/opt/freeware/include -I/usr/include" ; 
export   CFLAGS="-I`pwd`/src/include -I`pwd`/src/interfaces/ecpg/include -I/opt/freeware/include -I/usr/include" ;

# For more information in building trace :  --enable-debug --enable-dtrace

./configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir64} \
    --mandir=%{_mandir} \
    --enable-debug \
%if %plperl
    --with-perl \
%endif
%if %pltcl
    --with-tcl \
    --with-tclconfig=%{_libdir} \
%endif
%if %plpython
    --with-python \
%endif
%if %ldap
    --with-ldap \
%endif
%if %ssl
    --with-openssl \
%endif
%if %xml
    --with-libxml \
    --with-libxslt \
%endif
%if %nls
    --enable-nls \
%endif
%if %pgfts
    --enable-thread-safety \
%endif
%if %tap_tests
    --enable-tap-tests \
%endif
    --sysconfdir=/etc/sysconfig/%{name}

$MAKE %{?_smp_mflags} world

# Have to hack makefile to put correct path into tutorial scripts
sed "s|C=\`pwd\`;|C=%{_libdir}/%{name}/tutorial;|" < src/tutorial/Makefile > src/tutorial/GNUmakefile
$MAKE %{?_smp_mflags} -C src/tutorial NO_PGXS=1 all
rm -f src/tutorial/GNUmakefile

%if %runselftest
    HERE=$PWD
    mkdir -p src/bin/scripts/tmp_check/log/
    find src/bin/scripts/tmp_check| xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/test                 | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/pl/plperl            | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/pl/plpgsql           | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/pl/plpython          | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/pl/tcl               | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/interfaces/ecpg/test | xargs chown %{TESTUSER}.%{TESTGROUP}
    find contrib/*                | xargs chown %{TESTUSER}.%{TESTGROUP}
#    find src/bin/pg_upgrade       | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/bin       | xargs chown %{TESTUSER}.%{TESTGROUP}
    [ -d tmp_install ] && find tmp_install | xargs chown %{TESTUSER}.%{TESTGROUP}
    chown %{TESTUSER}.%{TESTGROUP} .

    export EXTRA_TESTS_LIST="numeric_big"
#     Failing Extra Tests (to be studied) :
#	hs_standby_allowed
#	hs_standby_check
#	hs_standby_disallowed
#	hs_standby_functions"
#     Non-existing Extra Tests src/test/regress/expected/hs_primary_*.out files for:
#	hs_primary_extremes
#	hs_primary_setup

#   Do not use ! "su - ..." since environment variables, like: OBJECT_MODE, CC, etc. will not be passed
#   ulimit -m unlimited is required for "regex" test in 64bit and is done in %prep phase
    su %{TESTUSER} -c  "(set -x;cd $HERE || exit 1; \
$MAKE -k MAX_CONNECTIONS=5 check-world EXTRA_TESTS=$EXTRA_TESTS_LIST 2>&1;exit 0)"
    
    chown root.system .
    
%endif


############################### 64-bit END ##############################

############################### 32-bit BEGIN ##############################

cd ../32bit
export LIBPATH=${RPM_BUILD_DIR}/%{name}-%{version}/32bit/tmp_install/opt/freeware/lib/:${RPM_BUILD_DIR}/%{name}-%{version}/32bit/tmp_install/opt/freeware/lib/postgresql:/opt/freeware/lib:/opt/freeware/lib

# now build the 64-bit version
export OBJECT_MODE=32
export CC="${CC32}   $GLOBAL_CC_OPTIONS"
#export CXX="${CXX32} $GLOBAL_CC_OPTIONS"

export PERL=/opt/freeware/bin/perl
export PYTHON=/opt/freeware/bin/python
export LDFLAGS="-L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib:/usr/lib:/lib -Wl,-bmaxdata:0x80000000 -lintl"

export CPPFLAGS="-I`pwd`/src/include -I`pwd`/src/interfaces/ecpg/include -I/opt/freeware/include -I/usr/include" ;
export   CFLAGS="-I`pwd`/src/include -I`pwd`/src/interfaces/ecpg/include -I/opt/freeware/include -I/usr/include" ;

./configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir} \
    --mandir=%{_mandir} \
    --enable-debug \
%if %plperl
    --with-perl \
%endif
%if %pltcl
    --with-tcl \
    --with-tclconfig=%{_libdir} \
%endif
%if %plpython
    --with-python \
%endif
%if %ldap
    --with-ldap \
%endif
%if %ssl
    --with-openssl \
%endif
%if %xml
    --with-libxml \
    --with-libxslt \
%endif
%if %nls
    --enable-nls \
%endif
%if %pgfts
    --enable-thread-safety \
%endif
%if %tap_tests
    --enable-tap-tests \
%endif
    --sysconfdir=/etc/sysconfig/%{name}

$MAKE %{?_smp_mflags} world

# Have to hack makefile to put correct path into tutorial scripts
sed "s|C=\`pwd\`;|C=%{_libdir}/%{name}/tutorial;|" < src/tutorial/Makefile > src/tutorial/GNUmakefile
$MAKE %{?_smp_mflags} -C src/tutorial NO_PGXS=1 all
rm -f src/tutorial/GNUmakefile

%if %runselftest
    HERE=$PWD
    mkdir -p src/bin/scripts/tmp_check/log/
    find src/bin/scripts/tmp_check| xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/test                 | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/pl/plperl            | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/pl/plpgsql           | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/pl/plpython          | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/pl/tcl               | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/interfaces/ecpg/test | xargs chown %{TESTUSER}.%{TESTGROUP}
    find contrib/*                | xargs chown %{TESTUSER}.%{TESTGROUP}
#    find src/bin/pg_upgrade       | xargs chown %{TESTUSER}.%{TESTGROUP}
    find src/bin       | xargs chown %{TESTUSER}.%{TESTGROUP}
    [ -d tmp_install ] && find tmp_install | xargs chown %{TESTUSER}.%{TESTGROUP}
    chown %{TESTUSER}.%{TESTGROUP} .

    export EXTRA_TESTS_LIST="numeric_big"
#     Failing Extra Tests (to be studied) :
#	hs_standby_allowed
#	hs_standby_check
#	hs_standby_disallowed
#	hs_standby_functions"
#     Non-existing Extra Tests src/test/regress/expected/hs_primary_*.out files for:
#	hs_primary_extremes
#	hs_primary_setup

    su %{TESTUSER} -c  "(set -x; cd $HERE || exit 1; $MAKE -k MAX_CONNECTIONS=5 check-world EXTRA_TESTS=$EXTRA_TESTS_LIST 2>&1; exit 0)"
    
    chown root.system .
    
%endif


############################### 32-bit END ##############################


############################################################################
#                                                                          #
#                                %install                                  #
#                                                                          #
############################################################################

%install

if [ "%{default_bits}" == 32 ]; then
    DEFAULT_BITS=32;
else
    DEFAULT_BITS=64;
fi


export PATH=/opt/freeware/bin:/opt/freeware/bin:/usr/linux/bin:/usr/local/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java5/jre/bin:/usr/java5/bin
export AR=/usr/bin/ar

cp $0 %{name}-%{version}_script_install.ksh

[ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf ${RPM_BUILD_ROOT}

export MAKE="gmake --trace"
cd 64bit
export OBJECT_MODE=64
export LDFLAGS="-L/opt/freeware/lib64 -L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib64:/opt/freeware/lib:/usr/lib:/lib -lintl"

$MAKE DESTDIR=${RPM_BUILD_ROOT} install-world

cd src/test/modules
$MAKE DESTDIR=${RPM_BUILD_ROOT} install
cd -
cd contrib
$MAKE DESTDIR=${RPM_BUILD_ROOT} install
cd -

(
  cd ${RPM_BUILD_ROOT}%{_bindir}
  for f in * ; do
    mv -f ${f} ${f}_64
  done
)

# multilib header hack; note pg_config.h is installed in two places!
mv ${RPM_BUILD_ROOT}%{_includedir}/pg_config.h ${RPM_BUILD_ROOT}%{_includedir}/pg_config-ppc64.h
mv ${RPM_BUILD_ROOT}%{_includedir}/%{name}/server/pg_config.h ${RPM_BUILD_ROOT}%{_includedir}/%{name}/server/pg_config-ppc64.h

mv ${RPM_BUILD_ROOT}%{_includedir}/ecpg_config.h ${RPM_BUILD_ROOT}%{_includedir}/ecpg_config-ppc64.h

cd ../32bit
export OBJECT_MODE=32
export LDFLAGS="-L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib:/usr/lib:/lib -Wl,-bmaxdata:0x80000000 -lintl"

$MAKE DESTDIR=${RPM_BUILD_ROOT} install

cd src/test/modules
$MAKE DESTDIR=${RPM_BUILD_ROOT} install
cd -
cd contrib
$MAKE DESTDIR=${RPM_BUILD_ROOT} install
cd -

(
  cd ${RPM_BUILD_ROOT}%{_bindir}
  for f in `ls -1 | grep -v _64` ; do
    mv -f ${f} ${f}_32
  done
  ln -fs postgres_64 postmaster_64
  ln -fs postgres_32 postmaster_32
)

/usr/bin/strip -X32_64 ${RPM_BUILD_ROOT}%{_bindir}/* || :

# make the 64-bit version the default version
(
  cd ${RPM_BUILD_ROOT}%{_bindir}
  for file in `ls -1 | grep -v _64` ; do
    f=`basename ${file} _32`
    ln -s ${f}_${DEFAULT_BITS} ${f}
  done

  cp %{SOURCE20} .
  cp %{SOURCE21} .
  chmod 0755 %{name}-switch-to-*.sh
)

# dynamic library :
# 1/ extract *.so from lib64
# 2/ merge *.so (lib64) to lib32
# 3/ Create a link from lib64 to lib32


# add the 64-bit shared objects to the shared libraries containing already the
# 32-bit shared objects

(
  cd ${RPM_BUILD_ROOT}%{_libdir64}
  for f in ecpg ecpg_compat pgtypes pq ; do
      /usr/bin/ar -X64 -x lib${f}.a
      rm -f lib${f}.a
      ln -s ../lib/lib${f}.a lib${f}.a
      /usr/bin/ar -X64 -q ${RPM_BUILD_ROOT}%{_libdir}/lib${f}.a ${RPM_BUILD_ROOT}%{_libdir64}/lib${f}.so*
      # Strip -e .so files that appear in .a files
      strip -X64 -e ${RPM_BUILD_ROOT}%{_libdir64}/lib${f}.so*
  done
  
# extraction /lib  of *.so
  cd ${RPM_BUILD_ROOT}%{_libdir}
  for f in ecpg ecpg_compat pgtypes pq ; do
    /usr/bin/ar -X32 -x lib${f}.a
    # Strip -e .so files that appear in .a files
    strip -X32 -e ${RPM_BUILD_ROOT}%{_libdir}/lib${f}.so*
  done

)

# multilib header hack; note pg_config.h is installed in two places!
mv ${RPM_BUILD_ROOT}%{_includedir}/pg_config.h ${RPM_BUILD_ROOT}%{_includedir}/pg_config-ppc32.h
mv ${RPM_BUILD_ROOT}%{_includedir}/%{name}/server/pg_config.h ${RPM_BUILD_ROOT}%{_includedir}/%{name}/server/pg_config-ppc32.h
cp %{SOURCE8} ${RPM_BUILD_ROOT}%{_includedir}/pg_config.h
cp %{SOURCE8} ${RPM_BUILD_ROOT}%{_includedir}/%{name}/server/pg_config.h

mv ${RPM_BUILD_ROOT}%{_includedir}/ecpg_config.h ${RPM_BUILD_ROOT}%{_includedir}/ecpg_config-ppc32.h
cp %{SOURCE9} ${RPM_BUILD_ROOT}%{_includedir}/ecpg_config.h

mkdir -p ${RPM_BUILD_ROOT}%{_libdir}/%{name}/tutorial
chmod 0755 ${RPM_BUILD_ROOT}%{_libdir}/%{name}/tutorial
cp src/tutorial/* ${RPM_BUILD_ROOT}%{_libdir}/%{name}/tutorial/

# prep the initscript, including insertion of some values it needs
mkdir -p ${RPM_BUILD_ROOT}/etc/rc.d/init.d
sed -e 's|^PGVERSION=.*$|PGVERSION=%{version}|' \
	-e 's|^PGDOCDIR=.*$|PGDOCDIR=%{_docdir}/%{name}-%{version}|' \
	< %{SOURCE6} > %{name}.init
cp %{name}.init ${RPM_BUILD_ROOT}/etc/rc.d/init.d/%{name}
chmod 0755 ${RPM_BUILD_ROOT}/etc/rc.d/init.d/%{name}

# PGDATA needs removal of group and world permissions due to pg_pwd hole.
mkdir -p ${RPM_BUILD_ROOT}/var/lib/%{name}/data
chmod 0700 ${RPM_BUILD_ROOT}/var/lib/%{name}/data

# backups of data go here...
mkdir -p ${RPM_BUILD_ROOT}/var/lib/%{name}/backups
chmod 0700 ${RPM_BUILD_ROOT}/var/lib/%{name}/backups

# postgres' .bash_profile
cp %{SOURCE11} ${RPM_BUILD_ROOT}/var/lib/%{name}/.bash_profile
chmod 0644 %{SOURCE11} ${RPM_BUILD_ROOT}/var/lib/%{name}/.bash_profile

# Create the multiple postmaster startup directory
mkdir -p ${RPM_BUILD_ROOT}/etc/sysconfig/%{name}
chmod 0700 ${RPM_BUILD_ROOT}/etc/sysconfig/%{name}

mkdir -p ${RPM_BUILD_ROOT}%{_libdir}/%{name}/pgxs/src/backend/port/aix
mkdir -p ${RPM_BUILD_ROOT}%{_libdir64}/%{name}/pgxs/src/backend/port/aix
cp src/backend/port/aix/mkldexport.sh \
    ${RPM_BUILD_ROOT}%{_libdir}/%{name}/pgxs/src/backend/port/aix
cp src/backend/port/aix/mkldexport.sh \
    ${RPM_BUILD_ROOT}%{_libdir64}/%{name}/pgxs/src/backend/port/aix
chmod 0755 ${RPM_BUILD_ROOT}%{_libdir}/%{name}/pgxs/src/backend/port/aix/mkldexport.sh
chmod 0755 ${RPM_BUILD_ROOT}%{_libdir64}/%{name}/pgxs/src/backend/port/aix/mkldexport.sh

%if %test
cd ../64bit
(
    # tests. There are many files included here that are unnecessary,
    # but include them anyway for completeness.  We replace the original
    # Makefiles, however.
    mkdir -p ${RPM_BUILD_ROOT}%{_libdir64}/%{name}/test
    cp -p -r src/test/regress ${RPM_BUILD_ROOT}%{_libdir64}/%{name}/test
    cd ${RPM_BUILD_ROOT}%{_libdir64}/%{name}/test/regress
    strip -X64 *.so
    rm -f GNUmakefile Makefile *.o
    cp %{SOURCE7} ${RPM_BUILD_ROOT}%{_libdir64}/%{name}/test/regress/Makefile
    chmod 0644 ${RPM_BUILD_ROOT}%{_libdir64}/%{name}/test/regress/Makefile
)
cd ../32bit
(
    # tests. There are many files included here that are unnecessary,
    # but include them anyway for completeness.  We replace the original
    # Makefiles, however.
    mkdir -p ${RPM_BUILD_ROOT}%{_libdir}/%{name}/test
    cp -p -r src/test/regress ${RPM_BUILD_ROOT}%{_libdir}/%{name}/test
    cd ${RPM_BUILD_ROOT}%{_libdir}/%{name}/test/regress
    strip -X32 *.so
    rm -f GNUmakefile Makefile *.o
    cp %{SOURCE7} ${RPM_BUILD_ROOT}%{_libdir}/%{name}/test/regress/Makefile
    chmod 0644 ${RPM_BUILD_ROOT}%{_libdir}/%{name}/test/regress/Makefile
)

(
  # librairy : libregress.a
  f=regress
  cd ${RPM_BUILD_ROOT}%{_libdir64}/postgresql/test/regress
  /usr/bin/ar  -X64 -x lib${f}.a
  rm -f lib${f}.a
  ln -s ../../../../lib/postgresql/test/regress/lib${f}.a lib${f}.a
  /usr/bin/ar -X64 -q ${RPM_BUILD_ROOT}%{_libdir}/postgresql/test/regress/lib${f}.a ${RPM_BUILD_ROOT}%{_libdir64}/postgresql/test/regress/${f}.so*
#  rm -f  ${f}.so*
  cd ${RPM_BUILD_ROOT}%{_libdir}/postgresql/test/regress
#  rm -f  ${f}.so*
####  /usr/bin/ar  -X64 -x lib${f}.a
)
%endif

# Fix some more documentation
# gzip doc/internals.ps
cp %{SOURCE10} README.rpm-dist
mv ${RPM_BUILD_ROOT}%{_datadir}/doc/%{name}/html doc
rm -rf ${RPM_BUILD_ROOT}%{_datadir}/doc/%{name}

# %find_lang does not find *any* message catalogs, thus use my own version
my_find_lang()
{
  /usr/bin/find ${RPM_BUILD_ROOT} -name $1.mo | \
      /usr/bin/sed "s|${RPM_BUILD_ROOT}||g" > $1.lang
}


my_find_lang ecpg-%{majorversion}
cat ecpg-%{majorversion}.lang > devel.lst
my_find_lang ecpglib6-%{majorversion}
cat ecpglib6-%{majorversion}.lang > libs.lst
my_find_lang initdb-%{majorversion}
cat initdb-%{majorversion}.lang > server.lst
my_find_lang libpq5-%{majorversion}
cat libpq5-%{majorversion}.lang >> libs.lst
my_find_lang pg_basebackup-%{majorversion}
cat pg_basebackup-%{majorversion}.lang >>server.lst
my_find_lang pg_controldata-%{majorversion}
cat pg_controldata-%{majorversion}.lang >> server.lst
my_find_lang pg_ctl-%{majorversion}
cat pg_ctl-%{majorversion}.lang >> server.lst
my_find_lang pg_config-%{majorversion}
cat pg_config-%{majorversion}.lang > main.lst
my_find_lang pg_dump-%{majorversion}
cat pg_dump-%{majorversion}.lang >> main.lst
my_find_lang pg_resetxlog-%{majorversion}
cat pg_resetxlog-%{majorversion}.lang >> server.lst
my_find_lang pgscripts-%{majorversion}
cat pgscripts-%{majorversion}.lang >> main.lst
%if %plperl
my_find_lang plperl-%{majorversion}
cat plperl-%{majorversion}.lang > plperl.lst
%endif
my_find_lang plpgsql-%{majorversion}
cat plpgsql-%{majorversion}.lang >> server.lst
%if %plpython
my_find_lang plpython-%{majorversion}
cat plpython-%{majorversion}.lang > plpython.lst
%endif
%if %pltcl
my_find_lang pltcl-%{majorversion}
cat pltcl-%{majorversion}.lang > pltcl.lst
%endif
my_find_lang postgres-%{majorversion}
cat postgres-%{majorversion}.lang >> server.lst
my_find_lang psql-%{majorversion}
cat psql-%{majorversion}.lang >> main.lst

# Create links from /usr/include -> (package devel) opt/freeware 
(
    cd ${RPM_BUILD_ROOT}%{_prefix}/include
    find . -type d |
	while read dirs;
        do
	    dir=$(echo $dirs| sed -e 's;^./;;')
            p=$(echo /usr/include/$dirs| sed -e 's;[^/];;g' -e 's;/$;;g' -e 's;/;../;g');
            mkdir -p ${RPM_BUILD_ROOT}/usr/include/$dir;
	    cd ${RPM_BUILD_ROOT}/usr/include/$dir;
	    ls -l $p"%{_prefix}"/include/$dir/*.h 2>/dev/null >/dev/null || continue;
            ln -sf $p"%{_prefix}"/include/$dir/*.h .;
            cd -;
        done
)

# Create others links from /usr -> (package libs et rpm de base) /opt/freeware 
(
    cd ${RPM_BUILD_ROOT}
    for dir in bin lib lib64
    do
	mkdir -p usr/${dir}
	cd usr/${dir}
	ln -sf ../..%{_prefix}/${dir}/* .
	cd -
    done
)

%pre server
# add the "postgres" group only if it does not yet exist
result=`/usr/sbin/lsgroup postgres | /usr/bin/awk '{ print $1 }' 2>/dev/null`
if [[ "${result}" != "postgres" ]] ; then
    /usr/bin/mkgroup -A id=%{postgres_gid} postgres 2> /dev/null || :
fi
# add the "postgres" user only if it does not yet exist
result=`/usr/sbin/lsuser postgres| /usr/bin/awk '{ print $1 }' 2>/dev/null`
if [[ "${result}" != "postgres" ]] ; then
    /usr/bin/mkuser id=%{postgres_uid} pgrp='postgres' gecos='PostgreSQL Server' \
        home='/var/lib/%{name}' \
        login='true' rlogin='true' postgres 2> /dev/null || :
fi


%preun server
if [ $1 = 0 ] ; then
    /etc/rc.d/init.d/%{name} stop >/dev/null 2>&1
fi
# remove "postgres" user and group
/usr/sbin/rmuser -p postgres || :
/usr/sbin/rmgroup postgres || :


%postun server
if [ $1 -ge 1 ] ; then
    /etc/rc.d/init.d/%{name} condrestart >/dev/null 2>&1 || :
fi


%clean
# [ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf ${RPM_BUILD_ROOT}


# FILES section.

%files -f 32bit/main.lst
%defattr(-,root,system)
%doc 32bit/doc/KNOWN_BUGS 32bit/doc/MISSING_FEATURES 32bit/doc/TODO
%doc 32bit/COPYRIGHT 32bit/README 32bit/HISTORY 32bit/doc/bug.template
%doc 32bit/README.rpm-dist
%{_bindir}/clusterdb*
%{_bindir}/createdb*
%{_bindir}/createlang*
%{_bindir}/createuser*
%{_bindir}/dropdb*
%{_bindir}/droplang*
%{_bindir}/dropuser*
%{_bindir}/pg_config*
%{_bindir}/pg_dump*
# %{_bindir}/pg_dumpall*
%{_bindir}/pg_isready*
%{_bindir}/pg_restore*
%{_bindir}/pg_upgrade*
%{_bindir}/psql*
%{_bindir}/reindexdb*
%{_bindir}/vacuumdb*
%{_bindir}/%{name}-switch-to-*.sh

/usr/bin/clusterdb*
/usr/bin/createdb*
/usr/bin/createlang*
/usr/bin/createuser*
/usr/bin/dropdb*
/usr/bin/droplang*
/usr/bin/dropuser*
/usr/bin/pg_config*
/usr/bin/pg_dump*
# /usr/bin/pg_dumpall*
/usr/bin/pg_isready*
/usr/bin/pg_restore*
/usr/bin/pg_upgrade*
/usr/bin/psql*
/usr/bin/reindexdb*
/usr/bin/vacuumdb*
/usr/bin/%{name}-switch-to-*.sh

%dir %{_libdir}/%{name}
%dir %{_libdir64}/%{name}

%{_mandir}/man1/clusterdb.*
%{_mandir}/man1/createdb.*
%{_mandir}/man1/createlang.*
%{_mandir}/man1/createuser.*
%{_mandir}/man1/dropdb.*
%{_mandir}/man1/droplang.*
%{_mandir}/man1/dropuser.*
%{_mandir}/man1/pg_dump.*
%{_mandir}/man1/pg_dumpall.*
%{_mandir}/man1/pg_isready.*
%{_mandir}/man1/pg_restore.*
%{_mandir}/man1/pg_upgrade.*
%{_mandir}/man1/psql.*
%{_mandir}/man1/reindexdb.*
%{_mandir}/man1/vacuumdb.*
%{_mandir}/man7/*

# Dynamic library
%{_libdir}/libecpg.a
%{_libdir}/libecpg.so*
%{_libdir64}/libecpg.so*
%{_libdir}/libecpg_compat.a
%{_libdir}/libecpg_compat.so*
%{_libdir64}/libecpg_compat.so*
%{_libdir}/libpgtypes.a
%{_libdir}/libpgtypes.so*
%{_libdir64}/libpgtypes.so*
%{_libdir}/libpq.a
%{_libdir}/libpq.so*
%{_libdir64}/libpq.so*
# Statics library
%{_libdir}/libpgcommon.a
%{_libdir64}/libpgcommon.a
%{_libdir}/libpgport.a
%{_libdir64}/libpgport.a
%{_libdir}/libpgfeutils.a
%{_libdir64}/libpgfeutils.a

# link from lib64 to lib for dynamics lybrary
%{_libdir64}/libpq.a
%{_libdir64}/libecpg.a
%{_libdir64}/libecpg_compat.a
%{_libdir64}/libpgtypes.a

# create link from /usr/bin to /opt/freeware/bin
/usr/lib/*.a
/usr/lib64/*.a
/usr/lib/*.so*
/usr/lib64/*so*



%files docs
%defattr(-,root,system)
%doc 32bit/*-US.pdf
%doc 32bit/doc/html
%{_libdir}/%{name}/tutorial/


%files contrib
%defattr(-,root,system)
%doc 32bit/contrib/spi/*.example
%{_bindir}/oid2name*
%{_bindir}/pg_archivecleanup*
%{_bindir}/pg_standby*
%{_bindir}/pg_test_fsync*
%{_bindir}/pg_test_timing*
%{_bindir}/pg_xlogdump*
%{_bindir}/pgbench*
%{_bindir}/vacuumlo*

/usr/bin/oid2name*
/usr/bin/pg_archivecleanup*
/usr/bin/pg_standby*
/usr/bin/pg_test_fsync*
/usr/bin/pg_test_timing*
/usr/bin/pg_xlogdump*
/usr/bin/pgbench*
/usr/bin/vacuumlo*

%{_datadir}/%{name}/extension/adminpack*
%{_datadir}/%{name}/extension/autoinc*
%{_datadir}/%{name}/extension/bloom*
%{_datadir}/%{name}/extension/btree_gin*
%{_datadir}/%{name}/extension/btree_gist*
%{_datadir}/%{name}/extension/chkpass*
%{_datadir}/%{name}/extension/citext*
%{_datadir}/%{name}/extension/cube*
%{_datadir}/%{name}/extension/dblink*
%{_datadir}/%{name}/extension/dict_int*
%{_datadir}/%{name}/extension/dict_xsyn*
%{_datadir}/%{name}/extension/earthdistance*
%{_datadir}/%{name}/extension/file_fdw*
%{_datadir}/%{name}/extension/fuzzystrmatch*
%{_datadir}/%{name}/extension/hstore*
%{_datadir}/%{name}/extension/insert_username*
%{_datadir}/%{name}/extension/intagg*
%{_datadir}/%{name}/extension/intarray*
%{_datadir}/%{name}/extension/isn*
%{_datadir}/%{name}/extension/lo*
%{_datadir}/%{name}/extension/ltree*
%{_datadir}/%{name}/extension/moddatetime*
%{_datadir}/%{name}/extension/pageinspect*
%{_datadir}/%{name}/extension/pg_buffercache*
%{_datadir}/%{name}/extension/pg_freespacemap*
%{_datadir}/%{name}/extension/pg_prewarm*
%{_datadir}/%{name}/extension/pg_stat_statements*
%{_datadir}/%{name}/extension/pg_trgm*
%{_datadir}/%{name}/extension/pg_visibility*
%{_datadir}/%{name}/extension/pgcrypto*
%{_datadir}/%{name}/extension/pgrowlocks*
%{_datadir}/%{name}/extension/pgstattuple*
%{_datadir}/%{name}/extension/postgres_fdw*
%{_datadir}/%{name}/extension/refint*
%{_datadir}/%{name}/extension/seg*
%{_datadir}/%{name}/extension/tablefunc*
%{_datadir}/%{name}/extension/tcn*
%{_datadir}/%{name}/extension/timetravel*
%{_datadir}/%{name}/extension/tsearch2*
%{_datadir}/%{name}/extension/tsm_system_rows*
%{_datadir}/%{name}/extension/tsm_system_time*
%{_datadir}/%{name}/extension/unaccent*
%{_libdir}/%{name}/_int.so
%{_libdir64}/%{name}/_int.so
%{_libdir}/%{name}/adminpack.so
%{_libdir64}/%{name}/adminpack.so
%{_libdir}/%{name}/auth_delay.so
%{_libdir64}/%{name}/auth_delay.so
%{_libdir}/%{name}/auto_explain.so
%{_libdir64}/%{name}/auto_explain.so
%{_libdir}/%{name}/autoinc.so
%{_libdir64}/%{name}/autoinc.so
%{_libdir}/%{name}/bloom.so
%{_libdir64}/%{name}/bloom.so
%{_libdir}/%{name}/btree_gin.so
%{_libdir64}/%{name}/btree_gin.so
%{_libdir}/%{name}/btree_gist.so
%{_libdir64}/%{name}/btree_gist.so
%{_libdir}/%{name}/chkpass.so
%{_libdir64}/%{name}/chkpass.so
%{_libdir}/%{name}/citext.so
%{_libdir64}/%{name}/citext.so
%{_libdir}/%{name}/cube.so
%{_libdir64}/%{name}/cube.so
%{_libdir}/%{name}/dblink.so
%{_libdir64}/%{name}/dblink.so
%{_libdir}/%{name}/dict_int.so
%{_libdir64}/%{name}/dict_int.so
%{_libdir}/%{name}/dict_xsyn.so
%{_libdir64}/%{name}/dict_xsyn.so
%{_libdir}/%{name}/earthdistance.so
%{_libdir64}/%{name}/earthdistance.so
%{_libdir}/%{name}/file_fdw.so
%{_libdir64}/%{name}/file_fdw.so
%{_libdir}/%{name}/fuzzystrmatch.so
%{_libdir64}/%{name}/fuzzystrmatch.so
%{_libdir}/%{name}/hstore.so
%{_libdir64}/%{name}/hstore.so
%if %plperl
%{_libdir}/%{name}/hstore_plperl.so
%{_libdir64}/%{name}/hstore_plperl.so
%endif
%if %plpython
%{_libdir}/%{name}/hstore_plpython2.so
%{_libdir64}/%{name}/hstore_plpython2.so
%{_libdir}/%{name}/ltree_plpython2.so
%{_libdir64}/%{name}/ltree_plpython2.so
%endif
%{_libdir}/%{name}/insert_username.so
%{_libdir64}/%{name}/insert_username.so
%{_libdir}/%{name}/isn.so
%{_libdir64}/%{name}/isn.so
%{_libdir}/%{name}/lo.so
%{_libdir64}/%{name}/lo.so
%{_libdir}/%{name}/ltree.so
%{_libdir64}/%{name}/ltree.so

%{_libdir}/%{name}/moddatetime.so
%{_libdir64}/%{name}/moddatetime.so

%{_libdir}/%{name}/pageinspect.so
%{_libdir64}/%{name}/pageinspect.so

%{_libdir}/%{name}/passwordcheck.so
%{_libdir64}/%{name}/passwordcheck.so

%{_libdir}/%{name}/pg_buffercache.so
%{_libdir64}/%{name}/pg_buffercache.so
%{_libdir}/%{name}/pg_freespacemap.so
%{_libdir64}/%{name}/pg_freespacemap.so
%{_libdir}/%{name}/pg_stat_statements.so
%{_libdir64}/%{name}/pg_stat_statements.so
%{_libdir}/%{name}/pg_trgm.so
%{_libdir64}/%{name}/pg_trgm.so
%{_libdir}/%{name}/pg_visibility.so
%{_libdir64}/%{name}/pg_visibility.so
%{_libdir}/%{name}/pgcrypto.so
%{_libdir64}/%{name}/pgcrypto.so
%{_libdir}/%{name}/pgrowlocks.so
%{_libdir64}/%{name}/pgrowlocks.so
%{_libdir}/%{name}/pgstattuple.so
%{_libdir64}/%{name}/pgstattuple.so
%{_libdir}/%{name}/postgres_fdw.so
%{_libdir64}/%{name}/postgres_fdw.so
%{_libdir}/%{name}/refint.so
%{_libdir64}/%{name}/refint.so
%{_libdir}/%{name}/seg.so
%{_libdir64}/%{name}/seg.so
%{_libdir}/%{name}/tablefunc.so
%{_libdir64}/%{name}/tablefunc.so
%{_libdir}/%{name}/tcn.so
%{_libdir64}/%{name}/tcn.so
%{_libdir}/%{name}/test_decoding.so
%{_libdir64}/%{name}/test_decoding.so
%{_libdir}/%{name}/timetravel.so
%{_libdir64}/%{name}/timetravel.so
%{_libdir}/%{name}/tsearch2.so
%{_libdir64}/%{name}/tsearch2.so
%{_libdir}/%{name}/tsm_system_rows.so
%{_libdir64}/%{name}/tsm_system_rows.so
%{_libdir}/%{name}/tsm_system_time.so
%{_libdir64}/%{name}/tsm_system_time.so
%{_libdir}/%{name}/unaccent.so
%{_libdir64}/%{name}/unaccent.so
%{_mandir}/man1/oid2name.*
%{_mandir}/man1/pg_archivecleanup.*
%{_mandir}/man1/pg_recvlogical.*
%{_mandir}/man1/pg_standby.*
%{_mandir}/man1/pg_test_fsync.*
%{_mandir}/man1/pg_test_timing.*
%{_mandir}/man1/pg_xlogdump.*
%{_mandir}/man1/pgbench.*
%{_mandir}/man1/vacuumlo.*
%{_mandir}/man3/dblink*

%if %ssl
%{_datadir}/%{name}/extension/sslinfo*
%{_libdir}/%{name}/sslinfo.so
%{_libdir64}/%{name}/sslinfo.so
%endif

%if %xml
%{_datadir}/%{name}/extension/xml2*
%{_libdir}/%{name}/pgxml.so
%{_libdir64}/%{name}/pgxml.so
%endif

# create link from /usr/lib(64)postgresql to opt/freeware/lib64/postgresql
/usr/lib/%{name}
/usr/lib64/%{name}

%files libs -f 32bit/libs.lst
%defattr(-,root,system)
%doc 32bit/COPYRIGHT
# Dynamic library
%{_libdir}/libecpg.a
%{_libdir}/libecpg.so*
%{_libdir64}/libecpg.so*
%{_libdir}/libecpg_compat.a
%{_libdir}/libecpg_compat.so*
%{_libdir64}/libecpg_compat.so*
%{_libdir}/libpgtypes.a
%{_libdir}/libpgtypes.so*
%{_libdir64}/libpgtypes.so*
%{_libdir}/libpq.a
%{_libdir}/libpq.so*
%{_libdir64}/libpq.so*
# Statics library
%{_libdir}/libpgcommon.a
%{_libdir64}/libpgcommon.a
%{_libdir}/libpgport.a
%{_libdir64}/libpgport.a
%{_libdir}/libpgfeutils.a
%{_libdir64}/libpgfeutils.a
# link from lib64 to lib for dynamics lybrary
%{_libdir64}/libpq.a
%{_libdir64}/libecpg.a
%{_libdir64}/libecpg_compat.a
%{_libdir64}/libpgtypes.a

# create link from /usr/bin to /opt/freeware/bin
/usr/lib/*.a
/usr/lib64/*.a
/usr/lib/*.so*
/usr/lib64/*so*

%files server -f 32bit/server.lst
%defattr(-,root,system)
/etc/rc.d/init.d/postgresql
%attr (755,root,system) %dir /etc/sysconfig/%{name}
%{_bindir}/initdb*
%{_bindir}/pg_basebackup*
%{_bindir}/pg_controldata*
%{_bindir}/pg_ctl*
%{_bindir}/pg_receivexlog*
%{_bindir}/pg_recvlogical*
%{_bindir}/pg_resetxlog*
%{_bindir}/pg_rewind*
%{_bindir}/postgres*
%{_bindir}/postmaster*

/usr/bin/initdb*
/usr/bin/pg_basebackup*
/usr/bin/pg_controldata*
/usr/bin/pg_ctl*
/usr/bin/pg_receivexlog*
/usr/bin/pg_recvlogical*
/usr/bin/pg_resetxlog*
/usr/bin/pg_rewind*
/usr/bin/postgres*
/usr/bin/postmaster*

%{_libdir}/%{name}/dict_snowball.so
%{_libdir64}/%{name}/dict_snowball.so
%{_libdir}/%{name}/plpgsql.so
%{_libdir64}/%{name}/plpgsql.so
%{_libdir}/%{name}/libpqwalreceiver.so
%{_libdir64}/%{name}/libpqwalreceiver.so
%{_libdir}/%{name}/pg_prewarm.so
%{_libdir64}/%{name}/pg_prewarm.so
%{_libdir}/%{name}/*_and_*.so
%{_libdir64}/%{name}/*_and_*.so
%{_libdir}/%{name}/euc2004_sjis2004.so
%{_libdir64}/%{name}/euc2004_sjis2004.so

# create link from /usr/lib(64)postgresql to opt/freeware/lib64/postgresql
/usr/lib/%{name}
/usr/lib64/%{name}

%dir %{_datadir}/%{name}
%{_datadir}/%{name}/*.sample
%{_datadir}/%{name}/conversion_create.sql
%dir %{_datadir}/%{name}/extension
%{_datadir}/%{name}/extension/plpgsql*
%{_datadir}/%{name}/information_schema.sql
%{_datadir}/%{name}/postgres.bki
%{_datadir}/%{name}/postgres.description
%{_datadir}/%{name}/postgres.shdescription
%{_datadir}/%{name}/snowball_create.sql
%{_datadir}/%{name}/sql_features.txt
%{_datadir}/%{name}/system_views.sql
%{_datadir}/%{name}/timezone/
%{_datadir}/%{name}/timezonesets/
%{_datadir}/%{name}/tsearch_data/

# %attr(700,postgres,postgres) %dir /var/lib/%{name}
# %attr(700,postgres,postgres) %dir /var/lib/%{name}/data
# %attr(700,postgres,postgres) %dir /var/lib/%{name}/backups
# %attr(644,postgres,postgres) %config(noreplace) /var/lib/%{name}/.bash_profile

%{_mandir}/man1/initdb.*
%{_mandir}/man1/pg_basebackup.*
%{_mandir}/man1/pg_controldata.*
%{_mandir}/man1/pg_ctl.*
%{_mandir}/man1/pg_receivexlog.*
%{_mandir}/man1/pg_resetxlog.*
%{_mandir}/man1/pg_rewind.*
%{_mandir}/man1/postgres.*
%{_mandir}/man1/postmaster.*

# %{_tmpfilesdir}/postgresql.conf
# %{_unitdir}/postgresql.service
# %{_unitdir}/postgresql@.service
%attr(700,postgres,postgres) %dir /var/lib/%{name}
%attr(644,postgres,postgres) %config(noreplace) /var/lib/%{name}/.bash_profile
%attr(700,postgres,postgres) %dir /var/lib/%{name}/backups
%attr(700,postgres,postgres) %dir /var/lib/%{name}/data

%files devel -f 32bit/devel.lst
%defattr(-,root,system)
%{_bindir}/ecpg*
%{_bindir}/pg_config*

/usr/bin/ecpg*
/usr/bin/pg_config*

%{_includedir}/*

# add link (/bin/) to include files
/usr/include/*

## %{_libdir}/libecpg.so

## %{_libdir64}/libecpg.so
## %{_libdir}/libecpg_compat.so
## %{_libdir64}/libecpg_compat.so
## %{_libdir}/libpgtypes.so
## %{_libdir64}/libpgtypes.so
## %{_libdir}/libpq.so
## %{_libdir64}/libpq.so
%{_libdir}/%{name}/pgxs/
%{_libdir64}/%{name}/pgxs/
%{_libdir}/pkgconfig/*.pc
%{_libdir64}/pkgconfig/*.pc

%{_mandir}/man1/ecpg.*
%{_mandir}/man1/pg_config.*
%{_mandir}/man3/SPI_*



%if %plperl
%files plperl -f 32bit/plperl.lst
%defattr(-,root,system)
%{_datadir}/%{name}/extension/plperl*
%{_libdir}/%{name}/plperl.so
%{_libdir64}/%{name}/plperl.so

/usr/lib/%{name}
/usr/lib64/%{name}

%endif


%if %pltcl
%files pltcl -f  32bit/pltcl.lst
%defattr(-,root,system)

%{_bindir}/pltcl_delmod*
%{_bindir}/pltcl_listmod*
%{_bindir}/pltcl_loadmod*

/usr/bin/pltcl_delmod*
/usr/bin/pltcl_listmod*
/usr/bin/pltcl_loadmod*

%{_datadir}/%{name}/extension/pltcl*
%{_datadir}/%{name}/unknown.pltcl
%{_libdir}/%{name}/pltcl.so
%{_libdir64}/%{name}/pltcl.so

/usr/lib/%{name}
/usr/lib64/%{name}

%endif

%if %plpython
%files plpython -f 32bit/plpython.lst
%defattr(-,root,system)
%{_datadir}/%{name}/extension/plpython2*
%{_datadir}/%{name}/extension/plpythonu*
%{_libdir}/%{name}/plpython2.so
%{_libdir64}/%{name}/plpython2.so
/usr/lib/%{name}
/usr/lib64/%{name}
%endif


%if %test
%files test
%defattr(-,postgres,postgres)
%attr(-,postgres,postgres) %dir %{_libdir}/%{name}/test
%attr(-,postgres,postgres) %dir %{_libdir64}/%{name}/test
%attr(-,postgres,postgres) %{_libdir}/%{name}/test/*
%attr(-,postgres,postgres) %{_libdir64}/%{name}/test/*
%endif

%changelog
* Thu Nov 23 2017 Tony Reix <tony.reix@bull.net> - 9.6.6-1
- Updated to version 9.6.6 .
- Compiled with xlC v12.01.0000.0016
- Remove -bmaxdata:0x80000000 for 64bit.

* Fri Mar 17 2017 Tony Reix <tony.reix@bull.net> - 9.6.2-1
- Updated to version 9.6.2 .

* Tue Mar 07 2017 Jean Girardet <jean.girardet@atos.net> - 9.6.1-2
- Add  --enable-tap-tests to configire.

* Tue Jan 31 2017 Tony Reix <tony.reix@bull.net> - 9.6.1-1
- Updated to version 9.6.1 .

* Mon Jan 30 2017 Tony Reix <tony.reix@bull.net> - 9.6.0-7
- Add more tests

* Thu Jan 26 2017 Tony Reix <tony.reix@bull.net> - 9.6.0-6
- Use xlc 13.0.3 . Remove xlC which is unused

* Tue Jan 25 2017 Tony Reix <tony.reix@bull.net> - 9.6.0-5
- Add aix 7.2

* Tue Jan 24 2017 Tony Reix <tony.reix@bull.net> - 9.6.0-4
- Add strip -e
- /opt/freeware/bin/perl_64bit_NoXLC mess. Just adding comments for now.

* Mon Nov 14 2016 Jean Girardet <jean.girardet@atos.net> - 9.6.0-3
- Add postgresql/test/regress/libregress.a

* Wed Oct 26 2016 Jean Girardet <jean.girardet@atos.net> - 9.6.0-2
- Updated to version 9.6.0

* Wed Oct 26 2016 Tony Reix <tony.reix@bull.net> - 9.6.0-1
- Updated to version 9.6.0

* Thu Jul 24 2014 Michael Perzl <michael@perzl.org> - 9.3.5-1
- updated to version 9.3.5

* Mon Mar 24 2014 Michael Perzl <michael@perzl.org> - 9.3.4-1
- updated to version 9.3.4

* Mon Feb 24 2014 Michael Perzl <michael@perzl.org> - 9.3.3-1
- updated to version 9.3.3

* Mon Feb 24 2014 Michael Perzl <michael@perzl.org> - 9.3.2-1
- updated to version 9.3.2

* Mon Feb 24 2014 Michael Perzl <michael@perzl.org> - 9.3.1-1
- updated to version 9.3.1

* Mon Feb 24 2014 Michael Perzl <michael@perzl.org> - 9.3.0-1
- updated to version 9.3.0

* Mon Feb 17 2014 Michael Perzl <michael@perzl.org> - 9.2.6-2
- changed build process to individual AIX versions
- fixed the rc.d/init.d init script

* Fri Dec 06 2013 Michael Perzl <michael@perzl.org> - 9.2.6-1
- updated to version 9.2.6

* Mon Oct 14 2013 Michael Perzl <michael@perzl.org> - 9.2.5-1
- updated to version 9.2.5

* Thu Apr 04 2013 Michael Perzl <michael@perzl.org> - 9.2.4-1
- updated to version 9.2.4

* Thu Feb 07 2013 Michael Perzl <michael@perzl.org> - 9.2.3-1
- updated to version 9.2.3

* Wed Dec 12 2012 Michael Perzl <michael@perzl.org> - 9.2.2-1
- updated to version 9.2.2

* Fri Dec 07 2012 Michael Perzl <michael@perzl.org> - 9.2.1-2
- added missing 64-bit shared objects to archive libraries
- added symlinks to easily switch between the 32-bit and 64-bit version

* Tue Sep 25 2012 Michael Perzl <michael@perzl.org> - 9.2.1-1
- updated to version 9.2.1

* Tue Sep 25 2012 Michael Perzl <michael@perzl.org> - 9.2.0-1
- updated to version 9.2.0

* Mon Aug 27 2012 Michael Perzl <michael@perzl.org> - 9.1.5-1
- updated to version 9.1.5

* Thu Jun 28 2012 Michael Perzl <michael@perzl.org> - 9.1.4-1
- updated to version 9.1.4

* Thu Jun 28 2012 Michael Perzl <michael@perzl.org> - 9.1.3-1
- updated to version 9.1.3

* Thu Jun 28 2012 Michael Perzl <michael@perzl.org> - 9.1.2-1
- updated to version 9.1.2

* Thu Jun 28 2012 Michael Perzl <michael@perzl.org> - 9.1.1-1
- updated to version 9.1.1

* Thu Jun 28 2012 Michael Perzl <michael@perzl.org> - 9.1.0-1
- updated to version 9.1.0

* Thu Jun 28 2012 Michael Perzl <michael@perzl.org> - 9.0.8-1
- updated to version 9.0.8

* Thu Jun 28 2012 Michael Perzl <michael@perzl.org> - 9.0.7-1
- updated to version 9.0.7

* Thu Jun 28 2012 Michael Perzl <michael@perzl.org> - 9.0.6-1
- updated to version 9.0.6

* Fri Apr 20 2012 Michael Perzl <michael@perzl.org> - 9.0.5-1
- updated to version 9.0.5

* Wed Aug 25 2011 Michael Perzl <michael@perzl.org> - 9.0.4-1
- updated to version 9.0.4

* Wed Aug 25 2011 Michael Perzl <michael@perzl.org> - 9.0.3-1
- updated to version 9.0.3

* Wed Mar 16 2011 Michael Perzl <michael@perzl.org> - 8.4.7-1
- first version for AIX V5.1 and higher
