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

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


# The IGTF installation bundle installs all profiles in one go,
# but we'll split them up in separate packages. Most of the metadata
# goes to /usr/share/igtf-policy/<profile>, but the certs themselves
# are put in /usr/share/ca-certificates/igtf-<profile> because the
# ca-certificates package only uses real files. The config, postinst,
# templates and prerm for the accredited profiles are so alike that
# they are generated in-line.


# the root of the installation tree.
BASE = /usr/share/igtf-policy

PROFILES = classic mics slcs iota

%:
	dh $@ 

# Include /etc/grid-security/certificates in each of
# the profile packages, so dpkg will remove them cleanly
# when the packages are uninstalled.
override_dh_installdirs:
	cd debian && for profile in $(PROFILES); do \
	    cp dirs.in igtf-policy-$$profile.dirs ; \
	done
	dh_installdirs

override_dh_auto_configure:
	./configure --prefix=`pwd`/debian/tmp/$(BASE) \
	--with-profile=classic \
	--with-profile=slcs \
	--with-profile=mics \
	--with-profile=iota \
	--with-profile=unaccredited \
	--with-profile=experimental \
	--with-install="install -m 644"


override_dh_auto_install: profile_files
	for profile in $(PROFILES) unaccredited experimental; do \
	    mkdir -p debian/tmp/$(BASE)/$$profile ; \
	    mkdir -p debian/tmp/usr/share/ca-certificates/igtf-$$profile ; \
	    make install-$$profile prefix=`pwd`/debian/tmp/$(BASE)/$$profile ; \
	    for i in debian/tmp/usr/share/igtf-policy/$$profile/*.pem ; do \
	        j=/usr/share/ca-certificates/igtf-$$profile/`basename $${i%.pem}`.crt ; \
	        mv $$i debian/tmp/$$j ; \
	        ln -s  $$j $$i ; \
	    done ; \
	done

profile_files:
	po2debconf debian/templates.in > debian/templates-translated.in
	cd debian && for profile in $(PROFILES); do \
	    sed -e "s/@PROFILE@/$$profile/g" < config.in > igtf-policy-$$profile.config ; \
	    sed -e "s/@PROFILE@/$$profile/g" < postinst.in > igtf-policy-$$profile.postinst ; \
	    sed -e "s/@PROFILE@/$$profile/g" < prerm.in > igtf-policy-$$profile.prerm ; \
	    sed -e "s/@PROFILE@/$$profile/g" < templates-translated.in > igtf-policy-$$profile.templates ; \
	done
	cd debian && for profile in $(PROFILES) unaccredited experimental; do \
	    sed -e "s/@PROFILE@/$$profile/g" < install.in > igtf-policy-$$profile.install ; \
	    cp docs.in igtf-policy-$$profile.docs ; \
	done

# The .info files aren't GNU Info files
override_dh_installinfo:

override_dh_installchangelogs:
	dh_installchangelogs CHANGES
