#!/usr/bin/make -f

export DH_VERBOSE=1

DEBVER := $(shell dpkg-parsechangelog -S Version)

%:
	dh $@

override_dh_auto_configure:
	cd igtf-bundle && ./configure \
	  --prefix="$(CURDIR)/debian/tmp/usr/share/osg-ca-certs" \
	  --with-install="install -m 644" \
	  --with-profile=classic \
	  --with-profile=mics \
	  --with-profile=slcs \
	  --with-profile=iota

override_dh_auto_build:
	cd igtf-bundle && make
	cd letsencrypt-ca && make

override_dh_auto_test:
	cd letsencrypt-ca && make check

override_dh_auto_install:
	cd igtf-bundle && make install
	cd letsencrypt-ca && \
	  cp -d -v *.pem \
	  "$(CURDIR)/debian/tmp/usr/share/osg-ca-certs"
	cd letsencrypt-ca && \
	  cp -d -v *.[0-9] *.signing_policy *.crl_url \
	  "$(CURDIR)/debian/tmp/usr/share/osg-ca-certs"
	cd debian/tmp/usr/share/osg-ca-certs && \
	  sha256sum *.[0-9] *.pem \
	  > "$(CURDIR)/debian/tmp/cacerts_sha256sum.txt"
	cd debian/tmp/usr/share/osg-ca-certs && \
	  ls | \
	  { \
	    while read name; do \
	      if [ -e "$$name" ]; then \
	        file_name="`readlink \"$$name\"`" || true ; \
	        if [ -n "$$file_name" ]; then \
	          echo "$$name/$${file_name##*/}" ; \
	        else  \
	          echo "$$name/$$name" ; \
	        fi ; \
	      else  \
	        echo "$$0: error: missing \`$$name'" >&2 ; \
	        exit 1 ; \
	      fi ; \
	    done \
	  } \
	  | LC_ALL=C sort \
	  > "$(CURDIR)/debian/tmp/cert-files-$(DEBVER)"

override_dh_installdeb:
	dh_installdeb \
	  --define CERTSDIR="/etc/grid-security/certificates" \
	  --define PKGHOME="/usr/share/osg-ca-certs" \
	  --define CERTFILES="cert-files-$(DEBVER)"

override_dh_auto_clean:
	cd letsencrypt-ca && make clean
	cd letsencrypt-certificates && ls | \
	  xargs -II rm -v -r -f ../letsencrypt-ca/I
	rm -v -f igtf-bundle/Makefile
