#!/usr/bin/make -f

# Turn on all hardening flags, as we're a networked daemon.
# Note: blhc (build log hardening check) will find these false positives: CPPFLAGS 2 missing, LDFLAGS 1 missing
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

# Vendor and version
CXXFLAGS += -DPACKAGEVERSION='"$(DEB_VERSION).$(DEB_VENDOR)"'


%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean
	rm -f dnslabeltext.cc
	chmod +x mkpubsuffixcc || true

override_dh_auto_configure:
	PATH=debian/configure-helpers/:$$PATH dh_auto_configure -- \
		--sysconfdir=/etc/powerdns \
		--enable-systemd --with-systemd=/lib/systemd/system \
		--enable-reproducible \
		--disable-silent-rules \
		--enable-unit-tests \
		--with-service-user=pdns \
		--with-service-group=pdns \
		--with-libcap \
		--with-libsodium \
		--with-lua \
		--with-net-snmp \
		--with-protobuf=yes \
		--enable-dns-over-tls \
		--enable-dnstap

override_dh_auto_install:
	dh_auto_install
	install -d debian/pdns-recursor/usr/share/pdns-recursor/lua-config
	install -m 644 -t debian/pdns-recursor/usr/share/pdns-recursor/lua-config debian/lua-config/rootkeys.lua
	install -m 644 -t debian/pdns-recursor/etc/powerdns debian/recursor.lua
	install -d debian/pdns-recursor/usr/share/pdns-recursor/snmp
	install -m 644 -t debian/pdns-recursor/usr/share/pdns-recursor/snmp RECURSOR-MIB.txt
	rm -f debian/pdns-recursor/etc/powerdns/recursor.conf-dist
	./pdns_recursor --no-config --config=default | sed \
		-e 's!^# config-dir=.*!config-dir=/etc/powerdns!' \
		-e 's!^# hint-file=.*!&\nhint-file=/usr/share/dns/root.hints!' \
		-e 's!^# include-dir=.*!&\ninclude-dir=/etc/powerdns/recursor.d!' \
		-e 's!^# local-address=.*!local-address=127.0.0.1!' \
		-e 's!^# lua-config-file=.*!lua-config-file=/etc/powerdns/recursor.lua!' \
		-e 's!^# public-suffix-list-file=.*!public-suffix-list-file=/usr/share/publicsuffix/public_suffix_list.dat!' \
		-e 's!^# quiet=.*!quiet=yes!' \
		-e 's!^# security-poll-suffix=.*!&\nsecurity-poll-suffix=!' \
		-e '/^# version-string=.*/d' \
		> debian/pdns-recursor/etc/powerdns/recursor.conf

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test
	-cat testrunner.log
endif

override_dh_gencontrol:
	dh_gencontrol -- $(SUBSTVARS)
