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

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

# enable dpkg build flags
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

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-gost --enable-gost-anyway --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 $@

override_dh_auto_configure:
	PYTHON_VERSION=${PYVER} dh_auto_configure -- $(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} all doc
	for pyvers in $(PYVERS); do \
		${MAKE} -C build/python-$$pyvers pyldns;\
		done

override_dh_auto_install:
	${MAKE} 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:
# upstream Makefile (as of 1.8.1) in distclean does *uninstall*
# (which will try removing things from running system if DESTDIR= isn't given)
# And generally does not work at all, removing libtool before using it.
# Use realclean and remove remnaints which are not removed.
# Note upstream removes the doxygen-generated manpages [real]clean too, the
# ones shipped in the upstream tarball, and a few surrounding files too,
# so the cleaned up source wont be the same anyway.
# These files are (re)generated during build, though.
	[ ! -f Makefile ] || ${MAKE} realclean
	rm -rf .libs/
	rm -f _ldns.la ldns_wrapper.*o linktest.*o
# remove the python-variant subdirs if any
	rm -rf build/

override_dh_auto_test:
# tests in Makefile.in are not distributed
