#!/usr/bin/make -f

# Enable compiler hardening flags.
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow,+pie

# Add --as-needed to drop a few unnecessary dependencies.
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

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

DOCS = debian/libshibsp-doc/usr/share/doc/libshibsp-doc

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

# These variables are used by get-orig-source and to generate man pages.
DEBVERS := $(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2 \
		| cut -d- -f1)
VERSION := $(shell echo '$(DEBVERS)' | sed -e 's/[+-].*//' -e 's/~//g')
URL      = http://www.shibboleth.net/downloads/service-provider/

# Download the upstream source and do the repackaging that we have to do for
# DFSG reasons.  Depends on wget.
get-orig-source:
	wget $(URL)/$(VERSION)/shibboleth-sp-$(VERSION).tar.gz
	tar xfz shibboleth-sp-$(VERSION).tar.gz
	rm shibboleth-sp-$(VERSION).tar.gz
	rm shibboleth-sp-$(VERSION)/schemas/WS-Trust.xsd
	mv shibboleth-sp-$(VERSION) shibboleth-sp2_$(DEBVERS).orig
	tar cf shibboleth-sp2_$(DEBVERS).orig.tar \
	    shibboleth-sp2_$(DEBVERS).orig
	rm -rf shibboleth-sp2_$(DEBVERS).orig
	xz shibboleth-sp2_$(DEBVERS).orig.tar

%:
	dh $@ --with apache2,autoreconf

override_dh_autoreconf:
	dh_autoreconf --as-needed

override_dh_auto_configure:
	dh_auto_configure --						\
	    --libexecdir=/usr/lib/$(DEB_HOST_MULTIARCH)/shibboleth	\
	    --localstatedir=/var --enable-apache-24			\
	    --with-apxs24=/usr/bin/apxs2 --disable-dependency-tracking	\
	    --with-memcached --with-fastcgi --with-gssapi $(SYSTEM)

override_dh_auto_install:
	NOKEYGEN=1 dh_auto_install
	rm -r debian/tmp/usr/share/doc/shibboleth*
	rm -r debian/tmp/var/run
	rm debian/tmp/etc/shibboleth/*.dist
	rm debian/tmp/etc/shibboleth/*.config
	rm debian/tmp/etc/shibboleth/shibd-osx.plist
	rm debian/tmp/etc/shibboleth/shibd-redhat
	rm debian/tmp/etc/shibboleth/shibd-suse
	chmod +x debian/tmp/etc/shibboleth/keygen.sh
	mv debian/tmp/etc/shibboleth/keygen.sh debian/tmp/usr/sbin/shib-keygen
	mv debian/tmp/etc/shibboleth/metagen.sh debian/tmp/usr/bin/shib-metagen
	mv debian/tmp/etc/shibboleth/shibd-debian \
	    debian/libapache2-mod-shib2.shibd.init

override_dh_install:
	mkdir -p debian/tmp/usr/lib/apache2/modules
	mv debian/tmp/usr/lib/*/shibboleth/mod_shib_24.so \
	    debian/tmp/usr/lib/apache2/modules/mod_shib2.so
	pod2man debian/man-pages/mdquery.pod --section 1		\
	    --center 'Shibboleth' --release $(VERSION)			\
	    debian/libapache2-mod-shib2/usr/share/man/man1/mdquery.1
	pod2man debian/man-pages/resolvertest.pod --section 1		  \
	    --center 'Shibboleth' --release $(VERSION)			  \
	    debian/libapache2-mod-shib2/usr/share/man/man1/resolvertest.1
	pod2man debian/man-pages/shib-metagen.pod --section 1		  \
	    --center 'Shibboleth' --release $(VERSION)			  \
	    debian/libapache2-mod-shib2/usr/share/man/man1/shib-metagen.1
	pod2man debian/man-pages/shib-keygen.pod --section 8		 \
	    --center 'Shibboleth' --release $(VERSION)			 \
	    debian/libapache2-mod-shib2/usr/share/man/man8/shib-keygen.8
	pod2man debian/man-pages/shibd.pod --section 8			\
	    --center 'Shibboleth' --release $(VERSION)			\
	    debian/libapache2-mod-shib2/usr/share/man/man8/shibd.8
	dh_install -s -i -X mod_shib2.so --fail-missing

override_dh_installchangelogs:
	dh_installchangelogs doc/RELEASE.txt

override_dh_installdocs:
	dh_installdocs -A doc/NOTICE.txt
	set -e; if [ -d "$(DOCS)" ] ; then			\
	    rm $(DOCS)/html/jquery.js ;				\
	    ln -s /usr/share/javascript/jquery/jquery.min.js	\
		$(DOCS)/html/jquery.js ;			\
	fi

override_dh_installinit:
	dh_installinit --name=shibd -- start 20 2 3 4 5 .

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

override_dh_builddeb:
	dh_builddeb -- -Zxz
