# Contributor: tcely <openresty+aports@tcely.33mail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=openresty
pkgver=1.27.1.2
_resty_openssl_aux_ver=0.3.0
pkgrel=0
pkgdesc="Scalable Web Platform by Extending NGINX with Lua"
url="https://openresty.org/"
arch="all !ppc64le !riscv64" # blocked by luajit
license="BSD-2-Clause"
options="!check" # tests environment not available
provides="nginx"
provider_priority=1
makedepends="linux-headers gd-dev geoip-dev openssl-dev>3 libxml2-dev libxslt-dev
	luajit-dev pcre-dev perl-dev pkgconf readline-dev zlib-dev"
subpackages="$pkgname-doc $pkgname-openrc"
install="$pkgname.pre-install $pkgname.post-install"
source="https://openresty.org/download/openresty-$pkgver.tar.gz
	lua-resty-openssl-aux-module-$_resty_openssl_aux_ver.tar.gz::https://github.com/fffonion/lua-resty-openssl-aux-module/archive/refs/tags/$_resty_openssl_aux_ver.tar.gz
	openresty.initd
	"

_modules_dir="/usr/lib/nginx/modules"
_add_module() {
	local name="http-$1"
	local _soname="${2-$1}"
	local soname="ngx_http_${_soname//-/_}_module.so"

	subpackages="$subpackages $pkgname-mod-${name}:_module"
	eval "_module_${name//-/_}_so='${soname//\'}'"

	[ -z "$3" ] || { shift; shift; eval "_module_${name//-/_}_deps='${*//\'}'"; }
}

_add_module echo
_add_module headers-more headers_more_filter
_add_module lua lua nginx-mod-devel-kit
_add_module lua-upstream lua_upstream "$pkgname-mod-http-lua"
_add_module redis2
_add_module set-misc set_misc nginx-mod-devel-kit

prepare() {
	default_prepare
	sed -i.orig -e '/--add-module=$dir/s/module/dynamic-&/' configure
}

_configure() {
	local _pkgname=nginx

	./configure -j"$(nproc)" \
	--prefix=/usr/lib/$_pkgname \
	--sbin-path=/usr/sbin/$_pkgname \
	--modules-path="$_modules_dir" \
	--conf-path=/etc/$_pkgname/$_pkgname.conf \
	--pid-path=/var/run/$_pkgname/$_pkgname.pid \
	--lock-path=/var/run/$_pkgname/$_pkgname.lock \
	--error-log-path=/var/log/$_pkgname/error.log \
	--http-log-path=/var/log/$_pkgname/access.log \
	\
	--http-client-body-temp-path=/var/tmp/$_pkgname/client_body \
	--http-proxy-temp-path=/var/tmp/$_pkgname/proxy \
	--http-fastcgi-temp-path=/var/tmp/$_pkgname/fastcgi \
	--http-uwsgi-temp-path=/var/tmp/$_pkgname/uwsgi \
	--http-scgi-temp-path=/var/tmp/$_pkgname/scgi \
	--with-perl_modules_path=/usr/lib/perl5/vendor_perl \
	\
	--user=$_pkgname \
	--group=$_pkgname \
	--with-threads \
	--with-file-aio \
	\
	--with-http_ssl_module \
	--with-http_v2_module \
	--with-http_realip_module \
	--with-http_addition_module \
	--with-http_xslt_module=dynamic \
	--with-http_image_filter_module=dynamic \
	--with-http_geoip_module=dynamic \
	--with-luajit=/usr \
	--with-http_sub_module \
	--with-http_dav_module \
	--with-http_flv_module \
	--with-http_mp4_module \
	--with-http_gunzip_module \
	--with-http_gzip_static_module \
	--with-http_auth_request_module \
	--with-http_random_index_module \
	--with-http_secure_link_module \
	--with-http_degradation_module \
	--with-http_slice_module \
	--with-http_stub_status_module \
	--with-http_perl_module=dynamic \
	--with-mail=dynamic \
	--with-mail_ssl_module \
	--with-stream=dynamic \
	--with-stream_ssl_module \
	--with-stream_realip_module \
	--with-stream_geoip_module=dynamic \
	--with-stream_ssl_preread_module \
	--add-module=$srcdir/lua-resty-openssl-aux-module-$_resty_openssl_aux_ver \
	--add-module=$srcdir/lua-resty-openssl-aux-module-$_resty_openssl_aux_ver/stream \
	--with-pcre-jit \

}

build() {
	_configure
	make
	mv build built

	mv configure.orig configure
	_configure
	make
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
}

_module() {
	#shellcheck disable=SC2295 # package name is sanitized
	local name="${subpkgname#$pkgname-mod-}"
	local soname="$(eval echo "\$_module_${name//-/_}_so")"
	local _deps="$(eval echo "\$_module_${name//-/_}_deps")"

	pkgdesc="$pkgdesc (module $soname)"
	depends="cmd:nginx openresty${_deps:+ }$_deps"
	provides=""

	mkdir -p "${subpkgdir}$_modules_dir"
	cd "${subpkgdir}$_modules_dir"
	mv "$builddir"/built/nginx-*/objs/"$soname" .

	mkdir -p "$subpkgdir/etc/nginx/modules"
	cd "$subpkgdir/etc/nginx/modules"
	echo "load_module \"modules/$soname\";" > "./$name.conf"
}

doc() {
	default_doc
	local _prefix='/usr/lib/nginx'

	mkdir -p "$subpkgdir$_prefix"
	mkdir -p "$subpkgdir$_prefix/bin"
	mkdir -p "$subpkgdir$_prefix/site"

	mv "$pkgdir$_prefix/pod" "$subpkgdir$_prefix/"
	mv "$pkgdir$_prefix/site/pod" "$subpkgdir$_prefix/site/"

	mv "$pkgdir$_prefix/bin/md2pod.pl" "$subpkgdir$_prefix/bin/"
	mv "$pkgdir$_prefix/bin/nginx-xml2pod" "$subpkgdir$_prefix/bin/"
	mv "$pkgdir$_prefix/bin/restydoc" "$subpkgdir$_prefix/bin/"
	mv "$pkgdir$_prefix/bin/restydoc-index" "$subpkgdir$_prefix/bin/"

	mkdir -p "$subpkgdir/usr/lib/perl5/vendor_perl"
	mv "$pkgdir/usr/lib/perl5/vendor_perl/man3" "$subpkgdir/usr/lib/perl5/vendor_perl/"
}

#gpg_signature_extensions="asc"
#gpgfingerprints="good:2545 1EB0 8846 0026 195B  D62C B550 E09E A0E9 8066"

sha512sums="
9246ebc9029086ad27f2754ebd9d164b11c95fa64d668951ed818bdafa38bb42247a709143729e0ab55190d5bcc60a9ed89821958677cc003203578d8cb709cf  openresty-1.27.1.2.tar.gz
f40b5dde5bfb4af88dd084e809442ad6f4a7948f7acb7743ba514013826fb7216addcda21e06742861ec6e781249f8dc1fc7c194011adcc2a41d1e99db84d2c3  lua-resty-openssl-aux-module-0.3.0.tar.gz
954ed4fd744d5e53bca6fbde84121008cd5d57a7e5aced9a48694587e0776b778f6623ecb1bd52e313d5fc09ae98054d5e22232f549b1e13fb2b7281862df5d7  openresty.initd
"
