#!/usr/bin/make -f
# -*- makefile -*-

DEB_BUILD_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow
include /usr/share/dpkg/buildflags.mk

LSB_RELEASE := $(shell lsb_release -i -s)
SERVER_VERSION := $(shell dpkg-parsechangelog | perl -ne 'print $$1 if m/Version:\s*([\d\.]+)/')
DEBIAN_VERSION := $(shell dpkg-parsechangelog | perl -ne 'print $$1 if m/Version:\s*(.+)/')
BUILD_DATETIME := $(shell date -u --date="`dpkg-parsechangelog -S Date`" +%FT%T)
MODULE_DIR := /usr/lib/apache2/modules/
API = $(shell perl -ne 'print $$1 if m/define\s+MODULE_MAGIC_NUMBER_MAJOR\s+?(.*)$$/' < include/ap_mmn.h)
AP2_CFLAGS = -pipe -Werror=implicit-function-declaration $(CFLAGS)
AP2_LDFLAGS = -Wl,--as-needed $(LDFLAGS)
AP2_CPPFLAGS = -DPLATFORM='\"$(LSB_RELEASE)\"' -DBUILD_DATETIME='\"$(BUILD_DATETIME)\"' $(CPPFLAGS)
AP2_LTFLAGS = --no-silent
DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

support/suexec-custom.c: support/suexec.c debian/patches/suexec-custom.patch
	cp support/suexec.c support/suexec-custom.c
	patch -p1 -i debian/patches/suexec-custom.patch

prebuild-checks:
	ERRS="" ;\
	for a in $$(find debian/config-dir/ -type f) ; do \
		t="$$a.$$$$" ;\
		unexpand < "$$a" > "$$t" ;\
		cmp -s "$$a" "$$t" || ERRS="$$ERRS $$a" ;\
		rm "$$t" ;\
	done ;\
	if [ -n "$$ERRS" ] ; then \
		echo 'ERROR: The following files contain spaces instead of tabs. Run through unexpand!' ;\
		ls -1 $$ERRS ;\
		false ;\
	fi
	MD5=$$(md5sum debian/index.html 2> /dev/null |cut -d' ' -f 1) ; \
	if ! grep -q $$MD5 debian/apache2.postrm ; then \
		echo "ERROR: index.html's md5 is missing in postrm!" ;\
		false ; \
	fi

debian/fixup_conffiles.tgz: \
    debian/config-dir/sites-available/000-default.conf \
    debian/config-dir/sites-available/default-ssl.conf \
    debian/config-dir/conf-available/charset.conf \
    debian/config-dir/conf-available/localized-error-pages.conf \
    debian/config-dir/conf-available/other-vhosts-access-log.conf \
    debian/config-dir/conf-available/security.conf \
    debian/config-dir/mods-available/cern_meta.load \
    debian/config-dir/mods-available/ident.load \
    debian/config-dir/mods-available/imagemap.load
	@# mtime/owner/group/mode are for reproducible build
	tar \
		--mtime=2000-01-01T00:00Z \
		--owner=root:0 \
		--group=root:0 \
		--mode=0644 \
		--transform 's,.*/,,' \
		-c $^ | gzip -n > $@

debian/fixup_conffiles.b64: debian/fixup_conffiles.tgz
	base64 < $< > $@

debian/apache2.preinst: debian/apache2.preinst.in debian/fixup_conffiles.b64 debian/create_preinst
	debian/create_preinst

clean-config-vars:
	# Clean up config_vars.mk
	# FIXME: Maybe someone could document here why we actually need to
	# cleanup some stuff here
	perl ./debian/clean_config_vars '$(AP2_CFLAGS)' '$(AP2_CPPFLAGS)' '$(AP2_LDFLAGS)'

%: %.in
	sed 's#__SERVER_VERSION__#$(SERVER_VERSION)#; s#__MODULE_DIR__#$(MODULE_DIR)#; s#__API__#$(API)#;' $< > $@
	chmod `/usr/bin/stat -c '%a' "$<"` $@

prepare-scripts: debian/a2query debian/debhelper/dh_apache2
	(grep -s -v apache2:API debian/apache2-bin.substvars; echo apache2:API=apache2-api-$(API)) > debian/apache2-bin.substvars.new
	mv debian/apache2-bin.substvars.new debian/apache2-bin.substvars

generate-maintainer-scripts: debian/apache2.preinst
	set -e ; \
	for type in custom pristine ; do \
		for f in postinst preinst prerm links dirs lintian-overrides postrm; do \
			if [ -e debian/apache2-suexec.$$f.in ] ; then \
				perl -pe "s{__TYPE__}{$$type}g" < debian/apache2-suexec.$$f.in > debian/apache2-suexec-$$type.$$f ;\
				chmod `/usr/bin/stat -c '%a' "debian/apache2-suexec.$$f.in"` debian/apache2-suexec-$$type.$$f ;\
			fi ;\
		done ;\
	done

clean build build-arch build-indep binary binary-arch binary-indep: %:
	dh $@ --parallel --with autotools_dev,systemd

override_dh_auto_configure: generate-maintainer-scripts prebuild-checks support/suexec-custom.c
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--enable-layout=Debian --enable-so --with-program-name=apache2 \
		--enable-suexec --with-suexec-caller=www-data \
		--with-suexec-bin=/usr/lib/apache2/suexec --with-suexec-docroot=/var/www \
		--with-suexec-userdir=public_html --with-suexec-logfile=/var/log/apache2/suexec.log \
		--with-suexec-uidmin=100 --enable-suexec=shared --enable-log-config=static \
		--with-apr=/usr/bin/apr-1-config --with-apr-util=/usr/bin/apu-1-config \
		--with-pcre=yes \
		--enable-pie \
		--enable-mpms-shared=all \
		--enable-mods-shared="all cgi ident authnz_fcgi imagemap cern_meta proxy_fdpass proxy_http2 bucketeer case_filter case_filter_in" \
		--enable-mods-static="unixd logio watchdog version" \
		CFLAGS="$(AP2_CFLAGS)" CPPFLAGS="$(AP2_CPPFLAGS)" LDFLAGS="$(AP2_LDFLAGS)" \
		LTFLAGS="$(AP2_LTFLAGS)" SHELL=/bin/bash


debian/config-dir/apache2.conf: debian/config-dir/apache2.conf.in
	@# Sanity check that the marker is actually there
	grep -q ___MUTEX___ $<
ifeq "$(DEB_BUILD_ARCH_OS)" "linux"
	@# On linux, we use the default pthread mutex. Comment out
	@# Mutex directive.
	perl -p -e 's,___MUTEX___,#,' < $< > $@
else
	@# On kfreebsd/hurd, pthread mutexes are not robust. There, we need to
	@# use fcntl and set the directory explicitly for multi-instance setups.
	perl -p -i -e 's,___MUTEX___,,' < $< > $@
endif

override_dh_install: clean-config-vars prepare-scripts debian/config-dir/apache2.conf
	# don't install mod_ssl_openssl.h in apache2-dev
	dh_install --list-missing -papache2-dev -Xopenssl
	dh_install --list-missing --remaining-packages

override_dh_fixperms-arch:
	# standard suexec
	chmod 4754 debian/apache2-suexec-pristine/usr/lib/apache2/suexec-pristine
	chgrp www-data debian/apache2-suexec-pristine/usr/lib/apache2/suexec-pristine
	# configurable suexec
	chmod 4754 debian/apache2-suexec-custom/usr/lib/apache2/suexec-custom
	chgrp www-data debian/apache2-suexec-custom/usr/lib/apache2/suexec-custom
	dh_fixperms -a -Xusr/lib/apache2/suexec-custom -Xusr/lib/apache2/suexec-pristine
	chown -R www-data:www-data debian/apache2/var/cache/apache2/mod_cache_disk
	chown root:adm debian/apache2/var/log/apache2
	chmod o-rx debian/apache2/var/log/apache2

override_dh_fixperms-indep:
	dh_fixperms -i

override_dh_installinit:
	dh_installinit --error-handler=true
	# We enable apache-htcacheclean but we don't start it, some
	# custom postinst code will then manually either disable it or
	# start the service
	dh_installinit --name apache-htcacheclean --no-start

override_dh_systemd_enable:
	dh_systemd_enable -papache2 apache2.service
	dh_systemd_enable -papache2 --name=apache2@ apache2@.service
	dh_systemd_enable -papache2 --name=apache-htcacheclean apache-htcacheclean.service
	dh_systemd_enable -papache2 --name=apache-htcacheclean@ apache-htcacheclean@.service

override_dh_installdocs-indep:
	# TODO: So, did anyone check convert_docs needs an update? ;)
	perl debian/convert_docs debian/apache2-doc/usr/share/doc/apache2-doc/manual
	dh_installdocs -i

override_dh_installdocs-arch:
	dh_installdocs --link-doc=apache2 -papache2 -papache2-dbg
	dh_installdocs --link-doc=apache2-dev -papache2-ssl-dev
	dh_installdocs -a

override_dh_installchangelogs:
	# Do not install changelogs the -data package not to reduce
	# disk space usage.
	dh_installchangelogs -Napache2-data

override_dh_installman:
	mv debian/tmp/usr/share/man/man8/suexec.8 debian/tmp/usr/share/man/man8/suexec-pristine.8
	pod2man debian/debhelper/dh_apache2 > debian/manpages/dh_apache2.1
	pod2man debian/a2query.in > debian/manpages/a2query.8
	dh_installman

override_dh_strip:
	dh_strip --dbg-package=apache2-dbg

override_dh_auto_install:
	dh_auto_install -- -j1

override_dh_builddeb:
	dh_builddeb -- -Zxz

.PHONY: generate-maintainer-scripts clean-config-vars prepare-scripts prebuild-checks
.PHONY: clean build build-arch build-indep binary binary-arch binary-indep
