#!/usr/bin/make -f

# Vendor and version
version := $(shell dpkg-parsechangelog -SVersion).$(shell dpkg-vendor --query Vendor)
CXXFLAGS += -DPACKAGEVERSION='"$(version)"'

# for atomic support on mips(el)
LDFLAGS += -latomic

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

DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Use new build system
%:
	dh $@ --with autoreconf --parallel

override_dh_auto_configure:
	mv pdns/dnslabeltext.cc debian/dnslabeltext.cc.moved
	./configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--sysconfdir=/etc/powerdns \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--libdir='$${prefix}/lib/$(DEB_HOST_MULTIARCH)' \
		--libexecdir='$${prefix}/lib' \
		--with-dynmodules="$(backends)" \
		--with-modules="" \
		--with-pgsql-includes=`pg_config --includedir` \
		--disable-cryptopp \
		--enable-botan1.10 \
		--enable-tools \
		--enable-unit-tests \
		--enable-reproducible

override_dh_strip:
	dh_strip --ddeb-migration='pdns-server-dbg (<< 4.0.0-0)'

# Additional permissions
override_dh_fixperms:
	dh_fixperms
	chmod 755 debian/pdns-server/etc/resolvconf/update.d/pdns

# init script needs to be named pdns, not pdns-server
override_dh_installinit:
	dh_systemd_enable --name=pdns
	dh_installinit --name=pdns
	dh_systemd_start --restart-after-upgrade

override_dh_install:
	make -C pdns sdig dnsgram
	install -c pdns/sdig debian/tmp/usr/bin/sdig
	install -c pdns/dnsgram debian/tmp/usr/bin/dnsgram
	dh_install
	./pdns/pdns_server --no-config --config | sed \
		-e 's!# config-dir=.*!config-dir=/etc/powerdns!' \
		-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/usr/share/pdns-server/pdns.conf

# Verbose build (shows used compiler/linker and their flags)
override_dh_auto_build-arch:
	dh_auto_build -- V=1

# Verbose tests (shows used compiler/linker and their flags)
override_dh_auto_test:
	echo Skipping make test, as dependencies are missing.
	#dh_auto_test -- V=1

# restore moved files
override_dh_clean:
	test -f debian/dnslabeltext.cc.moved && mv debian/dnslabeltext.cc.moved pdns/dnslabeltext.cc || true
	dh_clean
