#!/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

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/rustc/architecture.mk
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

export CARGO=/usr/share/cargo/bin/cargo
export CARGO_HOME=$(CURDIR)/debian/cargo_home
export CARGO_REGISTRY=$(CURDIR)/debian/cargo_registry
export DEB_CARGO_CRATE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)

# for pdns makefile
export RUSTC_TARGET_ARCH=$(DEB_HOST_RUST_TYPE)

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

CONFIGURE_ARGS =

# Workaround for #1078387 https://github.com/PowerDNS/pdns/issues/14084
CONFIGURE_ARGS += --with-libcrypto=/usr


%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean
	rm -f dnslabeltext.cc
	chmod +x mkpubsuffixcc || true
	rm -rf debian/cargo_registry

override_dh_auto_configure:
	$(CARGO) prepare-debian debian/cargo_registry --link-from-system
	echo> ./settings/rust/Cargo.lock
	PATH=debian/configure-helpers/:$$PATH dh_auto_configure -- \
		--sysconfdir=/etc/powerdns \
		--enable-systemd --with-systemd=$$(pkgconf --variable=systemdsystemunitdir systemd) \
		--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 \
		$(CONFIGURE_ARGS)
	cat config.log
	grep 'OpenSSL ECDSA: yes' config.log

override_dh_auto_install:
	dh_auto_install
	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
	install -d debian/pdns-recursor/usr/share/doc/pdns-recursor/examples
	mv debian/pdns-recursor/etc/powerdns/recursor.yml-dist debian/pdns-recursor/usr/share/doc/pdns-recursor/examples/
	install -m 644 -t debian/pdns-recursor/etc/powerdns debian/recursor.conf
	# workaround for static libs in non-default dir, see https://salsa.debian.org/rust-team/dh-cargo/-/issues/1
	ln -s ./settings/rust/target .
	sed -e '/rustc-link-lib=static=settings/d' -i target/*/release/build/settings*/output
	/usr/share/cargo/bin/dh-cargo-built-using pdns-recursor

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

override_dh_gencontrol:
	dh_gencontrol -- $(SUBSTVARS)
