#!/usr/bin/make -f

# g++ 4.5 and 4.6 fail to build this package on armel with an internal
# error: internal compiler error: in expand_expr_real_1, at expr.c:8532
DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq ($(DEB_BUILD_ARCH),armel)
    export CXX=g++-4.4
endif

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-$(VERSION)/schemas/WS-Trust.xsd
	mv shibboleth-$(VERSION) shibboleth-sp2_$(DEBVERS).orig
	tar cf shibboleth-sp2_$(DEBVERS).orig.tar \
	    shibboleth-sp2_$(DEBVERS).orig
	rm -rf shibboleth-sp2_$(DEBVERS).orig
	gzip -9 shibboleth-sp2_$(DEBVERS).orig.tar

override_dh_auto_configure:
	autoreconf -f -i
	dh_auto_configure -- --libexecdir=/usr/lib/shibboleth \
	    --localstatedir=/var --enable-apache-22 \
	    --with-apxs2=/usr/bin/apxs2 --disable-dependency-tracking \
	    --with-memcached $(SYSTEM)

override_dh_auto_clean:
	dh_auto_clean
	find . -name Makefile.in -print0 | xargs -0r rm

override_dh_auto_install:
	NOKEYGEN=1 dh_auto_install
	rm debian/tmp/usr/lib/shibboleth/*.la
	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:
	mv debian/tmp/usr/lib/shibboleth/mod_shib_22.so \
	    debian/libapache2-mod-shib2/usr/lib/apache2/modules
	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 --fail-missing

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

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

override_dh_makeshlibs:
	dh_makeshlibs -Xusr/lib/shibboleth

%:
	dh $@
