# Contributor: Kurt Marasco <celilo@lavabit.com>
# Contributor: Pascal Ernster <aur at hardfalcon dot net>
# Contributor: Stuart Cardall <developer at it-offshore dot co.uk>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=hiawatha
pkgver=11.6
pkgrel=1
_php=php81
pkgdesc="Secure and advanced webserver"
url="https://hiawatha.leisink.net/"
arch="all"
license="GPL-2.0-only"
options="suid !check"
subpackages="$pkgname-doc $pkgname-openrc $pkgname-letsencrypt::noarch"
makedepends="cmake libxml2-dev libxslt-dev mbedtls-dev samurai"
source="https://hiawatha.leisink.net/files/hiawatha-$pkgver.tar.gz
	hiawatha.initd
	hiawatha.conf.sample
	letsencrypt.helper
	"

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 \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=None \
		-DUSE_SYSTEM_MBEDTLS=ON \
		$CMAKE_CROSSOPTS .
	cmake --build build
}

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

	sed -i 's|#ServerId = www-data|ServerId = nobody|' \
		"$pkgdir"/etc/hiawatha/hiawatha.conf
	sed -i 's|www-data|nobody|g' build/logrotate.d/hiawatha
	install -Dm644 build/logrotate.d/hiawatha \
		"$pkgdir"/etc/logrotate.d/hiawatha
	install -Dm755 "$srcdir"/hiawatha.initd \
		"$pkgdir"/etc/init.d/hiawatha
	install -Dm644 "$srcdir"/hiawatha.conf.sample \
		"$pkgdir"/usr/share/doc/hiawatha/hiawatha.conf.sample
}

letsencrypt() {
	depends="$_php $_php-openssl"
	pkgdesc="PHP scripts to generate SSL certificates with letsencrypt"

	mkdir -p "$subpkgdir"/usr/sbin "$subpkgdir"/usr/share/man/man1 "$subpkgdir"/usr/lib/hiawatha
	mv "$pkgdir"-doc/usr/share/man/man1/lefh.1.gz "$subpkgdir"/usr/share/man/man1/
	mv "$pkgdir"/usr/lib/hiawatha/letsencrypt "$subpkgdir"/usr/lib/hiawatha
	rmdir -p "$pkgdir"/usr/lib/hiawatha || true
	mv "$pkgdir"/usr/sbin/lefh "$subpkgdir"/usr/sbin/
	mkdir -p "$subpkgdir"/usr/lib/hiawatha/letsencrypt/scripts
	install -Dm750 "$builddir"/extra/letsencrypt/scripts/* "$subpkgdir"/usr/lib/hiawatha/letsencrypt/scripts/
	install -Dm750 "$srcdir"/letsencrypt.helper "$subpkgdir"/usr/sbin/letsencrypt
}

sha512sums="
a8d9f53d07e4bfb09e761db5ab03f2b83dc2399c7f2ba75d256737c428c7daae6208bb8f472b23feabb940130cfd4d40ae577716a88a514bab7690abee5cb2a1  hiawatha-11.6.tar.gz
cec0adfb48e1751cf13031f2c28f4590340b2c747aa8e81d18c16bc8789bce69b2d9d99290a93b74b501db3b63190a071033a78abeeb8f60becba2dcaf3b5c40  hiawatha.initd
771bed17b5831bb9d4a28eb94482dd39de8c4b5b35b2c08e1c303a2778254f9e34e40a8d791cc7ef4e73740f474c502685252378e7fe2cba494c782c11f960d4  hiawatha.conf.sample
dbdd0cdf975b226d2ac27257861a1cc6c1f77c8387d632014368a1c835817a33b780f446cd3fdb7d9ca4322eaede1fea28652dde70928cc8a73f0b05dba46f6e  letsencrypt.helper
"
