#!/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

export DH_COMPAT=4

DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

DESTDIR = `pwd`/debian/tmp

PACKAGE = dhcp3

include /usr/share/dpatch/dpatch.make

CFLAGS = -Wall -g
INSTALL = install
INSTALL_FILE 	= $(INSTALL) -p -o root -g root -m 644
INSTALL_PROGRAM = $(INSTALL) -p -o root -g root -m 755
INSTALL_SCRIPT 	= $(INSTALL) -p -o root -g root -m 755

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

IVARS = DESTDIR=$(DESTDIR)

BVARS = PREDEFINES='-D_PATH_DHCPD_DB=\"/var/lib/dhcp3/dhcpd.leases\" \
	-D_PATH_DHCLIENT_DB=\"/var/lib/dhcp3/dhclient.leases\" \
	-D_PATH_DHCLIENT_SCRIPT=\"/sbin/dhclient-script\" \
	-D_PATH_DHCPD_CONF=\"/etc/dhcp3/dhcpd.conf\" \
        -D_PATH_DHCLIENT_CONF=\"/etc/dhcp3/dhclient.conf\"'


#patch: patch-stamp
#patch-stamp:
#	dh_testdir
#	if [ ! -f patch-stamp ]; then /bin/sh debian/scripts/patch-source; fi
#	touch patch-stamp
#
#unpatch:
#	dh_testdir
#	if [ -f patch-stamp ]; then /bin/sh debian/scripts/unpatch-source; fi
#	rm -f patch-stamp

build: patch-stamp build-stamp
build-stamp:
	dh_testdir

	./configure 
	#CFLAGS="$(CFLAGS)" sh -x debian/scripts/insert_cflags.sh patch
	#cat site.conf
	$(MAKE) $(BVARS) 

	cp debian/dhclient-script.$(DEB_HOST_ARCH_OS) client/scripts/debian

	touch build-stamp

clean: unpatch
	dh_testdir
	rm -f build-stamp install-stamp

	#CFLAGS="$(CFLAGS)" sh -x debian/scripts/insert_cflags.sh unpatch

	# Add here commands to clean up after the build process.
	-$(MAKE) distclean

	# Remove leftover junk...
	rm -Rf work.*/

	debconf-updatepo
	dh_clean

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -A

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


	mkdir -p $(DESTDIR)/etc/dhcp3

	#mv $(DESTDIR)/etc/dhclient-script $(DESTDIR)/sbin

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

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

	# install the udeb binary
	$(INSTALL_PROGRAM) -m 755 $(DESTDIR)/sbin/dhclient \
		`pwd`/debian/dhcp3-client-udeb/sbin/

	# udeb needs the os-specific script too
	cp `pwd`/debian/tmp/sbin/dhclient-script \
		`pwd`/debian/dhcp3-client-udeb/sbin

	# Rename binaries so they do not conflict with the binaries in the
	#	v2 packages.
	for f in /usr/sbin/dhcpd /usr/sbin/dhcrelay /sbin/dhclient; do \
		mv $(DESTDIR)$$f $(DESTDIR)$${f}3; \
	done

	# Rename man page so the have the same name as the binaries.
	for f in dhcpd dhcrelay dhclient; do \
		mv $(DESTDIR)/usr/share/man/man8/$$f.8 \
			$(DESTDIR)/usr/share/man/man8/$${f}3.8; \
	done

	# 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-enter debian/dhcp3-client/etc/dhcp3/dhclient-enter-hooks.d/debug
	cp debian/debug-exit debian/dhcp3-client/etc/dhcp3/dhclient-exit-hooks.d/debug

	dh_movefiles
	# Create symlink so we don't break ifupdown
	dh_link -a

	# Remove unwanted directories that dh_movefiles leaves around
	rmdir $(DESTDIR)/sbin/
	rmdir $(DESTDIR)/usr/lib/
	rm -Rf $(DESTDIR)/usr/include/

	# Install Linux specific documentation
ifeq ($(DEB_HOST_ARCH_OS), linux)
	for p in dhcp3-client dhcp3-relay dhcp3-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
	$(INSTALL_FILE) debian/dhcp3-client.lintian \
		debian/dhcp3-client/usr/share/lintian/overrides/dhcp3-client

	touch install-stamp

UDEBPACKAGE=dhcp3-client-udeb
VERSION=$(shell dpkg-parsechangelog | grep ^Version:.* | cut -d ' ' -f 2)
ARCH=$(shell dpkg --print-architecture)
#UDEBFILENAME=$(UDEBPACKAGE)_$(VERSION)_$(ARCH).udeb
PRIORITY=$(shell grep ^Priority: debian/control | cut -d ' ' -f 2)

# Build architecture-dependent files here (this package does not contain
#	architecture-independent files).
binary-arch: build install
#	dh_testversion
	dh_testdir -a
	dh_testroot -a
	dh_installdebconf
	dh_installdocs -a -A debian/README.Debian -X doc/ja_JP.eucJP
	dh_installexamples -a
#	dh_installmenu -a
#	dh_installemacsen -a
	dh_installinit -a -n
#	dh_installcron -a
#	dh_installmanpages -a
#	dh_undocumented
	dh_installchangelogs -a 
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a 
	dh_shlibdeps -a
	dh_gencontrol -a 
#	dh_makeshlibs -a
	dh_md5sums -a --no-package=dhcp3-client-udeb
	dh_builddeb -a 

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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