# Contributor: André Klitzing <aklitzing@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Andy Postnikov <apostnikov@gmail.com>
pkgname=unit
pkgver=1.34.2
pkgrel=4
pkgdesc="NGINX Unit is a dynamic web application server"
url="https://unit.nginx.org/"
arch="all"
license="Apache-2.0"
_phpver2=82
_phpver3=83
_phpver4=84
makedepends="
	linux-headers
	njs-dev
	openssl-dev>3
	perl-dev
	php$_phpver2-dev
	php$_phpver2-embed
	php$_phpver3-dev
	php$_phpver3-embed
	php$_phpver4-dev
	php$_phpver4-embed
	python3-dev
	ruby-dev
	"
checkdepends="procps-ng
	py3-pytest py3-openssl
	php$_phpver2-opcache php$_phpver3-opcache php$_phpver4-opcache
	"
options="net" # ruby tests require rack
pkgusers="$pkgname"
pkggroups="$pkgname"
install="$pkgname.pre-install"
subpackages="$pkgname-dev $pkgname-openrc $pkgname-tools::noarch $pkgname-doc"
#source="https://sources.nginx.org/unit/unit-$pkgver.tar.gz
source="$pkgname-$pkgver.tar.gz::https://github.com/nginx/unit/archive/refs/tags/$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotate
	phpver.patch
	fix-ruby-3.4.patch
	fix-ssl-shutdown.patch
	njs-0.9.0-compat.patch
	"

for _mod in perl php$_phpver2 php$_phpver3 php$_phpver4 python3 ruby; do
	subpackages="$subpackages $pkgname-$_mod:_module"
done

_modules_dir="/usr/lib/unit/modules"

build() {
	./configure \
		--prefix="/usr" \
		--localstatedir="/var" \
		--runstatedir="/run" \
		--statedir="/var/lib/unit" \
		--control="unix:/run/control.unit.sock" \
		--pid="/run/unit.pid" \
		--log="/var/log/unit.log" \
		--tmpdir=/tmp \
		--modulesdir="$_modules_dir" \
		--openssl \
		--user=unit \
		--group=unit \
		--cc-opt="$(pkgconf --cflags njs)" \
		--njs \
		--tests
	./configure perl
	./configure php --module=php$_phpver2 --config=php-config$_phpver2
	./configure php --module=php$_phpver3 --config=php-config$_phpver3
	./configure php --module=php$_phpver4 --config=php-config$_phpver4 --lib-path=/usr/lib/php$_phpver4
	./configure python --config=python3-config
	./configure ruby

	make
	make tests
}

check() {
	./build/tests

	local _ruby="$srcdir/ruby-rack"
	gem install rack -i $_ruby --no-document

	# FIXME: some tests fail in CI or locally in checkroot too
	local _fds=160 # fds leaking in tests
	local _allow_fail=no
	case "$CARCH" in
		armhf | armv7) _allow_fail=yes ;; # segfault
	esac
	GEM_PATH=$_ruby pytest test --fds-threshold=$_fds -k "
		not java and not go and not node \
		and not njs \
		and not test_static_mount \
		and not test_php_isolation \
		and not test_php_application_forbidden \
		and not test_php_application_shared_opcache \
		and not test_python_isolation \
		and not test_python_user_group \
		and not test_ruby_isolation \
		and not test_settings_send_timeout \
	" || [ "$_allow_fail" = yes ]
}

package() {
	make unitd-install manpage-install libunit-install DESTDIR="$pkgdir"

	install -D -m 755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m 644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -D -m 644 "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname

	install -D -m 755 tools/setup-unit "$pkgdir"/usr/bin/setup-unit
	install -D -m 755 tools/unitc "$pkgdir"/usr/bin/unitc
	install -D -m 644 tools/README.md "$pkgdir"/usr/share/doc/$pkgname/README.md
}

tools() {
	pkgdesc="Configuration scripts for Unit"
	depends="bash curl"

	amove usr/bin/setup-unit
	amove usr/bin/unitc
	amove usr/share/doc/$pkgname/README.md
}

openrc() {
	default_openrc
	depends="curl"
}

_module() {
	local modname=${subpkgname#"$pkgname"-}
	pkgdesc="$modname module for NGINX Unit"
	depends="$pkgname=$pkgver-r$pkgrel"

	cd "$builddir"
	make $modname-install DESTDIR="$subpkgdir"
}

sha512sums="
f30f54d00d1d0fd2e2378574c365bf464bbd9c1b4d14d69aada2bc3b7c2cec3467a499374cd655f2deb098486d4f2c64be5fd75d9ec4cab2973bb81a2c43097f  unit-1.34.2.tar.gz
76ca55b0b697361c9799a57414d88c36bc59458af8154f7c4275302ad7694bef4ba4a295685956dd5472d8508bac8e1d1977ff7ff4f46cc443f63ea3e3c35d55  unit.initd
f85112726dfcace2b6d94b10669615fef517f5aa10ac858890dd9f5c868a6e2569500f7411f758fcb24c98c9630760d36a74bd33ea510ab0f8ca8cd6cb1fb1e8  unit.confd
723e465162dfdb31881680200221542add414e54ef4f4f1fc57e91b7b57777dfb21c2eee4727ecbba0c1943bb77a2597cc0225b16e334c38258c296d15b1df74  unit.logrotate
c1ba0930f1af713c539453278cf9a2f28e12d2e3bdc0e54dd4a48f0ae1295ef9da23a4b7d484a890c4278cc806bc31f42417fea480faf8411a9c9e738e830f4a  phpver.patch
fd6f0310d5d5f7723765b42e76cbe56b1bb35967932592dc5bd6198c65bda58751a078cf924da3f7b0e110a633ef3e02050daeb9eb63b498c1ca8f94886503b1  fix-ruby-3.4.patch
02186a2b14c2f7851ef23855a786646970d6784e400ed98abe65c10f8e320fafbd5725353125236d4a97f8a714279b5ad1a926587f4e65a59e8f2e25fe3f3bbf  fix-ssl-shutdown.patch
d23f3a784cd60ed22d9d4ffc3798c97845892fe0e1aeeecb18145527ca145c1ad10fef301faf4af25d621f640bfe6bf25d9d3d6fe012f9cd18dd1f192a38ed3c  njs-0.9.0-compat.patch
"
