### this file is sourced not run
PKGVER=1.28.0
PKGBUILD=1
PKGARCH=x86_64

# source: james
TARBALL=nginx-$PKGVER.tar.gz
MD5SUM=1ad2b6606c3709ed1268ca32ae447c21
SRC_URL=http://nginx.org/download/nginx-$PKGVER.tar.gz
BUNDLE=

MOD_RTMP_URL=https://github.com/arut/nginx-rtmp-module/archive/6c7719d0ba32e00b563ec70bd43dad11960fa9c4.tar.gz
MOD_RTMP_TARBALL=nginx-rtmp-module-2025-08-23.tar.gz
MOD_RTMP_MD5=5b3a516e4a20c9628b6242d823077d9c

MOD_PUSH_URL=https://github.com/wandenberg/nginx-push-stream-module/archive/refs/tags/0.6.0.tar.gz
MOD_PUSH_TARBALL=nginx-push-stream-module-0.6.0.tar.gz
MOD_PUSH_MD5=d602969b1acf7a601b56ee638a7cd608

MOD_NJS_URL=https://github.com/nginx/njs/archive/refs/tags/0.9.1.tar.gz
MOD_NJS_TARBALL=nginx-njs-module-0.9.1.tar.gz
MOD_NJS_MD5=3af7227bb9c0c41843a6164116bb615d

SLACKREQ='openssl libpcre zlib libgd libatomic_ops
libedit' # libquickjs build-time only, libedit for njs-interactive shell.
SLACKDESC="nginx: nginx $PKGVER (Web Server)  
nginx:  
nginx: Nginx (pronounced engine-x) is a free, open-source, high-performance 
nginx: HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. 
nginx: Nginx is known for its high performance, stability, rich feature set, 
nginx: simple configuration, and low resource consumption.  
nginx:
nginx: Extra modules: rtmp, push-stream
nginx: 
nginx: 
nginx: https://nginx.org/en/
"

### download
pkg_download() {
	pkg_check $SRC_DIR $MOD_RTMP_TARBALL $MOD_RTMP_MD5 $MOD_RTMP_URL &&
	pkg_check $SRC_DIR $MOD_PUSH_TARBALL $MOD_PUSH_MD5 $MOD_PUSH_URL &&
	pkg_check $SRC_DIR $MOD_NJS_TARBALL  $MOD_NJS_MD5  $MOD_NJS_URL &&
	pkg_check $SRC_DIR $TARBALL $MD5SUM $SRC_URL
}

### prepare
pkg_prepare() {
	pkg_download &&
	tar -xf $SRC_DIR/$TARBALL -C ./tmp &&
	tar -xf $SRC_DIR/$MOD_RTMP_TARBALL -C ./tmp/nginx-* &&
	tar -xf $SRC_DIR/$MOD_PUSH_TARBALL -C ./tmp/nginx-* &&
	tar -xf $SRC_DIR/$MOD_NJS_TARBALL  -C ./tmp/nginx-* &&
	wget -O ./tmp/Directives.html https://github.com/arut/nginx-rtmp-module/wiki/Directives &&
	return 0
}

### default pkg_package
### build
pkg_build() {
	# prepare modules
	modroot=$(ls -d /tmp/nginx*) &&
	cd /tmp/nginx* &&
	mv -v nginx-rtmp-module* nginx-rtmp-module &&
	mv -v nginx-push-stream-module* nginx-push-stream-module &&
	mv -v njs* njs &&

	# so that this can be found by njs
	ln -s /usr/$LIBDIR/quickjs/libquickjs.a /usr/$LIBDIR &&
	
	CONFFLAGS="--prefix=/usr \
	--conf-path=/etc/nginx/nginx.conf \
\
	--pid-path=/var/run/nginx.pid \
	--error-log-path=/var/log/nginx/error.log \
	--http-log-path=/var/log/nginx/access.log \
	--http-client-body-temp-path=/var/tmp/nginx/client \
	--http-proxy-temp-path=/var/tmp/nginx/proxy \
	--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
	--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
	--http-scgi-temp-path=/var/tmp/nginx/scgi \
	--user=webuser --group=webgroup \
	--with-compat  \
	--modules-path=/usr/$LIBDIR/nginx \
	--with-http_ssl_module \
\
	--with-select_module \
	--with-poll_module \
	--with-threads \
	--with-file-aio \
	--with-http_v2_module \
	--with-http_v3_module \
	--with-http_realip_module \
	--with-http_addition_module \
	--with-http_xslt_module=dynamic \
	--with-http_image_filter_module=dynamic \
	--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_ssl_preread_module \
	--with-pcre \
	--with-libatomic \
\
	--add-dynamic-module=$modroot/nginx-rtmp-module \
	--add-dynamic-module=$modroot/nginx-push-stream-module \
	--add-dynamic-module=$modroot/njs/nginx \
	" &&
	# --sbin-path=/usr/sbin - already there if prefix=/usr
	# --with-http_geoip_module=dynamic - requires libgeoip
	# --with-stream_geoip_module=dynamic - requires libgeoip
	# --with-google_perftools_module - requires perftools

	mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi} /var/log/nginx/ &&
	pkg_build_autoconf && pkg_build_slackdesc &&

	# move to correct locations
	mkdir -p /var/www &&
	mv /usr/html /var/www &&
	sed -i -e 's|root.*html;|root /var/www/html;|' /etc/nginx/nginx.conf &&

	# startup script
	mkdir -p /etc/init.d &&
	install -m644 /tmp/pkg/60-nginx /etc/init.d &&

	# base docs
	install -m644 man/nginx.8 /usr/share/man/man8 &&
	mkdir -p /usr/share/vim/addons &&
	cp -r contrib/vim/* /usr/share/vim/addons &&

	# module docs
	mkdir -p /usr/share/doc/nginx/{rtmp,push-stream,njs} &&
	mv /tmp/Directives.html /usr/share/doc/nginx/rtmp &&
	cp -r nginx-rtmp-module/{doc,test,README.md,stat.xsl} /usr/share/doc/nginx/rtmp &&
	cp -r nginx-push-stream-module/{docs,misc,README.textile} /usr/share/doc/nginx/push-stream &&	
	cp -r njs/{README.md,CHANGES,ts,test,SECURITY.md} /usr/share/doc/nginx/njs &&
	
	# module tools
	cd njs &&
	./configure &&
	make njs &&
	install build/njs /usr/bin/nginx-njs &&

	# cleanup
	rm -f /usr/$LIBDIR/libquickjs.a &&
	
	return 0
}
