#!/usr/bin/make -f
# Made with the iad of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Also some stuff taken from debmake scripts, by Cristopt Lameter.

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

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)

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS=CC=$(DEB_HOST_GNU_TYPE)-gcc
else
CROSS=
endif

export DEB_BUILD_HARDENING=1
DPKG_EXPORT_BUILDFLAGS = 1

DESTDIR = `pwd`/debian/tmp

PACKAGE = isc-dhcp

-include /usr/share/dpkg/buildflags.mk

INSTALL = install
INSTALL_FILE 	= $(INSTALL) -p -o root -g root -m 644

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
CFLAGS += -Wno-error=unused-but-set-variable
export CFLAGS


bind-stamp:
	dh_testdir
	cd bind && tar xf bind.tar.gz && cd bind-* && \
	    QUILT_PATCHES=../../debian/bind-patches quilt push -a
	cd bind/bind-* && \
	for f in $$(find -name config.guess); do \
	  cp /usr/share/misc/config.* $$(dirname $$f)/.; \
	done
	touch $@

build-ldap-stamp: bind-stamp
	dh_testdir

	test ! -f Makefile || $(MAKE) distclean
	./configure \
		--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 \
		--with-ldap \
		--with-ldapcrypto \
		--enable-paranoia
	$(MAKE) $(CROSS)

	mkdir -p isc-dhcp-server-ldap
	mv server/dhcpd isc-dhcp-server-ldap/
	
	touch $@
	
build: build-arch build-indep

build-arch: build-ldap-stamp build-non-ldap-stamp build-arch-stamp
build-indep:

build-arch-stamp:
	dh_testdir
	touch $@

build-non-ldap-stamp: bind-stamp
	dh_testdir

	test ! -f Makefile || $(MAKE) distclean
	./configure \
		--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 \
		--enable-paranoia
	$(MAKE) $(CROSS)

	touch $@

clean:
	dh_testdir
	rm -f build*stamp install*stamp bind-stamp
	rm -rf isc-dhcp-server-ldap

	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f Makefile client/Makefile config.log config.status
	cd bind && rm -rf bind-* include lib bindvar.tmp \
	    build.log configure.log install.log

	debconf-updatepo
	dh_clean

install: install-stamp
install-stamp: build-ldap-stamp build-non-ldap-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -A

	# Add here commands to install the package into debian/tmp.
	$(MAKE) install DESTDIR=$(DESTDIR)

	mkdir -p $(DESTDIR)/etc/dhcp

	# Install apparmor profile
	mkdir -p $(DESTDIR)/etc/apparmor.d
	mkdir -p $(DESTDIR)/etc/apparmor.d/dhcpd.d
	$(INSTALL_FILE) debian/apparmor-profile.dhcpd $(DESTDIR)/etc/apparmor.d/usr.sbin.dhcpd
	$(INSTALL_FILE) debian/apparmor-profile.dhclient $(DESTDIR)/etc/apparmor.d/sbin.dhclient

	# Install dhcp's conffile.
	$(INSTALL_FILE) -m 644 debian/dhcpd.conf $(DESTDIR)/etc/dhcp

	# Install dhcp-client's conffiles.
	$(INSTALL_FILE) -m 644 debian/dhclient.conf $(DESTDIR)/etc/dhcp

	# udeb needs simplified dhclient script
	$(INSTALL_FILE) -m 755 `pwd`/debian/dhclient-script.$(DEB_HOST_ARCH_OS).udeb \
		`pwd`/debian/isc-dhcp-client-udeb/sbin/dhclient-script

	# Weird, weird Japanese manpages in weird, weird locations
	# need to be special-cased...
	mkdir -p $(DESTDIR)/usr/share/man/ja/man5
	for f in dhclient.conf dhcp-eval dhclient.leases dhcp-options; do \
		cp doc/ja_JP.eucJP/$$f.5 \
			$(DESTDIR)/usr/share/man/ja/man5; \
	done

	mkdir -p $(DESTDIR)/usr/share/man/ja/man8
	for f in dhclient dhclient-script; do \
		cp doc/ja_JP.eucJP/$$f.8 \
			$(DESTDIR)/usr/share/man/ja/man8; \
	done

	cp debian/debug-hook debian/isc-dhcp-client/etc/dhcp/dhclient-enter-hooks.d/debug
	cp debian/debug-hook debian/isc-dhcp-client/etc/dhcp/dhclient-exit-hooks.d/debug

	dh_install
	dh_link

	cp debian/dhclient-script.$(DEB_HOST_ARCH_OS) `pwd`/debian/isc-dhcp-client/sbin/dhclient-script
	cp debian/rfc3442-classless-routes.$(DEB_HOST_ARCH_OS) \
		`pwd`/debian/isc-dhcp-client/etc/dhcp/dhclient-exit-hooks.d/rfc3442-classless-routes

	# Install Linux specific documentation
ifeq ($(DEB_HOST_ARCH_OS), linux)
	for p in isc-dhcp-client isc-dhcp-relay isc-dhcp-server ; do \
		install -d -m 755 `pwd`/debian/$p/usr/share/doc; \
		install -m 644 `pwd`/debian/dhcp-on-linux.txt `pwd`/debian/$p/usr/share/doc; \
	done
endif
	dh_lintian

	touch $@

# Build architecture-dependent files here (this package does not contain
#	architecture-independent files).
binary-arch: build-arch install
	dh_testdir
	dh_testroot 
	dh_installdebconf
	dh_installdocs -A debian/README.Debian -X doc/ja_JP.eucJP
	dh_installexamples -a
	dh_installinit -pisc-dhcp-server --name isc-dhcp-server
	dh_installinit -pisc-dhcp-server --name isc-dhcp-server6
	dh_installinit -pisc-dhcp-relay --name isc-dhcp-relay
	dh_installinit -pisc-dhcp-relay --name isc-dhcp-relay6
	dh_installchangelogs
	dh_install -pisc-dhcp-server-ldap	
	dh_apparmor -pisc-dhcp-client --profile-name=sbin.dhclient
	dh_apparmor -pisc-dhcp-server --profile-name=usr.sbin.dhcpd
	dh_apport -a
	dh_strip -pisc-dhcp-server-ldap 
	dh_strip -pisc-dhcp-server --dbg-package=isc-dhcp-server-dbg
	dh_strip -pisc-dhcp-relay --dbg-package=isc-dhcp-relay-dbg
	dh_strip -pisc-dhcp-client --dbg-package=isc-dhcp-client-dbg
	dh_strip -pisc-dhcp-common
	dh_strip -pisc-dhcp-client-udeb 
	dh_compress
	dh_fixperms -a
	dh_installdeb
	dh_shlibdeps -a
	dh_gencontrol
	dh_md5sums -a --no-package=isc-dhcp-client-udeb
	dh_builddeb

binary-indep: build-indep

binary: binary-arch binary-indep

.PHONY: build clean binary-indep binary-arch binary
