# --- T2-COPYRIGHT-NOTE-BEGIN ---
# T2 SDE: package/*/freenginx/freenginx.conf
# Copyright (C) 2023 - 2024 The T2 SDE Project
# 
# This Copyright note is generated by scripts/Create-CopyPatch,
# more information can be found in the files COPYING and README.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.
# --- T2-COPYRIGHT-NOTE-END ---

prepare_nginx_modules() {
	local module=
	local moddir=$builddir/nginx-modules

	mkdir -pv $builddir/nginx-modules
	for x in $(match_source_file -p | grep -v $pkg-$ver); do
		module=${x##*/} module=${module%%.tar.*}

		echo "[INFO] extracting nginx module $moddir"
		tar -C $moddir $taropt $x

		var_append confopt ' ' "--add-dynamic-module=$moddir/$module"
	done
}
hook_add prepatch 5 prepare_nginx_modules

# make our build system find config options
# context here: grep sub_scripts scripts/functions.in
hook_add preconf 5 "ln -svf $builddir/$pkg-$ver/auto/{options,configure}"

nginx_fix_cross_integers() {
	# TODO make a condition for 32 bits too

	cat <<-EOF >> objs/ngx_auto_config.h
	#ifndef NGX_PTR_SIZE
	#define NGX_PTR_SIZE  8
	#endif

	#ifndef NGX_MAX_SIZE_T_VALUE
	#define NGX_MAX_SIZE_T_VALUE  9223372036854775807LL
	#endif

	#ifndef NGX_SIZE_T_LEN
	#define NGX_SIZE_T_LEN  (sizeof("-9223372036854775808") - 1)
	#endif

	#ifndef NGX_MAX_OFF_T_VALUE
	#define NGX_MAX_OFF_T_VALUE  9223372036854775807LL
	#endif

	#ifndef NGX_OFF_T_LEN
	#define NGX_OFF_T_LEN  (sizeof("-9223372036854775808") - 1)
	#endif

	#ifndef NGX_TIME_T_SIZE
	#define NGX_TIME_T_SIZE  8
	#endif

	#ifndef NGX_TIME_T_LEN
	#define NGX_TIME_T_LEN  (sizeof("-9223372036854775808") - 1)
	#endif

	#ifndef NGX_MAX_TIME_T_VALUE
	#define NGX_MAX_TIME_T_VALUE  9223372036854775807LL
	#endif
	EOF
}
hook_add premake 5 "nginx_fix_cross_integers"

var_append confopt ' ' "
	--user=$pkg \
	--group=$pkg \
	--prefix=$localstatedir/lib/$pkg \
	--sbin-path=$sbindir/$pkg \
	--conf-path=$sysconfdir/$pkg/$pkg.conf \
	--pid-path=$localstatedir/run/$pkg/$pkg.pid \
	--lock-path=$localstatedir/run/$pkg/$pkg.lock \
	--error-log-path=stderr \

	--with-threads \
	--with-pcre-jit \

	--without-mail \
	--with-pcre2 \

	--http-client-body-temp-path=$localstatedir/lib/$pkg/tmp/client_body \
	--http-fastcgi-temp-path=$localstatedir/lib/$pkg/tmp/fastcgi \
	--http-log-path=$localstatedir/log/nginx/access.log \
	--http-proxy-temp-path=$localstatedir/lib/$pkg/tmp/proxy \
	--http-scgi-temp-path=$localstatedir/lib/$pkg/tmp/scgi \
	--http-uwsgi-temp-path=$localstatedir/lib/$pkg/tmp/uwsgi \

	--with-file-aio \
	--with-http_addition_module \
	--with-http_auth_request_module \
	--with-http_dav_module \
	--with-http_degradation_module \
	--with-http_flv_module \
	--with-http_gunzip_module \
	--with-http_gzip_static_module \
	--with-http_mp4_module \
	--with-http_random_index_module \
	--with-http_realip_module \
	--with-http_secure_link_module \
	--with-http_slice_module \
	--with-http_ssl_module \
	--with-http_stub_status_module \
	--with-http_sub_module \
	--with-http_v2_module \
	--with-http_xslt_module=dynamic \

	--with-stream \
	--with-stream=dynamic \
	--with-stream_realip_module \
	--with-stream_ssl_module \
	--with-stream_ssl_preread_module
"

if pkginstalled libgd; then
	var_append confopt ' ' --with-http_image_filter_module=dynamic
else
	hook_add preconf 5 "rm -f $builddir/$pkg-$ver/src/http/modules/ngx_http_image_filter_module.c"
fi

nginx_load_modules() {
	mkdir -pv $root$sysconfdir/$pkg/modules
	for x in $root$localstatedir/lib/$pkg/modules/*; do
		local module=${x##*/}
		echo "load_module \"modules/${x##*/}\";" > $root$sysconfdir/$pkg/modules/${module%%.so}.conf
	done
}
hook_add postinstall 5 nginx_load_modules