# Contributor: Alexandru Campeanu <tiotags1@gmail.com>
# Maintainer: Alexandru Campeanu <tiotags1@gmail.com>
pkgname=hinsightd
pkgver=0.9.20
pkgrel=0
pkgdesc="hinsightd a http/1.1 webserver with (hopefully) minimal goals"
url="https://tiotags.gitlab.io/hinsightd/"
arch="all"
license="BSD-3-Clause"
makedepends="cmake libcap-utils lua-dev openssl-dev>3 zlib-dev liburing-dev linux-headers samurai"
pkgusers="$pkgname"
pkggroups="$pkgname"
subpackages="$pkgname-dbg $pkgname-openrc"
source="
	https://gitlab.com/tiotags/hin9/-/archive/v$pkgver/hin9-v$pkgver.tar.gz
	fix-statx-redefinition.patch
	"
builddir="$srcdir"/hin9-v$pkgver
# no test suite
# setcap cap_net_bind
options="!check setcap"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=Release \
		-DUSE_FCGI=1 \
		-DUSE_OPENSSL=1 \
		-DUSE_RPROXY=1 \
		$CMAKE_CROSSOPTS
	cmake --build build
}

package() {
	install -D -m755 "$builddir"/build/hinsightd \
		"$pkgdir"/usr/sbin/hinsightd
	setcap cap_net_bind_service+eip "$pkgdir"/usr/sbin/hinsightd
	install -D -m755 "$builddir"/build/hinsightd_pid_helper \
		"$pkgdir"/usr/bin/hinsightd_pid_helper

	# create dirs
	install -d -m2750 -o $pkgusers -g $pkggroups \
		"$pkgdir"/var/log/hinsightd/
	install -d -m755 \
		"$pkgdir"/etc/hinsightd/ \
		"$pkgdir"/etc/hinsightd/config/ \
		"$pkgdir"/var/www/localhost/htdocs

	install -m755 -D "$builddir"/external/packaging/$pkgname.initd.sh \
		"$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$builddir"/external/packaging/$pkgname.confd.sh \
		"$pkgdir"/etc/conf.d/$pkgname
	install -m644 -D "$builddir"/external/packaging/$pkgname.logrotate.sh \
		"$pkgdir"/etc/logrotate.d/$pkgname

	# config files
	local i; for i in main.lua lib.lua; do
		install -m644 "$builddir"/workdir/"$i" "$pkgdir"/etc/$pkgname/"$i"
	done
	install -m644 -D "$builddir"/workdir/config/* -t "$pkgdir"/etc/$pkgname/config/

}

sha512sums="
7a4d7d7824b787156e7c87d76e97c2fb5129eb50ecf5ea33a7d1ea2e76718ef81a44f49d3161d2cc131ad6d0f27e61b480a2538651d428c19565d2a58cedc25b  hin9-v0.9.20.tar.gz
fe6645d0df91011076dc9dea323ae7824d58d9e3dcf1bbd5599151fe53e3b8b7f05d2e993c06f6c3728eb2e7fb5641f150abd4b41b8b8bd4c09b96e3a5614bb8  fix-statx-redefinition.patch
"
