# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=postgresql-pgmq
pkgver=1.1.1
pkgrel=1
pkgdesc="A lightweight message queue, like AWS SQS and RSMQ, but on PostgreSQL"
url="https://github.com/tembo-io/pgmq"
# loongarch64: blocked by libc crate
arch="all !loongarch64"
license="PostgreSQL"
makedepends="
	cargo
	chrpath
	clang16-libclang
	jq
	openssl-dev
	postgresql
	postgresql-dev
	rustfmt
	"
source="https://github.com/tembo-io/pgmq/archive/v$pkgver/postgresql-pgmq-$pkgver.tar.gz
	bump-pgrx.patch
	"
builddir="$srcdir/pgmq-$pkgver"
# check: tests tries to write to /usr/lib/...
# net: fetches crates in build phase
options="!check net"

export PGRX_HOME="$srcdir/.pgrx"

prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked
}

build() {
	local pgver="$(pg_config --major-version)"

	# We must install it this way because cargo-pgrx and pgrx dependency
	# versions in the project must be identical. :(
	local pgrx_ver; pgrx_ver="$(cargo metadata --locked --format-version 1 \
		| jq -r '.packages[] | select(.name == "pgrx") | .version')"
	# Use dev profile to speed-up compilation.
	cargo install --locked --profile dev --bins --root target/tools "cargo-pgrx@$pgrx_ver"

	export PATH="$PWD/target/tools/bin:$PATH"

	cargo pgrx init --pg$pgver="$(which pg_config)"

	# TODO: Remove after https://github.com/pgcentralfoundation/pgrx/issues/1455 is resolved.
	[ -n "$RUSTFLAGS" ] && export RUSTFLAGS="$(echo $RUSTFLAGS)" || unset RUSTFLAGS
	cargo pgrx package --verbose --out-dir="target/release/dist"

	# Remove RPATH.
	# NOTE: CARGO_PROFILE_RELEASE_RPATH=false doesn't work here.
	chrpath -d target/release/dist/usr/lib/postgresql*/*.so
}

package() {
	depends="postgresql$(pg_config --major-version)"

	mkdir -p "$pkgdir"
	cp -r target/release/dist/* "$pkgdir"/
}

sha512sums="
75e9472f0b5ef3d3b29f0ff04571acae9fa00a1210e49b3cd36710099607f9b4827d34764424f42cbb169351b88bb4d04afba53993e8c8862e83755d1ab1d304  postgresql-pgmq-1.1.1.tar.gz
ce5161c08645cad6f5ef2623786654e7916eb14190b453affeb10c940aff7b98178a1b4d7ac99b2cff22fe589ced108f3261acc0fe8a9da160f18a7128b20775  bump-pgrx.patch
"
