#!/usr/bin/make -f

# Turn on all hardening flags, as we're a networked daemon.
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)"'

# Disable systemd integration on non-linux archs
ifeq ($(DEB_HOST_ARCH_OS),linux)
CONFIGURE_ARGS += --enable-systemd --with-systemd=/lib/systemd/system
else
CONFIGURE_ARGS += --disable-systemd
endif

# Avoid test failure in docker.
export PDNS_TEST_NO_IPV6=1

# Backends
backends := bind ldap lmdb pipe gmysql godbc gpgsql gsqlite3 geoip lua mydns remote random tinydns


%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean
	rm -f dnslabeltext.cc

override_dh_auto_configure:
	dh_auto_configure -- \
		--sysconfdir=/etc/powerdns \
		--with-dynmodules="$(backends)" \
		--with-modules="" \
		--enable-ixfrdist \
		--enable-tools \
		--without-protobuf \
		--enable-unit-tests \
		--enable-reproducible \
		--disable-silent-rules \
		$(CONFIGURE_ARGS)

# init script needs to be named pdns, not pdns-server. if no pdns-backend
# is installed, start will fail, so pass --no-start. backends will trigger
# a restart.
override_dh_installinit:
	dh_installinit --error-handler=initscript_error --no-start -ppdns-server --name=pdns
	dh_installinit --error-handler=initscript_error --no-start -ppdns-ixfrdist --name=ixfrdist

override_dh_installsystemd:
	dh_installsystemd --no-start -ppdns-server --name=pdns
	dh_installsystemd --no-start -ppdns-ixfrdist --name=ixfrdist

override_dh_install:
	dh_install
	./pdns/pdns_server --no-config --config | sed \
	  -e 's!# module-dir=.*!!' \
	  -e 's!# include-dir=.*!&\ninclude-dir=/etc/powerdns/pdns.d!' \
	  -e 's!# launch=.*!&\nlaunch=!' \
	  -e 's!# setgid=.*!setgid=pdns!' \
	  -e 's!# setuid=.*!setuid=pdns!' \
	  -e 's!# security-poll-suffix=.*!&\nsecurity-poll-suffix=!' \
	  > debian/pdns-server/etc/powerdns/pdns.conf

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	./debian/tests-source/run
	echo Skipping make test, as dependencies are missing.
	make check || (cat pdns/test-suite.log; false)
endif

override_dh_fixperms:
	dh_fixperms
	# these files often contain passwords.
	chmod 0600 debian/pdns-server/etc/powerdns/pdns.conf
