#!/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_BUILD_GNU_TYPE := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

DESTDIR = `pwd`/debian/tmp

PACKAGE = vyatta-dhcp3

CFLAGS = -Wall -g -D_GNU_SOURCE

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

# Use hardening options
export DEB_BUILD_HARDENING=1

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

CFLAGS += -D_PATH_DHCPD_DB=\"/var/lib/dhcp3/dhcpd.leases\"
CFLAGS += -D_PATH_DHCPD6_DB=\"/var/lib/dhcp3/dhcp6.leases\"
CFLAGS += -D_PATH_DHCLIENT_DB=\"/var/lib/dhcp3/dhclient.leases\"
CFLAGS += -D_PATH_DHCLIENT6_DB=\"/var/lib/dhcp3/dhclient6.leases\"
CFLAGS += -D_PATH_DHCLIENT_SCRIPT=\"/sbin/dhclient-script\"
CFLAGS += -D_PATH_DHCPD_CONF=\"/etc/dhcp3/dhcpd.conf\"
CFLAGS += -D_PATH_DHCLIENT_CONF=\"/etc/dhcp3/dhclient.conf\"


ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

IVARS = DESTDIR=$(DESTDIR)

configure: configure.ac Makefile.am
	./debian/autogen.sh

config.status: configure
	dh_testdir
	./configure --prefix=/usr --build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_HOST_GNU_TYPE)

build: build-stamp

build-stamp: config.status
	$(MAKE)
	touch build-stamp

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

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

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

	# was doing a debconf-updatepo here, which doesn't make sense. po is
	# supposed to be updated by developers when template changes.
	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 dhclient-script file.
	$(INSTALL_FILE) -m 644 debian/dhclient-script.linux $(DESTDIR)/usr/sbin/dhclient-script

	# Install dhcp-client's conffiles.
# Do not install default 'dhclient.conf' as this is generated by the Vyatta config system
#	$(INSTALL_FILE) -m 644 debian/dhclient.conf $(DESTDIR)/etc/dhcp3

	# Rename binaries so they do not conflict with the binaries in the
	#	v2 packages.
	for f in /usr/sbin/dhcpd /usr/sbin/dhcrelay /usr/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/vyatta-dhcp3-client/etc/dhcp3/dhclient-enter-hooks.d/debug
	cp debian/debug-exit debian/vyatta-dhcp3-client/etc/dhcp3/dhclient-exit-hooks.d/debug

	mkdir -p $(DESTDIR)/sbin
	mv $(DESTDIR)/usr/sbin/*dhclient* $(DESTDIR)/sbin

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

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

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

	touch install-stamp

VERSION=$(shell dpkg-parsechangelog | grep ^Version:.* | cut -d ' ' -f 2)
ARCH=$(shell dpkg --print-architecture)
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
	rm -f debian/files
#	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_lintian -a
	dh_installdeb -a 
	dh_shlibdeps -a
	rm -f debian/*/DEBIAN/conffiles
	if [ -n "$$UBNT_DEV_PKG_VERSION" ]; then \
		dh_gencontrol -a -- "-v$$UBNT_DEV_PKG_VERSION"; \
	else \
		dh_gencontrol -a; \
	fi
#	dh_makeshlibs -a
	dh_md5sums -a
	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
