# Template file for 'apache'
pkgname=apache
version=2.4.58
revision=1
build_style=gnu-configure
configure_args="--prefix= --sbindir=/usr/bin --enable-pie --enable-modules=all
 --enable-mods-shared=all --enable-authn-dbm --enable-authn-anon
 --enable-authn-dbd --enable-authn-alias --enable-authz-dbm --enable-http2
 --enable-authz-owner --enable-authnz-ldap --enable-auth-digest -enable-isapi
 --enable-file-cache --enable-cache --enable-disk-cache --enable-mem-cache
 --enable-dbd --enable-bucketeer --enable-dumpio --enable-echo --enable-reqtimeout
 --enable-ext-filter --enable-substitute --enable-deflate --enable-charset-lite
 --enable-ldap --enable-log-forensic --enable-logio --enable-mime-magic
 --enable-cern-meta --enable-expires --enable-headers --enable-ident
 --enable-usertrack --enable-unique-id --enable-proxy --enable-proxy-connect
 --enable-proxy-ftp --enable-proxy-http --enable-proxy-scgi --enable-proxy-ajp
 --enable-proxy-balancer --enable-ssl --enable-http --enable-speling
 --enable-dav --enable-info --enable-cgi --enable-cgid --enable-dav-fs
 --enable-dav-lock --enable-vhost-alias --enable-imagemap --enable-so
 --enable-rewrite --enable-layout=XBPS --sysconfdir=/etc/${pkgname}
 --enable-mpms-shared=all --with-z=${XBPS_CROSS_BASE}/usr
 --with-apr=${XBPS_CROSS_BASE}/usr/bin/apr-1-config"
conf_files="
	/etc/${pkgname}/extra/*.conf
	/etc/${pkgname}/httpd.conf
	/etc/${pkgname}/magic
	/etc/${pkgname}/mime.types"
hostmakedepends="pkg-config perl"
makedepends="zlib-devel libuuid-devel pcre2-devel nghttp2-devel
 openssl-devel db-devel gdbm-devel expat-devel libldap-devel apr-util-devel"
depends="apache-htpasswd"
short_desc="Apache http server"
maintainer="ologantr <mrphyber@protonmail.com>"
license="Apache-2.0"
homepage="https://httpd.apache.org/"
changelog="https://www.apache.org/dist/httpd/CHANGES_2.4"
distfiles="https://archive.apache.org/dist/httpd/httpd-${version}.tar.bz2"
checksum=fa16d72a078210a54c47dd5bef2f8b9b8a01d94909a51453956b3ec6442ea4c5

if [ "$XBPS_TARGET_LIBC" = "glibc" ]; then
	makedepends+=" libxcrypt-devel"
fi

system_accounts="_apache"
_apache_homedir="/srv/www/$pkgname"

make_dirs="
 /srv/www/apache 0755 _apache _apache
 /var/log/httpd 0755 _apache _apache"

if [ "$CROSS_BUILD" ]; then
	# FIXME: is sizeof(void*) ever less than sizeof(long)?
	export ap_cv_void_ptr_lt_long=no
	# apr is 1.3.0 or later
	export ap_cv_aprver13=yes
fi

pre_configure() {
	# set default user
	vsed -e 's#User daemon#User _apache#' \
		-e 's#Group daemon#Group _apache#' \
		-i docs/conf/httpd.conf.in

	cat ${FILESDIR}/xbps.layout >> config.layout
	if [ "$CROSS_BUILD" ]; then
		# NOTE: Here, we prepare a modified version of
		# libtool into $XBPS_WRAPPERDIR. This sets...
		#
		# `CC=${CC:=gcc}`
		# `LD=${LD:=ld}`
		#
		# ...so the build doesn't attempt to use the
		# host linker...
		sed ${XBPS_CROSS_BASE}/usr/bin/libtool \
			-e 's;^\(LD\)="\(.\+\)"$;\1="${\1:=\2}";g' \
			-e 's;^\(CC\)="\(.\+\)"$;\1="${\1:=\2}";g' \
			> ${XBPS_WRAPPERDIR}/libtool
		chmod +x ${XBPS_WRAPPERDIR}/libtool

		# Here, we're altering the configure script
		# to use our modified libtool...
		vsed -i configure \
			-e "s;\(my_libtool=\).*$;\1\""${XBPS_WRAPPERDIR}"/libtool\";g" \
			-e "s;\(SH_LIBTOOL=\)'\$(SHELL).*'$;\1'\$(LIBTOOL)';g"

		# pcre{,2}-config detection doesn't work on cross
		# since httpd-2.4.53, manually export PCRE_CONFIG
		# to let configure know where it is
		export PCRE_CONFIG="${XBPS_CROSS_BASE}/usr/bin/pcre2-config"
		configure_args+=" --with-apr-util=$XBPS_WRAPPERDIR/apu-1-config"
	else
		configure_args+=" --with-apr-util=/usr/bin/apu-1-config"
	fi
}

post_configure() {
	if [ "$CROSS_BUILD" ]; then
		# Build gen_test_char using $BUILD_CC
		vsed -i server/Makefile \
			-e "s;\$(LINK) \$(EXTRA_LDFLAGS) \$(\(gen_test_char\).*;${BUILD_CC} -o \1 \1.c -I${XBPS_CROSS_BASE}/usr/include/apr-1;"
		# More hacks to make cross compiling work
		vsed -i build/rules.mk support/Makefile \
			-e "s;--mode=compile;& --tag=CC;" \
			-e "s;--mode=link;& --tag=CC;"
	fi
}

post_install() {
	# Remove CROSS_BUILD and ccache reference
	sed -i -e "s; -L${XBPS_CROSS_BASE}/*usr/lib$;;" \
		-e "s; -L[^[:space:]]*/usr/lib[36][24] ; ;" \
		-e "s; -I${XBPS_CROSS_BASE}/*usr/include/* ; ;" \
		-e "s; -I${XBPS_CROSS_BASE}/*usr/include/*$;;" \
		-e "s;${XBPS_CROSS_BASE}/*usr/;/usr/;g" \
		-e 's; -ffile-prefix-map=[^[:space:]]*;;' \
		"$DESTDIR/usr/share/apache/webroot/build/config_vars.mk"

	# Set a working ServerRoot
	#
	# Load modules:
	#  * mod_include.so
	#  * mod_slotmem_shm.so
	#  * mod_negotiation.so
	#  * mod_userdir.so
	#
	# Avoid loading modules:
	#  * mod_unique_id.so
	#
	# Set and enable ServerName to 127.0.0.1.
	#
	# include extras:
	#  * httpd-multilang-errordoc.conf
	#  * httpd-autoindex.conf
	#  * httpd-languages.conf
	#  * httpd-userdir.conf
	#  * httpd-default.conf
	#
	vsed \
		-e 's|ServerRoot ""|ServerRoot "/srv/www/apache"|' \
		-e 's|#\(LoadModule include_module /usr/libexec/httpd/mod_include.so\)|\1|' \
		-e 's|\(LoadModule unique_id_module /usr/libexec/httpd/mod_unique_id.so\)|#\1|' \
		-e 's|#\(LoadModule slotmem_shm_module /usr/libexec/httpd/mod_slotmem_shm.so\)|\1|' \
		-e 's|#\(LoadModule negotiation_module /usr/libexec/httpd/mod_negotiation.so\)|\1|' \
		-e 's|#\(LoadModule userdir_module /usr/libexec/httpd/mod_userdir.so\)|\1|' \
		-e 's|#ServerName www.example.com:80|ServerName 127.0.0.1:80|' \
		-e 's|#\(Include /etc/apache/extra/httpd-multilang-errordoc.conf\)|\1|' \
		-e 's|#\(Include /etc/apache/extra/httpd-autoindex.conf\)|\1|' \
		-e 's|#\(Include /etc/apache/extra/httpd-languages.conf\)|\1|' \
		-e 's|#\(Include /etc/apache/extra/httpd-userdir.conf\)|\1|' \
		-e 's|#\(Include /etc/apache/extra/httpd-default.conf\)|\1|' \
		-i "${DESTDIR}/etc/apache/httpd.conf"

	# Remove unused stuff.
	vmkdir usr/share/apache/webroot
	mv ${DESTDIR}/srv/www/$pkgname/cgi-bin ${DESTDIR}/usr/share/apache/webroot
	rm -rf ${DESTDIR}/srv
	rm -rf ${DESTDIR}/etc/$pkgname/original

	vinstall ${FILESDIR}/apache.logrotate 644 etc/logrotate.d apache
	vsv apache
	vdoc ${FILESDIR}/README.voidlinux
}

apache-htpasswd_package() {
	short_desc+=" - htpasswd"
	pkg_install() {
		vmove usr/bin/htpasswd
		vmove usr/share/man/man1/htpasswd.1
	}
}

apache-devel_package() {
	depends="apr-devel apr-util-devel perl"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove usr/share/apache/webroot/build
		vmove usr/bin/apxs
		vmove "usr/share/man/man1/apxs*"
		vmove "usr/share/apache/webroot/manual/programs/apxs*"
	}
}
