#!/usr/bin/make -f
#export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk

ifneq ($(DEB_HOST_ARCH), amd64)
CONFIGURE_ARGS = --disable-flto
endif

ifneq ($(DEB_HOST_ARCH_OS), linux)
CONFIGURE_ARGS = --with-libbsd
endif

LIBRARY = libunbound2
DOPACKAGES = $(shell dh_listpackages)

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

clean:
	dh_autotools-dev_restoreconfig
	dh_autoreconf_clean
	dh clean

build:
build-arch:
build-indep:

binary-arch: build
	dh_testdir
	dh_autoreconf
	dh_autotools-dev_updateconfig

ifneq (,$(filter unbound unbound-anchor unbound-host,$(DOPACKAGES)))
	# first build -- build unbound daemon
	PYTHON_VERSION="$(shell py3versions -vd)" \
	CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="-Wl,--as-needed $(LDFLAGS)" \
		dh_auto_configure -- \
		--disable-rpath \
		--with-pidfile=/run/unbound.pid \
		--with-rootkey-file=/var/lib/unbound/root.key \
		--with-libevent \
		--with-pythonmodule \
		--enable-subnet \
		--enable-dnstap \
		--enable-systemd \
		--with-dnstap-socket-path=/run/dnstap.sock \
		--libdir=/usr/lib \
		$(CONFIGURE_ARGS)
	$(MAKE)
	$(MAKE) install DESTDIR="$(CURDIR)/debian/tmp"
	$(MAKE) clean
endif

	# second build -- build libunbound only, against nettle
	CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="-Wl,--as-needed $(LDFLAGS)" \
		dh_auto_configure -- \
		--disable-rpath \
		--with-libunbound-only \
		--with-nettle \
		--with-rootkey-file=/var/lib/unbound/root.key \
		--with-libevent \
		--without-pythonmodule \
		--without-pyunbound \
		--enable-event-api \
		$(CONFIGURE_ARGS)
	$(MAKE)
	$(MAKE) install DESTDIR="$(CURDIR)/debian/tmp-lib"
	install -D -m 0644 contrib/libunbound.pc \
		$(CURDIR)/debian/libunbound-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/libunbound.pc
	$(MAKE) clean

ifneq (,$(filter python-unbound,$(DOPACKAGES)))
	# third build - pyunbound for Python 2
	PYTHON_VERSION="$(shell pyversions -vd)" \
	CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="-Wl,--as-needed $(LDFLAGS)" \
		dh_auto_configure -- \
		--disable-rpath \
		--with-pythonmodule \
		--with-pyunbound \
		$(CONFIGURE_ARGS)
	$(MAKE) _unbound.la
	install -D -m 0644 .libs/_unbound.so \
		debian/python-unbound/usr/lib/$(shell pyversions -d)/dist-packages/_unbound.so
	install -m 0644 \
		pythonmod/unboundmodule.py \
		libunbound/python/unbound.py \
		debian/python-unbound/usr/lib/$(shell pyversions -d)/dist-packages
	$(MAKE) clean
endif

ifneq (,$(filter python3-unbound,$(DOPACKAGES)))
	# fourth build - pyunbound for Python 3
	PYTHON_VERSION="$(shell py3versions -vd)" \
	CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="-Wl,--as-needed $(LDFLAGS)" \
		dh_auto_configure -- \
		--disable-rpath \
		--with-pythonmodule \
		--with-pyunbound \
		$(CONFIGURE_ARGS)
	$(MAKE) _unbound.la
	install -D -m 0644 .libs/_unbound.so \
		debian/python3-unbound/usr/lib/$(shell py3versions -d)/dist-packages/_unbound.so
	install -m 0644 \
		pythonmod/unboundmodule.py \
		libunbound/python/unbound.py \
		debian/python3-unbound/usr/lib/$(shell py3versions -d)/dist-packages
	$(MAKE) clean
endif

	dh_installdirs

ifneq (,$(filter unbound unbound-anchor unbound-host,$(DOPACKAGES)))
	dh_systemd_enable -p unbound
	dh_systemd_enable -p unbound --name=unbound-resolvconf
	dh_systemd_start -p unbound unbound.service

	dh_installinit --error-handler=true --restart-after-upgrade

	echo '$$named unbound' > debian/unbound/etc/insserv.conf.d/unbound
	install -m 0644 debian/resolvconf debian/unbound/etc/resolvconf/update.d/unbound
	install -m 0755 debian/resolvconf-package debian/unbound/usr/lib/resolvconf/dpkg-event.d/unbound
	install -m 0644 doc/example.conf debian/unbound/usr/share/doc/unbound/examples/unbound.conf
	install -m 0644 contrib/update-anchor.sh debian/unbound/usr/share/doc/unbound/contrib
	install -D -m 0755 contrib/unbound_munin_ debian/unbound/usr/share/munin/plugins/unbound_munin_
endif

	mkdir -p debian/libunbound-dev/usr/lib/$(DEB_HOST_MULTIARCH)
	mv \
		debian/tmp-lib/usr/lib/$(DEB_HOST_MULTIARCH)/libunbound.a \
		debian/tmp-lib/usr/lib/$(DEB_HOST_MULTIARCH)/libunbound.so \
		debian/libunbound-dev/usr/lib/$(DEB_HOST_MULTIARCH)

	mkdir -p debian/$(LIBRARY)/usr/lib/$(DEB_HOST_MULTIARCH)
	mv \
		debian/tmp-lib/usr/lib/$(DEB_HOST_MULTIARCH)/*.so.* \
		debian/$(LIBRARY)/usr/lib/$(DEB_HOST_MULTIARCH)
	chmod 0644 debian/$(LIBRARY)/usr/lib/$(DEB_HOST_MULTIARCH)/*

	dh_install
	dh_installchangelogs
	dh_installdocs
	dh_installman
ifneq (,$(filter python-unbound,$(DOPACKAGES)))
	dh_python2 --no-guessing-versions
endif
ifneq (,$(filter python3-unbound,$(DOPACKAGES)))
	dh_python3
endif
	dh_strip
	dh_compress -Xusr/share/doc/unbound/examples/unbound.conf

ifneq (,$(filter unbound unbound-anchor unbound-host,$(DOPACKAGES)))
	mkdir -p debian/unbound/etc/apparmor.d
	cp debian/apparmor-profile debian/unbound/etc/apparmor.d/usr.sbin.unbound
	dh_apparmor --profile-name=usr.sbin.unbound -punbound
endif

	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep:
binary: binary-arch binary-indep
