#!/usr/bin/make -f

# Enable compiler hardening flags.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    DEBUG = --enable-debug
else
    DEBUG =
endif

ifneq ($(wildcard /usr/include/systemd/sd-daemon.h),)
    enable-systemd = --enable-systemd
else
    remove-service = shibd-systemd
endif

# Download the upstream source and do the repackaging that we have to do for
# DFSG reasons.  Depends on devscripts.  Mostly a reminder until devscripts
# version 2.15.10 enables setting these options in the watch file.
get-orig-source:
	uscan --repack --compression=xz --destdir='$(CURDIR)'

%:
	dh $@ --parallel --with apache2,autoreconf

override_dh_auto_configure:
	dh_auto_configure -- \
	    --enable-apache-24 --with-apxs24=/usr/bin/apxs2 \
	    $(enable-systemd) \
	    --with-memcached --with-fastcgi --with-gssapi $(DEBUG)

override_dh_auto_install:
	NOKEYGEN=1 dh_auto_install

docdir = debian/tmp/usr/share/doc/shibboleth-*
override_dh_install:
	# The necessary documentation files are installed in each binary
	# package by dh_installdocs below.
	cd $(docdir) && \
	    rm CREDITS.txt LICENSE.txt NOTICE.txt README.txt RELEASE.txt \
	       FASTCGI.LICENSE OPENSSL.LICENSE LOG4CPP.LICENSE
	# The tag file is unreproducible (it captures the full build path),
	# but is used as a "stamp" file during the build.
	cd $(docdir) && rm -f api/shibboleth.tag api/html/*.md5 api/html/*.map
	cd debian/tmp/etc/shibboleth && \
	    rm *.dist *.config shibd-amazon shibd-osx.plist shibd-redhat shibd-suse \
	       $(remove-service)
	mkdir -p debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/shibsp
	mv debian/tmp/usr/include/shibsp/paths.h \
	    debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/shibsp/
	dh_install --list-missing
# This really should be --fail-missing, but #831786 made it impossible.
# If no better solution emerges, I'd prefer backing out of using dh-exec.

UTIL_MANS:=$(patsubst %,debian/%, \
    mdquery.1 resolvertest.1 shib-metagen.1 shib-keygen.8 shibd.8)

debian/%: debian/man-pages/%.pod
	pod2man --section '$(subst .,,$(suffix $@))' --center Shibboleth \
		--release '$(word 3,$(shell ./config.status --version))' $< $@

override_dh_installman: $(UTIL_MANS)
	dh_installman -p shibboleth-sp2-utils $(UTIL_MANS)

override_dh_installdocs:
	dh_installdocs -A doc/NOTICE.txt doc/CREDITS.txt doc/RELEASE.txt doc/README.txt

override_dh_installinit:
# dh_installinit does not find the init script installed by dh_install,
# so we have to handle default+tmpfile and the maintainer scripts separately
	dh_installinit -p shibboleth-sp2-utils --name=shibd --noscripts
	dh_installinit -p shibboleth-sp2-utils --name=shibd --onlyscripts

override_dh_fixperms-arch:
	dh_fixperms
	chown www-data debian/libapache2-mod-shib2/var/log/shibboleth-www

override_dh_makeshlibs:
	dh_makeshlibs -Xusr/lib/$(DEB_HOST_MULTIARCH)/shibboleth

override_dh_clean:
	dh_clean
	rm -f $(UTIL_MANS)
