SUMMARY="Robust, small and high performance http and reverse proxy server"
DESCRIPTION="nginx [engine x] is an HTTP and reverse proxy server, a mail proxy \
server, and a generic TCP proxy server, originally written by Igor Sysoev. \
For a long time, it has been running on many heavily loaded Russian sites \
including Yandex, Mail.Ru, VK, and Rambler. According to Netcraft, nginx \
served or proxied 21.89% busiest sites in June 2015."
HOMEPAGE="http://nginx.org/"
COPYRIGHT="2002-2019 Igor Sysoev
	2011-2019 Nginx, Inc."
# "it's complicated", cf. http://packages.gentoo.org/package/www-servers/nginx
LICENSE="BSD (2-clause)"
REVISION="2"
SOURCE_URI="https://nginx.org/download/nginx-$portVersion.tar.gz"
CHECKSUM_SHA256="308919b1a1359315a8066578472f998f14cb32af8de605a3743acca834348b05"
PATCHES="nginx-$portVersion.patchset"
ADDITIONAL_FILES="
	userlaunch.in
	setup-nginx.sh
	"

ARCHITECTURES="all ?x86_gcc2 ?x86"
SECONDARY_ARCHITECTURES="x86"

GLOBAL_WRITABLE_FILES="
	settings/nginx directory keep-old
	"

POST_INSTALL_SCRIPTS="
	$relativePostInstallDir/setup-nginx.sh
	"

#FIXME: on ./auto/configure:
#grep: /etc/group: No such file or directory
PROVIDES="
	nginx$secondaryArchSuffix = $portVersion
	cmd:nginx$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libpcre$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libpcre$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:awk
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:ranlib
	cmd:sed
	"

BUILD()
{
	./configure --prefix=$prefix \
		--sbin-path=$binDir/nginx \
		--doc-prefix=$docDir \
		--user=nginx \
		--group=nginx \
		--conf-path=$settingsDir/nginx/nginx.conf \
		--error-log-path=$localStateDir/log/nginx/error.log \
		--http-log-path=$localStateDir/log/nginx/access.log \
		--pid-path=$localStateDir/run/nginx.pid \
		--lock-path=$localStateDir/run/nginx.lock \
		--http-client-body-temp-path=$localStateDir/tmp/nginx/client_body \
		--http-proxy-temp-path=$localStateDir/tmp/nginx/proxy \
		--http-fastcgi-temp-path=$localStateDir/tmp/nginx/fastcgi \
		--http-uwsgi-temp-path=$localStateDir/tmp/nginx/uwsgi \
		--http-scgi-temp-path=$localStateDir/tmp/nginx/scgi \
		--with-http_v2_module \
		--with-http_ssl_module \
		--with-threads \
		--with-debug
	make $jobArgs
}

INSTALL()
{
	mkdir -p $postInstallDir
	cp -f $portDir/additional-files/setup-nginx.sh $postInstallDir
	local DOCDIR="$docDir"
	sed -i -e "s|@DOCDIR@|$DOCDIR|g" $postInstallDir/setup-nginx.sh
	chmod a+rx "$postInstallDir/setup-nginx.sh"

	mkdir -p \
		$localStateDir/log/nginx \
		$localStateDir/tmp/nginx \
		$localStateDir/www/nginx \
		$localStateDir/nginx \
		$localStateDir/run \
		$dataDir/user_launch \
		$binDir \
		$docDir

	make install

	# prevents broken default configs on package upgrades
	local VARDIR=`finddir B_SYSTEM_VAR_DIRECTORY`

	# fix up the nginx config file and remove extra config
	sed -i \
		-e "44s|html|$VARDIR/www/nginx|" \
		-e "54s|html|$VARDIR/www/nginx|" \
		-e "66s|html|$VARDIR/www/nginx|" \
		-e "90s|html|$VARDIR/www/nginx|" \
		-e "112s|html|$VARDIR/www/nginx|" \
		-e "45s|index.html|index.nginx.html index.html|" \
		-e "91s|index.html|index.nginx.html index.html|" \
		-e "113s|index.html|index.nginx.html index.html|" \
		$settingsDir/nginx/nginx.conf
	rm $settingsDir/nginx/nginx.conf.default

	# launch_daemon setup
	sed \
		-e "s|@APP@|nginx|" \
		-e "s|@BIN@|"$binDir"/nginx|" \
		$portDir/additional-files/userlaunch.in > $dataDir/user_launch/nginx
}
