# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=agensgraph
pkgver=2.13.1
pkgrel=4
pkgdesc="A transactional graph database based on PostgreSQL"
url="https://bitnine.net/agensgraph/"
# armhf, armv7, s390x, x86: tests fail, not supported by upstream
# riscv64: no tested yet
arch="all !armhf !armv7 !riscv64 !s390x !x86"
license="PostgreSQL AND Apache-2.0"
depends="tzdata"
depends_dev="
	icu-dev
	openssl-dev
	"
makedepends="$depends_dev
	bison
	cmd:xmllint
	cmd:xsltproc
	docbook-xml
	docbook-xsl
	flex
	linux-headers
	perl-dev
	python3-dev
	readline-dev
	tcl-dev
	util-linux-dev
	zlib-dev
	"
checkdepends="diffutils"
pkgusers="postgres"
pkggroups="postgres"
install="$pkgname.pre-install $pkgname.post-install"
sonameprefix="$pkgname:so:"
subpackages="
	$pkgname-contrib
	$pkgname-plperl
	$pkgname-plperl-contrib:plperl_contrib
	$pkgname-plpython3
	$pkgname-plpython3-contrib:plpython3_contrib
	$pkgname-pltcl
	$pkgname-dev
	$pkgname-openrc
	"
source="https://github.com/bitnine-oss/agensgraph/archive/v$pkgver/agensgraph-$pkgver.tar.gz
	initdb.patch
	per-version-dirs.patch
	disable-broken-tests.patch
	disable-test-collate.icu.utf8.patch
	disable-html-docs.patch
	remove-libecpg_compat.patch
	pg_config-add-major-version.patch
	dont-use-locale-a-on-musl.patch
	icu-collations-hack.patch
	libpgport-pkglibdir.patch
	fix-test-plperl-5.8-pragma.patch

	agensgraph-unix_socket_directories.patch
	agensgraph-dirs.patch
	agensgraph-default-port.patch
	agensgraph-exclude-broken-extensions.patch

	$pkgname.initd
	$pkgname.confd
	"

_bindir=usr/libexec/$pkgname
_datadir=usr/share/$pkgname
_includedir=usr/include/$pkgname
_libdir=usr/lib/$pkgname

prepare() {
	default_prepare
	update_config_sub
}

build() {
	export PYTHON=/usr/bin/python3
	export CFLAGS="${CFLAGS/-Os/-O2}"
	export CPPFLAGS="${CPPFLAGS/-Os/-O2}"

	# without-libxml - XML tests fail (static memory buffer doesn't support encoding)
	# without-llvm - fails to build with LLVM 14
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--bindir=/$_bindir \
		--datarootdir=/usr/share \
		--datadir=/$_datadir \
		--docdir=/.DELETE \
		--includedir=/$_includedir \
		--libdir=/$_libdir \
		--mandir=/.DELETE \
		--sysconfdir=/etc/$pkgname \
		--enable-rpath \
		--with-system-tzdata=/usr/share/zoneinfo \
		--without-libxml \
		--with-openssl \
		--with-uuid=e2fs \
		--without-llvm \
		--with-icu \
		--with-perl \
		--with-python \
		--with-tcl \
		--without-ldap \
		--with-pgport=5434
	make world
}

check() {
	_run_tests src/test
	_run_tests src/pl
	_run_tests contrib
}

package() {
	make DESTDIR="$pkgdir" install

	cd "$pkgdir"

	rm -rf ./.DELETE

	# Duplicate of usr/bin/ecpg.
	rm -f ./$_bindir/ecpg

	rm ./$_bindir/psql
	ln -s agens ./$_bindir/psql

	mkdir -p ./usr/bin
	ln -s /$_bindir/ag_ctl ./usr/bin/
	ln -s /$_bindir/agens ./usr/bin/
	ln -s /$_bindir/postgres ./usr/bin/agens-server

	install -d -m750 -o postgres -g postgres \
		./etc/$pkgname \
		./var/lib/$pkgname \
		./var/log/$pkgname

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

contrib() {
	pkgdesc="Extension modules distributed with AgensGraph"
	depends="$pkgname=$pkgver-r$pkgrel"

	cd "$builddir"

	# Avoid installing plperl and plpython extensions, these will be
	# installed into separate subpackages.
	sed -Ei -e 's/(.*_plperl)/#\1/' \
		-e 's/(.*_plpython)/#\1/' \
		contrib/Makefile

	make -C contrib DESTDIR="$subpkgdir" install

	_contrib_common
}

pltcl() {
	pkgdesc="PL/Tcl procedural language for AgensGraph"
	depends="$pkgname=$pkgver-r$pkgrel pgtcl"

	amove $_libdir/pltcl.so
	amove $_datadir/extension/pltcl*
}

plperl() {
	pkgdesc="PL/Perl procedural language for AgensGraph"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_libdir/plperl.so
	amove $_datadir/extension/plperl*
}

plperl_contrib() {
	_plcontrib plperl "PL/Perl"

	cd "$builddir"
	make -C contrib/hstore_plperl DESTDIR="$subpkgdir" install

	_contrib_common
}

plpython3() {
	pkgdesc="PL/Python3 procedural language for AgensGraph"
	depends="$pkgname=$pkgver-r$pkgrel python3"

	amove $_libdir/plpython3.so
	amove $_datadir/extension/plpython*
}

plpython3_contrib() {
	_plcontrib plpython3 "PL/Python 3"

	cd "$builddir"
	make -C contrib/hstore_plpython DESTDIR="$subpkgdir" install
	make -C contrib/ltree_plpython DESTDIR="$subpkgdir" install

	cd "$subpkgdir"/$_datadir/extension/
	rm ./*plpython2* ./*plpythonu*

	_contrib_common
}

dev() {
	default_dev

	amove $_libdir/pgxs/*
}

_plcontrib() {
	local subname="$1"
	pkgdesc="$2 extension modules distributed with AgensGraph"
	depends="$pkgname-$subname=$pkgver-r$pkgrel"
	install_if="$pkgname-$subname=$pkgver-r$pkgrel $pkgname-contrib=$pkgver-r$pkgrel"
}

_contrib_common() {
	# Move headers and bitcode from subpackage back to pkgdir, so it
	# can be catched by subsequent split functions.
	local dir; for dir in $_includedir $_libdir/bitcode; do
		[ -d "$subpkgdir"/$dir ] || continue

		mkdir -p "$pkgdir"/$dir
		cp -rf "$subpkgdir"/$dir/* "$pkgdir"/$dir/
		rm -rf "$subpkgdir"/$dir/*
		rmdir -p "$subpkgdir"/$dir || true
	done
	rm -rf "$subpkgdir"/.DELETE
}

_run_tests() {
	local path="$1"; shift

	msg "Running test suite at $path..."
	# Note: some tests fail when running in parallel.
	make -k -j 1 -C "$path" "$@" check MAX_CONNECTIONS=5 || {
		printf "\n%s\n\n" "Trying to find all regression.diffs files in build directory..." >&2
		find "$path" -name regression.diffs | while read -r file; do
			echo "=== test failure: $file ===" >&2
			cat "$file" >&2
		done
		return 1
	}
}

sha512sums="
b1e55bc048971cafb3fee7bfa3d5aa98db69fcf4a1412b3a103be6cddd05e7448e645be5de42ea0bf19ac1d8f660891e14066e9673af5b786305c77166ac25dd  agensgraph-2.13.1.tar.gz
1f8e7dc58f5b0a12427cf2fd904ffa898a34f23f3332c8382b94e0d991c007289e7913a69e04498f3d93fc5701855796c207b4b1cc4a0b366f586050124d7fcc  initdb.patch
92d28f91b57a01afc81a00d386e87b9e2aa812f341a1578f262af372ee905530205592eda595111827582acceacd1bf244d1df130330003f05e74e4ddb5a7c4f  per-version-dirs.patch
c4179fcd8b71791cdc41ea7b622cf82e9bd42ac1de66999234b98a83c0c508c79c492a9301274fe859c06a3f1a8b17b53ab97541ab76801a985f8f0b9b8716e7  disable-broken-tests.patch
14703da0a9441ae1bbad9fe124c4c267526975a22080c5f3e8c3a323164b743158ee10fcff31f18131a64e765c89125410652b317da46e92e962f251079f5a9a  disable-test-collate.icu.utf8.patch
2e33e1ae38d60e0daf3ed18e6eaa9ddf6762b4b1bb7e51f1d2690e3df9d602aa1a700e603ba3ee69314a75a963131c7dc67c1b1f8b7eb5564e9c4253e81a4db4  disable-html-docs.patch
d8eb4274a54b94bed4a2ded7ae775c5a95ca0f051b831b859ccf78bf6d2ea6fe89a9a0611771f6ad85573995a7e3af1fdf5859e20cae3267a52239f12e1b61c3  remove-libecpg_compat.patch
7790e4e4374f7bdc6b4484ba87a5fa709d30d3cbdce61ee7bf9c5dfce40cb51c7bd54ab42f4050fb48eede08ef573624d819128e57cc8c976e01202854740308  pg_config-add-major-version.patch
b0688d66fdd7d612c24d9aa69bdd80d30787d2d6409b4524c79b41797144fc743213460e6de9c536bfb72da089f92cf89731f15137b1407fd04ca97fd393bfd2  dont-use-locale-a-on-musl.patch
0ebee9cf40a7e84e8eda3998c0dd0f7601d9288233411f45f9710fcafc5f4ca3e4a91e8e1b552cc4f4d2c0e8fa6512703b1094e501f132ea2842db0398e62934  icu-collations-hack.patch
f8ed2b7b96fd22cd87c982151e659d82bcae10033a97f403f7847fce6daa8fc580e998cfb3813af9cb59a12f0c6bcc276397c28b1fc48321eed8c7ba5f3f92ed  libpgport-pkglibdir.patch
48bda2484c505a9537bf442703f1fc466972395584756f175766936d9b3f6afb4999913c49c6040d5318c74e02e5dfdcf2ff8ff8bab15759c58a84aea0edc871  fix-test-plperl-5.8-pragma.patch
c453466d45a3de66bd96954008f640cfddc8d79723be834a191ae54c882600a57c71b9c6df48aa1d8aacfae0c31128e46061d0dd81e03ae977f8fb903e074dfe  agensgraph-unix_socket_directories.patch
de9ef8745d766d634676b7bf6cfa657e88c594decde918ddb8195b2834dc316cb2c6ae0251dbaa50d399934831445c42e2f26ae0c1aef42b9dd992ed93203afa  agensgraph-dirs.patch
12557c47f63af317f707b5e58342e5a4f3e40f76b297a4f76fe395a6b0c3145cb3707b3cabd1aef864d3b6a2da65cd811e13f5f710e1f7768141e4d2ab771961  agensgraph-default-port.patch
91ea5036ecac747c2a0b67f2592fdd9e2f7f05751e9fda3cd9f6a6925758d235c759ea832be041e753f4677b6967e26ee8e57ca195ac3dcfda6095623c1de51a  agensgraph-exclude-broken-extensions.patch
8c196b2f5e3a118410137d9d0d7a021699c39a6e2544275a493b962141804a4d9c3aeaa80ec993704729c97699ccb1def6ced28979b5841a7da47f1ad5cce957  agensgraph.initd
bba23fceaa515c18c20ebeacf255f22205129d32aaacc851f14a82aba441d0b1b3faae8d19e61b11bf9de70ba9b9a28e1d705644676203037f7ed9fde7383482  agensgraph.confd
"
