#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DPKG_GENSYMBOLS_CHECK_LEVEL=4

# the main python version (single)
PYVER=$(shell py3versions -vd)
# extra python versions to build if any (plural)
# To enable multiple pyversions:
#   Build-Depends: python3-all-dev vs python3-dev in d/control.
#PYVERS=$(filter-out ${PYVER},$(shell py3versions -vs))
# This does not (yet) work for different python versions, b/c each next install
# rule below overrides python module files installed by the previous install,
# so we end up with just the last module.  Also, the upstream build system has
# bugs building pyldns in a subdir.
# We can do a loop over $PYVERS in build target and run make _ldns.la in turn,
# with all python-related definitions taken directly from python$vers-config,
# removing ldns_wrapper.*o before each step, and saving resulting _ldns.la
# under a version-specific name.  But this is too ugly already. Upstream should
# grow up a real module description file like setup.py and whole thing will be
# built automatically from there instead of using all the wrong custom
# commands, and it will automatically build things for multiple versions too,
# so we'll no need to override many targets in there.
# For now, build only for a single pyversion is supported.
PYVERS=

CONFIGFLAGS=--disable-rpath --enable-ed25519 --disable-ldns-config \
	--with-trust-anchor=/usr/share/dns/root.key

build-arch build-indep build \
install-indep install-arch install \
binary-arch binary-indep binary \
clean \
: %:
	dh $@
.PHONY: build-arch build-indep build \
install-indep install-arch install \
binary-arch binary-indep binary \
clean

override_dh_auto_configure:
	PYTHON_VERSION=${PYVER} dh_auto_configure -B build/main -- $(CONFIGFLAGS) --with-examples --with-drill --with-pyldns
	for pyvers in $(PYVERS); do \
		PYTHON_VERSION=$$pyvers dh_auto_configure -B build/python-$$pyvers -- $(CONFIGFLAGS) --with-pyldns; \
		done

override_dh_auto_build:
	${MAKE} -C build/main all doc
	for pyvers in $(PYVERS); do \
		${MAKE} -C build/python-$$pyvers pyldns;\
		done

override_dh_auto_install:
	${MAKE} -C build/main DESTDIR=${CURDIR}/debian/tmp install install-doc
	for pyvers in $(PYVERS); do \
		${MAKE} DESTDIR=${CURDIR}/debian/tmp -C build/python-$$pyvers install-pyldns install-pyldnsx;\
		done
	# upstream wrongly builds static library for the python module (.a & .la)
	rm -fv debian/tmp/usr/lib/python3/dist-packages/_ldns.*a

override_dh_auto_clean:
	rm -rf build/
	# generated even when doing build in a separate directory:
	rm -f contrib/python/ldns.py contrib/python/ldns_wrapper.c

override_dh_auto_test:
# tests in Makefile.in are not distributed
