# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=pgexporter
pkgver=0.5.0
pkgrel=1
pkgdesc="Prometheus exporter for PostgreSQL"
url="https://pgexporter.github.io"
arch="all"
license="BSD-3-Clause"
makedepends="
	cmake
	cmd:rst2man
	libev-dev
	openssl-dev
	samurai
	yaml-dev
	"
pkgusers="pgexporter"
pkggroups="pgexporter"
install="$pkgname.pre-install"
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-openrc
	$pkgname-bash-completion
	$pkgname-zsh-completion
	"
source="https://github.com/pgexporter/pgexporter/releases/download/$pkgver/pgexporter-$pkgver.tar.gz
	fix-stdout-buffering.patch
	default-config.patch
	$pkgname.initd
	$pkgname.confd
	bin-wrapper.in
	"
options="!check"  # no tests provided

build() {
	local crossopts=
	if [ "$CBUILD" != "$CHOST" ]; then
		crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		$crossopts
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	install -D -m644 contrib/shell_comp/pgexporter_comp.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_$pkgname

	install -D -m644 contrib/shell_comp/pgexporter_comp.bash \
		"$pkgdir"/usr/share/bash-completion/completions/$pkgname

	cd "$pkgdir"

	# pgexporter-{admin,cli} cannot be run as root (and they should always be run
	# as the pgexporter user), so replace them on PATH with a wrapper that execs
	# the command with su(1) if run as root.
	mkdir -p usr/libexec
	local cmd; for cmd in pgexporter-admin pgexporter-cli; do
		mv usr/bin/$cmd usr/libexec/
		sed "s|^COMMAND=.*|COMMAND='/usr/libexec/$cmd'|" "$srcdir"/bin-wrapper.in \
			| install -m755 /dev/stdin usr/bin/$cmd
	done

	install -d -m755 -o pgexporter -g pgexporter etc/$pkgname
	install -d -m750 -o pgexporter -g pgexporter var/lib/$pkgname

	install -D -m644 "$builddir"/doc/etc/pgexporter.conf -t etc/pgexporter/

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

sha512sums="
234cdb8f724321a8bbfefcc2e9f665d43295a23bfaec97e0158aabfa112f77d2611b69bdbff3d3a1e6102d84df2fef61741b8c8836d30797d4c38b5bd05216dd  pgexporter-0.5.0.tar.gz
d5f0179e50a0eb8aa778ec5a487d9fd6db7c5782385e82b48cc644fd175c28c07942c03e185c6ab526b02c3a5a5778d12a32ea138055b4bae799e0f4ec164a79  fix-stdout-buffering.patch
fe9a787be2c20d691a9faec5e993ff374a1d2b9081669e54782ad02b702df3325cf1cfeb57a041fbeb5f7e260d506495f8d30be2f276f3976fc64fb599cbbe64  default-config.patch
acf0cde661cc11c1b5903d2ceb8bd88f155495c1d053ea28c14a026f2aba29e6fce90371bf829216375bc34a027b61ca4049724d292e1207901589254ef9786d  pgexporter.initd
5195f0a77d3e194cfb2c52aeea70ee327efc16ac55ae613b0b44e90241d2327dfc4056ee209972608063ae3634595af21b41bb1e5b901b1d48f26467e05ecc2d  pgexporter.confd
76d3786c01576291bfcb4e6d3ca39cce0946222138a7892ff3fe9069e3155c81f9e2e5e6323af3ba8c801b30be41e04a5eede81216e73d5833396fcc49d2fc97  bin-wrapper.in
"
