# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
pkgname=postgresql-bdr
pkgver=9.4.14_p1
_pkgver=${pkgver/_p/_bdr}
pkgrel=15
pkgdesc="A sophisticated object-relational DBMS with bidirectional replication support (BDR)"
url="https://www.enterprisedb.com/products/bidirectional-replication-bdr-postgresql-database"
arch="all"
license="PostgreSQL"
depends="bash"
install="$pkgname.pre-install $pkgname.pre-upgrade"
depends_dev="readline-dev openssl-dev>3 zlib-dev libxml2-dev"
makedepends="$depends_dev perl bison flex-dev libpq autoconf automake libtool"
subpackages="$pkgname-dev $pkgname-contrib $pkgname-client $pkgname-openrc
	$pkgname-libs"
provides="postgresql=$pkgver-r$pkgrel"
# Avoid conflicts in provided so:* names with postgresql package.
# sonameprefix="$pkgname:"
source="https://distfiles.alpinelinux.org/distfiles/v3.15/postgresql-bdr-$_pkgver.tar.bz2
	initdb.patch
	postgresql-bdr-osxflags.patch
	autoconf-gcc14.patch
	$pkgname.initd
	$pkgname.confd
	pgbdr-restore.initd
	pgbdr-restore.confd
	"
builddir="$srcdir/$pkgname-$_pkgver"

prepare() {
	default_prepare

	autoreconf -vi
	update_config_sub
	update_config_guess

	# sanity check of conf.d
	(
		. "$srcdir"/$pkgname.confd
		_datadir=/var/lib/postgresql/${pkgver%.*}/data
		if [ "$_datadir" != "$PGDATA" ]; then
			die "PGDATA is $PGDATA while $_datadir is expected"
		fi
	)
	#rm doc/src/Makefile
}

build() {
	# native spinlocks not supported on riscv64 and loongarch64currently
	local _configure=""
	case $CARCH in
	riscv64|loongarch64) _configure=--disable-spinlocks ;;
	esac

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--with-openssl \
		$_configure

	# configure/programs.m4 doesn't properly detect flex
	# and offers manual user override
	#make FLEX="/usr/bin/flex" JADEFLAGS="-E 999999" OSXFLAGS="-E 999999" \
	 #world
	# Dont' build doc since it throws tons of openjade/osx errors
	make FLEX="/usr/bin/flex" all
}

check() {
	make check
}

package() {
	# Don't install docs
	make DESTDIR="$pkgdir" install

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -D -m755 "$srcdir"/pgbdr-restore.initd \
		"$pkgdir"/etc/init.d/pgbdr-restore
	install -D -m644 "$srcdir"/pgbdr-restore.confd \
		"$pkgdir"/etc/conf.d/pgbdr-restore
	# conflicts with libpq mainline
	rm "$pkgdir"/usr/lib/libpq.so*
}

client() {
	pkgdesc="PostgreSQL Client Utilities"
	provides="postgresql-client=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/usr/lib "$subpkgdir"/usr/bin
	# mv "$pkgdir"/usr/lib/libpq.so* "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/bin/clusterdb \
		"$pkgdir"/usr/bin/createdb \
		"$pkgdir"/usr/bin/createuser \
		"$pkgdir"/usr/bin/dropdb \
		"$pkgdir"/usr/bin/dropuser \
		"$pkgdir"/usr/bin/pg_basebackup \
		"$pkgdir"/usr/bin/pg_dump \
		"$pkgdir"/usr/bin/pg_dumpall \
		"$pkgdir"/usr/bin/pg_isready \
		"$pkgdir"/usr/bin/pg_recvlogical \
		"$pkgdir"/usr/bin/pg_restore \
		"$pkgdir"/usr/bin/psql \
		"$pkgdir"/usr/bin/reindexdb \
		"$pkgdir"/usr/bin/vacuumdb \
		"$subpkgdir"/usr/bin
}

contrib() {
	pkgdesc="Extension modules distributed with PostgreSQL"
	provides="postgresql-contrib=$pkgver-r$pkgrel"

	cd "$builddir"
	make DESTDIR="$subpkgdir" FLEX="/usr/bin/flex" -C contrib install
}

dev() {
	provides=""
	default_dev

	_submv usr/bin/pg_config \
		usr/bin/ecpg \
		usr/lib/postgresql/pgxs
}

libs() {
	depends=""
	provides="postgresql-libs=$pkgver-r$pkgrel"
	default_libs
}

_submv() {
	local path; for path in "$@"; do
		mkdir -p "$subpkgdir/${path%/*}"
		mv "$pkgdir"/$path "$subpkgdir"/${path%/*}/
	done
}

sha512sums="
cd8fec782c201270270d4abfa59296083889bf22f63b4e18f758e8262ee52deeae247d813a3e8633861819d803cb369157854de446affdde65c1e12477bbc0b8  postgresql-bdr-9.4.14_bdr1.tar.bz2
6f7fdceb1a5aba3f00894ae88afdf25e5f9c7a0ffc00b11211011a6415ea73639d6a67f516cab66c6487484bab26ded3c93471d6d41ac0d3822926c22274f2ba  initdb.patch
1973863bfc208ce20ca2dd82557ebbd4fcddd1219d6a80f2e83550d0110d39f034cb829cca3ee9208f513a24b032267b94559c740ddddb10a4f24c630194eb5e  postgresql-bdr-osxflags.patch
d4fa605fcd55d3ed43f82d91fd57ef40fe1f56f9e8795e918ea7ef0422af59861ebf12ed9596bcea680c545fc9a4f81a8c30dedf8f3f409e97eb067f34603466  autoconf-gcc14.patch
7aa4e5746f6b9b5f6a1b56c0f18f9506a576fafa7037727d94591948394e7dc158c438927b8359127913c88c1a9ffa9ddb4e89353c4e98c47903a27311b6f502  postgresql-bdr.initd
e906e23241bed2624719c1e7e5305695cce1cb520f26f6a4c6bbb994f59db3cc4c63afcfe0e7fb705ad4691dd0a911770012fc16819e4c2686c7029e1e4a4a45  postgresql-bdr.confd
a20f4b2f7f6e2500b6f2b649d8e27a59d82f07768d7a1ee503cea3a88d21d86344f3b7e89ea10f9ce8509b44d71dbee1c9d8ca60ec0c968f36867270120c913f  pgbdr-restore.initd
c14a5684e914abb3b0ee71bbf15eed71a9264deacaa404a6e3af6bfc330d93e7598624d0ed11a94263106cc660f7f54c8ff57e759033cf606a795f69ff6c1c7c  pgbdr-restore.confd
"
