# Template file for 'nginx'
#
# Consider using mime.types from mime-types package
# See comments in mime-types template
pkgname=nginx
version=1.26.3
revision=1
_tests_commit=88730862618b
_njs_version=0.8.4
create_wrksrc=yes
build_wrksrc="nginx-${version}"
build_style=gnu-makefile
_cfgdir=/etc/nginx
_tmpdir=/var/lib/nginx/tmp
configure_args="--prefix=${_cfgdir}
 --conf-path=${_cfgdir}/nginx.conf
 --sbin-path=/usr/bin/nginx
 --modules-path=/usr/lib/nginx/modules
 --pid-path=/run/nginx.pid
 --lock-path=/var/lock/nginx.lock
 --user=nginx
 --group=nginx
 --http-log-path=/var/log/nginx/access.log
 --error-log-path=/var/log/nginx/error.log
 --http-client-body-temp-path=${_tmpdir}/client-body
 --http-proxy-temp-path=${_tmpdir}/proxy
 --http-fastcgi-temp-path=${_tmpdir}/fastcgi
 --http-scgi-temp-path=${_tmpdir}/scgi
 --http-uwsgi-temp-path=${_tmpdir}/uwsgi
 --with-compat
 --with-file-aio
 --with-http_auth_request_module
 --with-http_dav_module
 --with-http_gunzip_module
 --with-http_gzip_static_module
 --with-http_realip_module
 --with-http_ssl_module
 --with-http_slice_module
 --with-http_stub_status_module
 --with-http_sub_module
 --with-http_v2_module
 --with-http_v3_module
 --with-http_xslt_module=dynamic
 --with-http_geoip_module=dynamic
 --with-http_perl_module=dynamic
 --with-openssl-opt=enable-ktls
 --with-stream=dynamic
 --with-stream_geoip_module=dynamic
 --with-stream_realip_module
 --with-stream_ssl_module
 --with-stream_ssl_preread_module
 --with-mail
 --with-mail_ssl_module
 --with-pcre-jit
 --with-threads
 --add-dynamic-module=../njs-${_njs_version}/nginx"
hostmakedepends="geoip geoip-devel openssl-devel pcre2-devel perl libxslt-devel
 which"
makedepends="${hostmakedepends}"
checkdepends="perl perl-IO-Socket-SSL perl-GD perl-FCGI which uwsgi uwsgi-python3"
short_desc="High performance web and reverse proxy server"
maintainer="Duncaen <duncaen@voidlinux.org>"
license="BSD-2-Clause"
homepage="https://nginx.org"
changelog="https://nginx.org/en/CHANGES-${version%.*}"
distfiles="https://nginx.org/download/nginx-${version}.tar.gz
 https://hg.nginx.org/nginx-tests/archive/${_tests_commit}.tar.gz
 https://hg.nginx.org/njs/archive/${_njs_version}.tar.gz"
checksum="69ee2b237744036e61d24b836668aad3040dda461fe6f570f1787eab570c75aa
 6cc1e4b393c4d05cef06ac16f4a38d504d0e5674fca08c7be20052fd3b07b322
 8191bff8491af9169a92e30e383ef8614717b0c6d40913d83b95051031e92321"
# Tests fail when nginx run as root and there is no nginx user
make_check=ci-skip

if [ "$XBPS_TARGET_LIBC" = "glibc" ]; then
	hostmakedepends+=" libxcrypt-devel"
	makedepends+=" libxcrypt-devel"
fi

# NOTE:
# On update, the pregenerated header file for ARM may need synchronization.
# See srcpkgs/nginx/files/ngx_auto_config.h.armv6l.patch for more information.

conf_files="/etc/nginx/fastcgi.conf
 /etc/nginx/fastcgi_params
 /etc/nginx/koi-win
 /etc/nginx/koi-utf
 /etc/nginx/mime.types
 /etc/nginx/nginx.conf
 /etc/nginx/scgi_params
 /etc/nginx/uwsgi_params
 /etc/nginx/win-utf"
system_accounts="nginx"
nginx_homedir="/var/lib/nginx"
make_dirs="/var/log/nginx 0750 root root
 /var/lib/nginx 1777 root root
 /var/lib/nginx/tmp 0750 nginx root"

do_configure() {
	if [ "$CROSS_BUILD" ]; then
		# fake configure run on host
		unset CC CPP LD CFLAGS CPPFLAGS LDFLAGS

		local perlprefix=${XBPS_STATEDIR}/perlprefix-${XBPS_TARGET_MACHINE}
		mkdir -p $perlprefix
		if [ -d "$XBPS_CROSS_BASE/usr/lib/perl5/core_perl" ]; then
			cp "$XBPS_CROSS_BASE/usr/lib/perl5/core_perl/Config"*.p? $perlprefix
			cp "$XBPS_CROSS_BASE/usr/lib/perl5/core_perl/Errno.pm" $perlprefix
			sed -i -e "s;archlibexp => '\(.*\)';archlibexp => '${XBPS_CROSS_BASE}\1';" \
				${perlprefix}/Config.pm
			sed -i -e "s;^archlibexp='\(.*\)';archlibexp='${XBPS_CROSS_BASE}\1';" \
				${perlprefix}/Config_heavy.pl
		else
			cp "/usr/lib/perl5/core_perl/Config"*.p? $perlprefix
			cp "/usr/lib/perl5/core_perl/Errno.pm" $perlprefix
		fi
		export PERL5LIB=$perlprefix
	fi

	./configure ${configure_args}
	( cd ../njs-${_njs_version} && ./configure )
}

pre_build() {
	if [ "$CROSS_BUILD" ]; then
		mkdir -p ../njs-${_njs_version}/build
		case "$XBPS_TARGET_MACHINE" in
		arm*)
			cp "${FILESDIR}/ngx_auto_config.h.armv6l" objs/ngx_auto_config.h
			cp "${FILESDIR}/njs_auto_config.h.armv6l" ../njs-${_njs_version}/build/njs_auto_config.h
			;;
		aarch64*)
			cp "${FILESDIR}/ngx_auto_config.h.aarch64" objs/ngx_auto_config.h
			cp "${FILESDIR}/njs_auto_config.h.aarch64" ../njs-${_njs_version}/build/njs_auto_config.h
			;;
		riscv64*)
			cp "${FILESDIR}/ngx_auto_config.h.riscv64" objs/ngx_auto_config.h
			cp "${FILESDIR}/njs_auto_config.h.riscv64" ../njs-${_njs_version}/build/njs_auto_config.h
			;;
		esac

		sed -i "s:-I/usr:-I${XBPS_CROSS_BASE}/usr:" objs/Makefile
	fi

	sed -i 's/-lcrypt/$(LDFLAGS) &/' objs/Makefile

	export LD="$CC"

	(
		cd ../njs-${_njs_version}
		make ${makejobs} NJS_CC="$CC" NJS_CFLAGS="$CFLAGS -fPIC" \
			NJS_STATIC_LINK="$AR -r -c" NJS_LINK="$CC -O"
	)
}

do_check() {
	TEST_NGINX_BINARY="$PWD/objs/nginx" \
		prove -j ${XBPS_MAKEJOBS} "../${pkgname}-tests-${_tests_commit}"
}

post_install() {
	sed -e 's|\<user\s\+\w\+;|user html;|g' \
		-e '44s|html|/usr/share/nginx/html|' \
		-e '54s|html|/usr/share/nginx/html|' \
		-i "$DESTDIR/etc/nginx/nginx.conf"
	rm -- "$DESTDIR"/etc/nginx/*.default

	vmkdir usr/share/nginx
	mv "$DESTDIR/etc/nginx/html" "$DESTDIR/usr/share/nginx"

	vman man/nginx.8

	vlicense LICENSE
	vsv nginx
}

nginx-mod-http-geoip_package() {
	short_desc+=" - HTTP GeoIP module"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/lib/nginx/modules/ngx_http_geoip_module.so
	}
}

nginx-mod-http-js_package() {
	short_desc+=" - HTTP njs module"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/lib/nginx/modules/ngx_http_js_module.so
	}
}

nginx-mod-http-perl_package() {
	short_desc+=" - Perl module"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/lib/nginx/modules/ngx_http_perl_module.so
	}
}

nginx-mod-http-xslt-filter_package() {
	short_desc+=" - XSLT filter module"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/lib/nginx/modules/ngx_http_xslt_filter_module.so
	}
}

nginx-mod-stream_package() {
	short_desc+=" - Stream module"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/lib/nginx/modules/ngx_stream_module.so
	}
}

nginx-mod-stream-js_package() {
	short_desc+=" - Stream njs module"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/lib/nginx/modules/ngx_stream_js_module.so
	}
}

nginx-mod-stream-geoip_package() {
	short_desc+=" - Stream GeoIP module"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/lib/nginx/modules/ngx_stream_geoip_module.so
	}
}
