SUMMARY="An open-source web server for speed-critical environments"
DESCRIPTION="Security, speed, compliance, and flexibility -- all of these \
describe lighttpd (pron. lighty) which is rapidly redefining efficiency of a \
webserver, as it is designed and optimized for high performance environments.
With a small memory footprint compared to other web-servers, effective \
management of the cpu-load, and advanced feature set (FastCGI, SCGI, Auth, \
Output-Compression, URL-Rewriting and many more) lighttpd is the perfect \
solution for every server that is suffering load problems."
HOMEPAGE="https://www.lighttpd.net/"
COPYRIGHT="2003-2019 lighttpd authors"
LICENSE="BSD (3-clause)"
REVISION="1"
SOURCE_URI="https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-$portVersion.tar.xz"
CHECKSUM_SHA256="3bdfce1cf3e9650a556a8c26fb15342c5717c63f530c54693db632b0371dcb78"
ADDITIONAL_FILES="
	create-lighttpd-directories.sh
	index.template
	geoip.shtml
	quickstart.txt
	quickstart-geoip.txt
	"

ARCHITECTURES="all !x86_gcc2 ?arm ?ppc"
if [ "$targetArchitecture" = x86_gcc2 ]; then
SECONDARY_ARCHITECTURES="x86"
fi

commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandBinDir=$prefix/bin
fi

GLOBAL_WRITABLE_FILES="
	settings/lighttpd/lighttpd.conf keep-old
	settings/lighttpd/modules.conf keep-old
	settings/lighttpd/conf.d/access_log.conf keep-old
	settings/lighttpd/conf.d/auth.conf keep-old
	settings/lighttpd/conf.d/cgi.conf keep-old
	settings/lighttpd/conf.d/cml.conf keep-old
	settings/lighttpd/conf.d/compress.conf keep-old
	settings/lighttpd/conf.d/debug.conf keep-old
	settings/lighttpd/conf.d/dirlisting.conf keep-old
	settings/lighttpd/conf.d/evhost.conf keep-old
	settings/lighttpd/conf.d/expire.conf keep-old
	settings/lighttpd/conf.d/fastcgi.conf keep-old
	settings/lighttpd/conf.d/magnet.conf keep-old
	settings/lighttpd/conf.d/mime.conf keep-old
	settings/lighttpd/conf.d/mysql_vhost.conf keep-old
	settings/lighttpd/conf.d/proxy.conf keep-old
	settings/lighttpd/conf.d/rrdtool.conf keep-old
	settings/lighttpd/conf.d/scgi.conf keep-old
	settings/lighttpd/conf.d/secdownload.conf keep-old
	settings/lighttpd/conf.d/simple_vhost.conf keep-old
	settings/lighttpd/conf.d/ssi.conf keep-old
	settings/lighttpd/conf.d/status.conf keep-old
	settings/lighttpd/conf.d/trigger_b4_dl.conf keep-old
	settings/lighttpd/conf.d/userdir.conf keep-old
	settings/lighttpd/conf.d/webdav.conf keep-old
	"
GLOBAL_WRITABLE_FILES_mod_geoip="
	settings/lighttpd/conf.d/geoip.conf keep-old
	"
USER_SETTINGS_FILES="
	settings/lighttpd directory
	settings/lighttpd/conf.d directory
	settings/lighttpd/lighttpd.conf template data/lighttpd/template-settings/lighttpd.conf
	settings/lighttpd/modules.conf template data/lighttpd/template-settings/modules.conf
	settings/lighttpd/conf.d/access_log.conf template data/lighttpd/template-settings/conf.d/access_log.conf
	settings/lighttpd/conf.d/auth.conf template data/lighttpd/template-settings/conf.d/auth.conf
	settings/lighttpd/conf.d/cgi.conf template data/lighttpd/template-settings/conf.d/cgi.conf
	settings/lighttpd/conf.d/cml.conf template data/lighttpd/template-settings/conf.d/cml.conf
	settings/lighttpd/conf.d/compress.conf template data/lighttpd/template-settings/conf.d/compress.conf
	settings/lighttpd/conf.d/debug.conf template data/lighttpd/template-settings/conf.d/debug.conf
	settings/lighttpd/conf.d/dirlisting.conf template data/lighttpd/template-settings/conf.d/dirlisting.conf
	settings/lighttpd/conf.d/evhost.conf template data/lighttpd/template-settings/conf.d/evhost.conf
	settings/lighttpd/conf.d/expire.conf template data/lighttpd/template-settings/conf.d/expire.conf
	settings/lighttpd/conf.d/fastcgi.conf template data/lighttpd/template-settings/conf.d/fastcgi.conf
	settings/lighttpd/conf.d/magnet.conf template data/lighttpd/template-settings/conf.d/magnet.conf
	settings/lighttpd/conf.d/mime.conf template data/lighttpd/template-settings/conf.d/mime.conf
	settings/lighttpd/conf.d/mysql_vhost.conf template data/lighttpd/template-settings/conf.d/mysql_vhost.conf
	settings/lighttpd/conf.d/proxy.conf template data/lighttpd/template-settings/conf.d/proxy.conf
	settings/lighttpd/conf.d/rrdtool.conf template data/lighttpd/template-settings/conf.d/rrdtool.conf
	settings/lighttpd/conf.d/scgi.conf template data/lighttpd/template-settings/conf.d/scgi.conf
	settings/lighttpd/conf.d/secdownload.conf template data/lighttpd/template-settings/conf.d/secdownload.conf
	settings/lighttpd/conf.d/simple_vhost.conf template data/lighttpd/template-settings/conf.d/simple_vhost.conf
	settings/lighttpd/conf.d/ssi.conf template data/lighttpd/template-settings/conf.d/ssi.conf
	settings/lighttpd/conf.d/status.conf template data/lighttpd/template-settings/conf.d/status.conf
	settings/lighttpd/conf.d/trigger_b4_dl.conf template data/lighttpd/template-settings/conf.d/trigger_b4_dl.conf
	settings/lighttpd/conf.d/userdir.conf template data/lighttpd/template-settings/conf.d/userdir.conf
	settings/lighttpd/conf.d/webdav.conf template data/lighttpd/template-settings/conf.d/webdav.conf
	"
USER_SETTINGS_FILES_mod_geoip="
	settings/lighttpd/conf.d/geoip.conf template data/lighttpd/template-settings/conf.d/geoip.conf
	"
POST_INSTALL_SCRIPTS="
	$relativePostInstallDir/create-lighttpd-directories.sh
	"

PROVIDES="
	lighttpd$secondaryArchSuffix = $portVersion
	cmd:lighttpd
	cmd:lighttpd_angel
	lib:mod_access$secondaryArchSuffix
	lib:mod_accesslog$secondaryArchSuffix
	lib:mod_alias$secondaryArchSuffix
	lib:mod_auth$secondaryArchSuffix
	lib:mod_authn_file$secondaryArchSuffix
#	lib:mod_authn_gssapi$secondaryArchSuffix
	lib:mod_authn_ldap$secondaryArchSuffix
#	lib:mod_authn_mysql$secondaryArchSuffix
	lib:mod_cgi$secondaryArchSuffix
	lib:mod_cml$secondaryArchSuffix
	lib:mod_compress$secondaryArchSuffix
	lib:mod_deflate$secondaryArchSuffix
	lib:mod_dirlisting$secondaryArchSuffix
	lib:mod_evasive$secondaryArchSuffix
	lib:mod_evhost$secondaryArchSuffix
	lib:mod_expire$secondaryArchSuffix
	lib:mod_extforward$secondaryArchSuffix
	lib:mod_fastcgi$secondaryArchSuffix
	lib:mod_flv_streaming$secondaryArchSuffix
	lib:mod_indexfile$secondaryArchSuffix
	lib:mod_magnet$secondaryArchSuffix
#	lib:mod_mysql_vhost$secondaryArchSuffix
	lib:mod_openssl$secondaryArchSuffix
	lib:mod_proxy$secondaryArchSuffix
	lib:mod_redirect$secondaryArchSuffix
	lib:mod_rewrite$secondaryArchSuffix
	lib:mod_rrdtool$secondaryArchSuffix
	lib:mod_scgi$secondaryArchSuffix
	lib:mod_secdownload$secondaryArchSuffix
	lib:mod_setenv$secondaryArchSuffix
	lib:mod_simple_vhost$secondaryArchSuffix
	lib:mod_sockproxy$secondaryArchSuffix
	lib:mod_ssi$secondaryArchSuffix
	lib:mod_staticfile$secondaryArchSuffix
	lib:mod_status$secondaryArchSuffix
#	lib:mod_trigger_b4_dl$secondaryArchSuffix
	lib:mod_uploadprogress$secondaryArchSuffix
	lib:mod_userdir$secondaryArchSuffix
	lib:mod_usertrack$secondaryArchSuffix
	lib:mod_vhostdb_ldap$secondaryArchSuffix
	lib:mod_vhostdb$secondaryArchSuffix
	lib:mod_webdav$secondaryArchSuffix
	lib:mod_wstunnel$secondaryArchSuffix
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libldap$secondaryArchSuffix
	lib:liblua$secondaryArchSuffix
	lib:libpcre$secondaryArchSuffix
	lib:libsqlite3$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libuuid$secondaryArchSuffix >= 1.3
	lib:libxml2$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_mod_geoip="
	lighttpd${secondaryArchSuffix}_mod_geoip = $portVersion
	lib:mod_geoip$secondaryArchSuffix
	"
REQUIRES_mod_geoip="
	haiku$secondaryArchSuffix
	lighttpd$secondaryArchSuffix == $portVersion base
	haiku$secondaryArchSuffix
	lib:libGeoIP$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	"

PROVIDES_devel="
	lighttpd${secondaryArchSuffix}_devel = $portVersion
	"
REQUIRES_devel="
	lighttpd$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libbz2$secondaryArchSuffix
	devel:libcrypto$secondaryArchSuffix
	devel:libGeoIP$secondaryArchSuffix
	devel:libintl$secondaryArchSuffix
	devel:libldap$secondaryArchSuffix
	devel:liblua$secondaryArchSuffix
	devel:libpcre$secondaryArchSuffix
	devel:libsqlite3$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	devel:libuuid$secondaryArchSuffix >= 1.3
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:aclocal
	cmd:autoconf
	cmd:autom4te
	cmd:automake
	cmd:awk
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:perl
	cmd:pkg_config$secondaryArchSuffix
	cmd:sed
	cmd:uname
	cmd:which
	"

debugList=(
	"$commandBinDir"/lighttpd
	"$commandBinDir"/lighttpd-angel
	"${libDir/lighttpd$secondaryArchSuffix/lighttpd${secondaryArchSuffix}_mod_geoip}"/lighttpd/mod_geoip.so
	)
for i in `echo "$PROVIDES" | sed -e "/^\s*#/ d;" | sed -n -e \
               "s/^\s*lib:\(mod_.*\)$secondaryArchSuffix/\1/p;"` ; do
       debugList+=("$libDir"/lighttpd/$i.so)
done
defineDebugInfoPackage lighttpd$secondaryArchSuffix "${debugList[@]}"

BUILD()
{
	LIBS="-lintl" \
	runConfigure --omit-dirs "libDir sbinDir" ./configure \
		--libdir=$libDir/lighttpd --sbindir=$commandBinDir \
		--with-openssl --with-zlib --with-bzip2 --with-pcre \
		--with-libxml --with-ldap --with-lua --with-sqlite \
		--with-uuid --with-webdav-locks --with-webdav-props \
		--with-geoip
	make $jobArgs
}

INSTALL()
{
	make install
	mkdir -p $postInstallDir
	cp -f $portDir/additional-files/create-lighttpd-directories.sh $postInstallDir
	rm $libDir/lighttpd/mod_*.la
	mkdir -p \
		$localStateDir/cache/lighttpd \
		$localStateDir/lib/lighttpd \
		$localStateDir/log/lighttpd \
		$localStateDir/tmp/lighttpd \
		$localStateDir/www/htdocs \
		$settingsDir/lighttpd/conf.d \
		$dataDir/lighttpd \
		$docDir
	cp -f \
		$portDir/additional-files/quickstart.txt \
		$portDir/additional-files/quickstart-geoip.txt \
		$docDir

	constantSystemSettingsDir="`finddir B_SYSTEM_SETTINGS_DIRECTORY`"
	constantSystemCacheDir="`finddir B_SYSTEM_CACHE_DIRECTORY`"
	constantSystemDataDir="`finddir B_SYSTEM_DATA_DIRECTORY`"
	constantSystemVarDir="`finddir B_SYSTEM_VAR_DIRECTORY`"

# Tweak lighttpd.conf to have a working config with mod_ssi enabled.
# Tweak ssi.conf to add index.shtml as index-file.names for mod_indexfile.
# Tweak modules.conf to also add index.lighttpd.html as index-file.names.
# This ensures a default main page will still be available if the user later
# choses to disable SSI.

	cp doc/config/lighttpd.conf $settingsDir/lighttpd
	sed -i \
		-e "s|^\(## \)/etc\(/lighttpd/lighttpd\.conf\)|\1$constantSystemSettingsDir\2|" \
		-e "s|^\(## check \)/etc\(/lighttpd/conf\.d/\)|\1$constantSystemSettingsDir\2|" \
		-e "/^var\.log_root/ \
			s|\"/var\(/log/lighttpd\"\)|\"$constantSystemVarDir\1|" \
		-e "/^var\.server_root/ \
			s|\"/srv/www\"|\"$constantSystemDataDir/lighttpd/www\"|" \
		-e "/^var\.server_root/ \
			a #var.server_root = \"/$relativeSharedStateDir/www\"" \
		-e "/^var\.state_dir/ \
			s|\"/var/run\"|\"$constantSystemVarDir/run\"|" \
		-e "/^var\.conf_dir/ \
			s|\"/etc\(/lighttpd\"\)|\"$constantSystemSettingsDir\1|" \
		-e "/^server\.use-ipv6 / s|\"enable\"|\"disable\"|" \
		-e "s|^server\.username |#&|" \
		-e "s|^server\.groupname |#&|" \
		-e "s|^server\.max-fds |#&|" \
		-e "s|^server\.max-connections |#&|" \
		-e "s|^server\.network-backend |#&|" \
		-e "/^server\.event-handler/ \
			s|\"linux-sysepoll\"|\"select\"|" \
		-e "/^server\.upload-dirs/ \
			s|\"/var/tmp\"|\"$constantSystemVarDir/tmp/lighttpd\"|" \
		-e "/^## \$ cat / \
			s|/etc\(/ssl/private/lighttpd\.key\)|$constantSystemSettingsDir\1|" \
		-e "/^## \$ cat / \
			s|/etc\(/ssl/certs/lighttpd\.crt\)|$constantSystemSettingsDir\1|" \
		-e "s| /etc\(/ssl/private/lighttpd\.pem\)| $constantSystemSettingsDir\1|" \
		-e "/^#ssl\.pemfile/ \
			s|\"/etc\(/ssl/private/lighttpd.pem\"\)|\"$constantSystemSettingsDir\1|" \
		-e "/^ssl\.pemfile/ \
			s|\"/etc\(/ssl/private/www\.example\.com\.pem\"\)|\"$constantSystemSettingsDir\1|" \
		-e "/^#include_shell \"cat \/etc/ \
			s|/etc\(/lighttpd/\)|\"$constantSystemSettingsDir\1|" \
		-e "/^include \"modules\.conf\"/ \
			a index-file.names += ( \"index.lighttpd.html\" )" \
		$settingsDir/lighttpd/lighttpd.conf

	cp doc/config/modules.conf $settingsDir/lighttpd
	# Enable mod_ssi
	sed -i \
		-e "s|^#\(include \"conf\.d/ssi\.conf\"\)|\1|" \
		$settingsDir/lighttpd/modules.conf

	cp doc/config/conf.d/*.conf $settingsDir/lighttpd/conf.d

	sed -i \
		-e "/^ssi\.extension/ \
			a index-file.names += ( \"index.lighttpd.shtml\" )" \
		$settingsDir/lighttpd/conf.d/ssi.conf

	sed -i \
		-e "/^#geoip\.db-filename =/ \
			s|/path/to/|$constantSystemCacheDir/GeoIP/|" \
		$settingsDir/lighttpd/conf.d/geoip.conf

	# Create a read-only copy of the settings
	cp -rp $settingsDir/lighttpd $dataDir/lighttpd/template-settings

	mkdir -p $dataDir/lighttpd/www/htdocs/geoip

# Create and install a static index.lighttpd.html test page from the template.

	cp $portDir/additional-files/index.template \
		$dataDir/lighttpd/www/htdocs/index.lighttpd.html
	sed -i \
		-e "s|@SYSTEM@|`uname -a`|" \
		-e "s|@GETARCH@|`getarch`|" \
		-e "s|@HOSTNAME@|`uname -n`|" \
		-e "s|@LIGHTTPD_VERSION@|lighttpd/$portVersion|" \
		$dataDir/lighttpd/www/htdocs/index.lighttpd.html

# Create and install the SSI index.lighttpd.shtml test page from the template.

	cp $portDir/additional-files/index.template \
		$dataDir/lighttpd/www/htdocs/index.lighttpd.shtml
	sed -i \
		-e "s|@SYSTEM@|<!--#exec cmd=\"/bin/uname -a\"-->|" \
		-e "s|@GETARCH@|<!--#exec cmd=\"/bin/getarch\"-->|" \
		-e "s|@HOSTNAME@|<!--#echo var=\"HTTP_HOST\"-->|" \
		-e "s|@LIGHTTPD_VERSION@|<!--#echo var=\"SERVER_SOFTWARE\"-->|" \
		$dataDir/lighttpd/www/htdocs/index.lighttpd.shtml

# Create and install the geoip.shtml demo page.

	cp $portDir/additional-files/geoip.shtml \
		$dataDir/lighttpd/www/htdocs/geoip/index.lighttpd.shtml

# Install headers in $includeDir/lighttpd/ for building third-party modules.
	mkdir -p  $includeDir/lighttpd
	cp -p src/*.h $includeDir/lighttpd
# Remove those headers that won't ever be needed for building a module.
	for i in \
		configparser.h \
		network.h \
		proc_open.h \
		request.h
	do
		rm -f $includeDir/lighttpd/$i
	done

	packageEntries mod_geoip \
		$libDir/lighttpd/mod_geoip.so \
		$settingsDir/lighttpd/conf.d/geoip.conf \
		$dataDir/lighttpd/template-settings/conf.d/geoip.conf \
		$dataDir/lighttpd/www/htdocs/geoip \
		$docDir/quickstart-geoip.txt

	packageEntries devel \
		$developDir
}

TEST()
{
	make check
}
