#!/usr/bin/make -f

export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

DEB_HOST_ARCH_OS=$(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_GNU_TYPE=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE=$(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS+=-Wall
CFLAGS+=-D_PATH_DHCLIENT_SCRIPT='"/sbin/dhclient-script"'
CFLAGS+=-D_PATH_DHCPD_CONF='"/etc/dhcp/dhcpd.conf"'
CFLAGS+=-D_PATH_DHCLIENT_CONF='"/etc/dhcp/dhclient.conf"'
CFLAGS+=-DNOMINUM

CONFFLAGS=--prefix=/usr \
          --sysconfdir=/etc/dhcp \
          --with-srv-lease-file=/var/lib/dhcp/dhcpd.leases \
          --with-srv6-lease-file=/var/lib/dhcp/dhcpd6.leases \
          --with-cli-lease-file=/var/lib/dhcp/dhclient.leases \
          --with-cli6-lease-file=/var/lib/dhcp/dhclient6.leases \

# cross-architecture building
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS=CC=$(DEB_HOST_GNU_TYPE)-gcc
else
CROSS=
endif

# hurd support
ifeq ($(DEB_HOST_ARCH_OS), hurd)
export DO_LPF=1
CONFFLAGS+=--enable-use-sockets
endif

%:
	dh $@ --parallel --with autoreconf

override_dh_auto_configure:

override_dh_auto_build:
	# extract and patch embedded bind
	cd bind && tar xf bind.tar.gz && \
	    cd bind-* && test -e ../../debian/bind-patches/series && \
	    QUILT_PATCHES=../../debian/bind-patches quilt push -a || true
	# ldap-enabled build
	test -f Makefile && $(MAKE) distclean || true
	./configure --with-ldap --with-ldapcrypto $(CONFFLAGS)
	$(MAKE) $(CROSS)
	mv server/dhcpd dhcpd
	# ldap-disabled build
	test -f Makefile && $(MAKE) distclean || true
	./configure $(CONFFLAGS)
	$(MAKE) $(CROSS)

override_dh_install:
	dh_install
	cp debian/dhclient-script.$(DEB_HOST_ARCH_OS) \
	    debian/isc-dhcp-client/sbin/dhclient-script
	cp debian/dhclient-script.$(DEB_HOST_ARCH_OS).udeb \
	    debian/isc-dhcp-client-udeb/sbin/dhclient-script
	cp debian/rfc3442-classless-routes.$(DEB_HOST_ARCH_OS) \
	    debian/isc-dhcp-client/etc/dhcp/dhclient-exit-hooks.d/rfc3442-classless-routes
	cp contrib/dhcp-lease-list.pl \
	    debian/isc-dhcp-server/usr/sbin/dhcp-lease-list

override_dh_strip:
	dh_strip --dbg-package=isc-dhcp-dbg

override_dh_clean:
	cd bind && rm -rf bind-* include lib bindvar.tmp *.log
	dh_clean -- dhcpd config.*
