SUMMARY="A powerful object-relational database system"
DESCRIPTION="PostgreSQL has more than 15 years of active development and a \
proven architecture that has earned it a strong reputation for reliability, \
data integrity, and correctness.

* It runs on all major operating systems, including Linux, UNIX (AIX, BSD, \
HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows.
* It is fully ACID compliant, has full support for foreign keys, joins, \
views, triggers, and stored procedures (in multiple languages).
* It includes most SQL:2008 data types, including INTEGER, NUMERIC, BOOLEAN, \
CHAR, VARCHAR, DATE, INTERVAL, and TIMESTAMP.
* It supports storage of binary large objects, including pictures, sounds, or \
video.
* It has native programming interfaces for C/C++, Java, .Net, Perl, Python, \
Ruby, Tcl, ODBC, among others, and exceptional documentation."
HOMEPAGE="https://www.postgresql.org/"
COPYRIGHT="1996-2018 PostgreSQL Global Development Group"
LICENSE="PostgreSQL"
REVISION="4"
SOURCE_URI="http://ftp.postgresql.org/pub/source/v$portVersion/postgresql-$portVersion.tar.bz2"
CHECKSUM_SHA256="90815e812874831e9a4bf6e1136bf73bc2c5a0464ef142e2dfea40cda206db08"
SOURCE_DIR="postgresql-$portVersion"
PATCHES="postgresql11-$portVersion.patchset"

ARCHITECTURES="all !x86_gcc2 ?x86"
SECONDARY_ARCHITECTURES="x86"

portVersionCompat="$portVersion compat >= ${portVersion%.*}"

PROVIDES="
	postgresql11$secondaryArchSuffix = $portVersionCompat
	cmd:psql$secondaryArchSuffix = $portVersionCompat
	lib:libecpg$secondaryArchSuffix = $portVersionCompat
	lib:libecpg_compat$secondaryArchSuffix = $portVersionCompat
	lib:libpgtypes$secondaryArchSuffix = $portVersionCompat
	lib:libpq$secondaryArchSuffix = $portVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libicudata$secondaryArchSuffix
	lib:libicui18n$secondaryArchSuffix
	lib:libicuuc$secondaryArchSuffix
	lib:libldap_r_2.4$secondaryArchSuffix
	lib:libpam$secondaryArchSuffix
	lib:libreadline$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libxslt$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_server="
	postgresql11${secondaryArchSuffix}_server = $portVersionCompat
	cmd:clusterdb$secondaryArchSuffix = $portVersionCompat
	cmd:createdb$secondaryArchSuffix = $portVersionCompat
	cmd:createlang$secondaryArchSuffix = $portVersionCompat
	cmd:createuser$secondaryArchSuffix = $portVersionCompat
	cmd:dropdb$secondaryArchSuffix = $portVersionCompat
	cmd:droplang$secondaryArchSuffix = $portVersionCompat
	cmd:dropuser$secondaryArchSuffix = $portVersionCompat
	cmd:ecpg$secondaryArchSuffix = $portVersionCompat
	cmd:initdb$secondaryArchSuffix = $portVersionCompat
	cmd:pg_archivecleanup$secondaryArchSuffix = $portVersionCompat
	cmd:pg_basebackup$secondaryArchSuffix = $portVersionCompat
	cmd:pg_config$secondaryArchSuffix = $portVersionCompat
	cmd:pg_controldata$secondaryArchSuffix = $portVersionCompat
	cmd:pg_ctl$secondaryArchSuffix = $portVersionCompat
	cmd:pg_dump$secondaryArchSuffix = $portVersionCompat
	cmd:pg_dumpall$secondaryArchSuffix = $portVersionCompat
	cmd:pg_isready$secondaryArchSuffix = $portVersionCompat
	cmd:pg_receivewal$secondaryArchSuffix = $portVersionCompat
	cmd:pg_receivexlog$secondaryArchSuffix = $portVersionCompat
	cmd:pg_recvlogical$secondaryArchSuffix = $portVersionCompat
	cmd:pg_resetwal$secondaryArchSuffix = $portVersionCompat
	cmd:pg_resetxlog$secondaryArchSuffix = $portVersionCompat
	cmd:pg_restore$secondaryArchSuffix = $portVersionCompat
	cmd:pg_rewind$secondaryArchSuffix = $portVersionCompat
	cmd:pg_test_fsync$secondaryArchSuffix = $portVersionCompat
	cmd:pg_test_timing$secondaryArchSuffix = $portVersionCompat
	cmd:pg_upgrade$secondaryArchSuffix = $portVersionCompat
	cmd:pg_verify_checksums$secondaryArchSuffix = $portVersionCompat
	cmd:pg_waldump$secondaryArchSuffix = $portVersionCompat
	cmd:pg_xlogdump$secondaryArchSuffix = $portVersionCompat
	cmd:pgbench$secondaryArchSuffix = $portVersionCompat
	cmd:postgres$secondaryArchSuffix = $portVersionCompat
	cmd:postmaster$secondaryArchSuffix = $portVersionCompat
	cmd:reindexdb$secondaryArchSuffix = $portVersionCompat
	cmd:vacuumdb$secondaryArchSuffix = $portVersionCompat
	"
REQUIRES_server="
	haiku$secondaryArchSuffix
	postgresql11${secondaryArchSuffix} == $portVersion base
	lib:libcrypto$secondaryArchSuffix
	lib:libicudata$secondaryArchSuffix
	lib:libicui18n$secondaryArchSuffix
	lib:libicuuc$secondaryArchSuffix
	lib:libldap_r_2.4$secondaryArchSuffix
	lib:libpam$secondaryArchSuffix
	lib:libreadline$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libxslt$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	postgresql11${secondaryArchSuffix}_devel = $portVersion
	devel:libpgport$secondaryArchSuffix = $portVersion
	devel:libpgcommon$secondaryArchSuffix = $portVersion
	devel:libecpg$secondaryArchSuffix = $portVersion
	devel:libpgfeutils$secondaryArchSuffix = $portVersion
	devel:libpgtypes$secondaryArchSuffix = $portVersion
	devel:libpq$secondaryArchSuffix = $portVersion
	devel:libecpg_compat$secondaryArchSuffix = $portVersion
	"
REQUIRES_devel="
	postgresql11${secondaryArchSuffix} == $portVersion base
	"

CONFLICTS_server="
	postgresql
	postgresql$secondaryArchSuffix
"

BUILD_REQUIRES="
	devel:libcrypto$secondaryArchSuffix
	devel:libicudata$secondaryArchSuffix
	devel:libicui18n$secondaryArchSuffix
	devel:libicuuc$secondaryArchSuffix
	devel:libldap$secondaryArchSuffix
	devel:libpam$secondaryArchSuffix
	devel:libreadline$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	devel:libxslt$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:aclocal
	cmd:autoconf
	cmd:automake
	cmd:autoheader
	cmd:awk
	cmd:bison
	cmd:flex
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:libtoolize$secondaryArchSuffix
	cmd:make
	cmd:m4
	cmd:pkg_config$secondaryArchSuffix
	cmd:touch
	"

BUILD()
{
	libtoolize --force --copy --install
	autoreconf -i

	touch src/template/haiku # AFAIK it should be empty
	touch config/missing # shouldn't be required

	CFLAGS=-D_BSD_SOURCE runConfigure ./configure \
		--with-icu \
		--with-ldap \
		--with-libxml \
		--with-libxslt \
		--with-openssl \
		--with-pam \
		--with-template=haiku
	make $jobArgs
}

INSTALL()
{
	make install

	prepareInstalledDevelLibs libpgport \
		libpgcommon \
		libecpg \
		libpgtypes \
		libpq \
		libecpg_compat \
		libpgfeutils
	fixPkgconfig

	mkdir -p $developLibDir/postgresql/pgxs
	mv $libDir/postgresql/pgxs $developLibDir/postgresql/

	# we want to keep the psql command in the base package
	mv $binDir/psql psql_tmp

	# all other commands go in the server package
	packageEntries server \
		$binDir \
		$dataDir/postgresql/ \
		$libDir/postgresql

	mkdir -p $binDir
	mv psql_tmp $binDir/psql

	packageEntries devel \
		$developDir
}

TEST()
{
	LIBRARY_PATH="$sourceDir/tmp_install/packages/postgresql-$portVersion-$REVISION/.self/lib${LIBRARY_PATH:+:$LIBRARY_PATH}"
	make check
}
