#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DH_OPTIONS

DEB_HOST_ARCH   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

export DEB_BUILD_HARDENING=1
export DEB_BUILD_HARDENING_PIE=0

CFLAGS = -Wl,-z,defs -Wl,--as-needed
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif


TRANG_VERSION ?= $(shell dpkg-query -W trang | cut -f 2)

%:
	dh --with quilt $@

override_dh_auto_clean:
	dh_auto_clean -Bbuild-sqlite3
	dh_auto_clean -Denforcer -Bbuild-mysql/enforcer
	rm -rf build-mysql build-sqlite3
	if test -f auditor/common/config.h.orig; then mv auditor/common/config.h.orig auditor/common/config.h; fi
	if test -f enforcer/common/config.h.orig; then mv enforcer/common/config.h.orig enforcer/common/config.h; fi
	if test -f signer/tools/config.h.orig; then mv signer/tools/config.h.orig signer/tools/config.h; fi

override_dh_auto_configure:
	autoreconf
	if test -f auditor/common/config.h; then mv auditor/common/config.h auditor/common/config.h.orig; fi
	if test -f enforcer/common/config.h; then mv enforcer/common/config.h enforcer/common/config.h.orig; fi
	if test -f signer/tools/config.h; then mv signer/tools/config.h signer/tools/config.h.orig; fi

	dh_auto_configure -Bbuild-sqlite3 -- --with-ruby=/usr/bin/ruby1.8 --with-ldns=/usr --with-database-backend=sqlite3 --with-sqlite3=/usr --libexecdir=/usr/lib/opendnssec/ --with-pkcs11-opensc=/usr/lib/pkcs11/opensc-pkcs11.so
	mkdir build-mysql
	ln -s ../build-sqlite3/libhsm build-mysql/libhsm
	dh_auto_configure -Bbuild-mysql/enforcer -- --with-ldns=/usr --with-database-backend=mysql --with-mysql=/usr --libexecdir=/usr/lib/opendnssec/ --disable-auditor --disable-signer --disable-doxygen-doc

	# Trang ends with bus error on kfreebsd-amd64, so we skip rebuilding at all
	# Only rebuild RNG files if we have recent trang
	#@if dpkg --compare-versions "$(TRANG_VERSION)" ge "20091111-1"; then \
	#  rm conf/*.rng; \
	#fi

override_dh_auto_build-arch:
	dh_auto_build -Bbuild-sqlite3
	dh_auto_build -Bbuild-mysql/enforcer

override_dh_auto_build-indep:
	dh_auto_build -Bbuild-sqlite3 -- docs

override_dh_auto_install:
	# Comment out example zone after regression testing
	(cd build-sqlite3; patch -p1 -i $(CURDIR)/debian/signconf.xml.patch)
	# Remove $Id tags + new line from xml file
	(cd build-sqlite3; for xml in conf/*.xml; do sed -ie '/<!-- $$Id.*/ N; /<!-- $$Id.*\n/ d;' $$xml; done)
	dh_auto_install -Bbuild-sqlite3 -- DESTDIR=$(CURDIR)/debian/tmp
	install -m 755 $(CURDIR)/enforcer/utils/migrate_keyshare_sqlite3.pl $(CURDIR)/debian/tmp/usr/share/opendnssec/
	dh_auto_install -Bbuild-mysql/enforcer -- DESTDIR=$(CURDIR)/debian/opendnssec-enforcer-mysql
	install -m 755 $(CURDIR)/enforcer/utils/migrate_keyshare_mysql.pl $(CURDIR)/debian/opendnssec-enforcer-mysql/usr/share/opendnssec/
	rm -rf $(CURDIR)/debian/opendnssec-enforcer-mysql/usr/share/man/ \
	       $(CURDIR)/debian/opendnssec-enforcer-mysql/var/run/opendnssec/ \
	       $(CURDIR)/debian/opendnssec-enforcer-mysql/usr/bin/ods-hsm* \
	       $(CURDIR)/debian/opendnssec-enforcer-mysql/usr/bin/ods-kasp2html \
	       $(CURDIR)/debian/opendnssec-enforcer-mysql/usr/sbin/ods-control \
	       $(CURDIR)/debian/opendnssec-enforcer-mysql/usr/share/opendnssec/*.rn? \
	       $(CURDIR)/debian/opendnssec-enforcer-mysql/usr/share/opendnssec/kasp2html.xsl \
	       $(CURDIR)/debian/opendnssec-enforcer-mysql/etc/opendnssec

override_dh_installdocs-arch:
	dh_installdocs -popendnssec-common -popendnssec -popendnssec-dbg-mysql -popendnssec-dbg-sqlite3 --link-doc=opendnssec-common
	dh_installdocs -popendnssec-enforcer -popendnssec-enforcer-sqlite3 -popendnssec-enforcer-mysql --link-doc=opendnssec-enforcer
	dh_installdocs -plibhsm-bin -popendnssec-auditor -popendnssec-signer -popendnssec-doc
	# Remove extra and unnecessary jquery.js library from generated docs (See #622147)
	for p in libhsm-bin opendnssec-signer opendnssec-enforcer; do \
	    rm -f $(CURDIR)/debian/opendnssec-doc/usr/share/doc/$$p/html/jquery.js; \
	done

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_strip:
	dh_strip -s --keep-debug
	install -d -m 755 $(CURDIR)/debian/opendnssec-dbg-sqlite3/usr/lib/debug
	install -d -m 755 $(CURDIR)/debian/opendnssec-dbg-mysql/usr/lib/debug
	for p in opendnssec-signer libhsm-bin; do \
	    cp -a $(CURDIR)/debian/$$p/usr/lib/debug $(CURDIR)/debian/opendnssec-dbg-sqlite3/usr/lib/; \
	    cp -a $(CURDIR)/debian/$$p/usr/lib/debug $(CURDIR)/debian/opendnssec-dbg-mysql/usr/lib/; \
	    rm -rf $(CURDIR)/debian/$$p/usr/lib/debug; \
	done
	cp -a $(CURDIR)/debian/opendnssec-enforcer-sqlite3/usr/lib/debug $(CURDIR)/debian/opendnssec-dbg-sqlite3/usr/lib/
	cp -a $(CURDIR)/debian/opendnssec-enforcer-mysql/usr/lib/debug $(CURDIR)/debian/opendnssec-dbg-mysql/usr/lib/
	rm -rf $(CURDIR)/debian/opendnssec-enforcer-sqlite3/usr/lib/debug
	rm -rf $(CURDIR)/debian/opendnssec-enforcer-mysql/usr/lib/debug
