#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules file - for whereami
# Copyright 1999-2006 by Andrew McMillan
# Based on the pcmcia package by Brian Mays
# Patterned after the hello package by Ian Jackson.

# The package we are dealing with
package=whereami

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

build-stamp:
	dh_testdir
	umask 022; $(MAKE) all
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	-$(MAKE) clean
	dh_clean

build:	build-stamp

binary-arch binary-indep: binary

binary:	build
	dh_testdir
	dh_testroot
	dh_clean -k
	$(RM) -r debian/$(package)
# Install directories
	dh_installdirs DEBIAN etc etc/init.d etc/$(package) \
							etc/$(package) etc/apm etc/apm/scripts.d \
							etc/apm/suspend.d etc/apm/resume.d \
							etc/dhcp etc/dhcp/dhclient-exit-hooks.d \
							etc/dhcp3 etc/dhcp3/dhclient-exit-hooks.d \
							etc/network etc/network/if-up.d \
							etc/network/if-pre-up.d \
							etc/network/if-down.d \
							etc/network/if-post-down.d \
							usr usr/sbin usr/share usr/share/doc \
							usr/share/$(package) \
							usr/share/$(package)/actions \
							usr/share/$(package)/tests \
							usr/share/doc/$(package) \
							usr/share/doc/$(package)/html/ \
							usr/share/doc/$(package)/html/images \
							usr/share/doc/$(package)/patches/ \
							usr/share/man usr/share/man/man8 \
							var/lib/whereami

# Install files
	install bin/$(package) debian/$(package)/usr/sbin/.
	dh_installinit --no-restart-on-upgrade --update-rcd-params="defaults 35 65"
	install bin/whereami.apm debian/$(package)/etc/apm/scripts.d/$(package)
	cd debian/$(package)/etc/apm/suspend.d; ln -s ../scripts.d/$(package) 01$(package)
	cd debian/$(package)/etc/apm/resume.d; ln -s ../scripts.d/$(package) 99$(package)
	install -m644 whereami.conf mail-relay.conf apm.conf detect.conf debian/$(package)/etc/$(package)/.
	install -m755 debian/whereami.dhclient3-exit-hook debian/$(package)/etc/dhcp/dhclient-exit-hooks.d/whereami
	install -m755 debian/whereami.dhclient3-exit-hook debian/$(package)/etc/dhcp3/dhclient-exit-hooks.d/whereami
	install -m755 debian/whereami.network-hook debian/$(package)/etc/network/if-pre-up.d/whereami
# If we want to run other than in "pre-up" then we should include something like this.  At this stage just "pre-up" seems right-ish...
# 	(cd debian/$(package)/etc/network; ln -s ../if-pre-up.d/whereami if-up.d/whereami; ln -s ../if-pre-up.d/whereami if-post-down.d/whereami; ln -s ../if-pre-up.d/whereami if-down.d/whereami)
	install -m644 debian/*.patch debian/$(package)/usr/share/doc/$(package)/patches/.
	install -m644 html/*.html debian/$(package)/usr/share/doc/$(package)/html/.
	install -m644 `find html/images -maxdepth 1 -type f` debian/$(package)/usr/share/doc/$(package)/html/images/.
	install -m755 `find scripts -maxdepth 1 -type f ` debian/$(package)/usr/share/$(package)/actions/.
	cd debian/$(package)/usr/share/$(package); sh -c 'for FILE in actions/* ; do ln -s $$FILE; done'
	install -m755 `find scripts/tests -maxdepth 1 -type f ` debian/$(package)/usr/share/$(package)/tests/.
# Find configuration files
	# find debian/$(package)/etc/$(package) -name "*.conf" -not -type d | \
		# sed 's%debian/$(package)%%' >debian/conffiles
	# echo "/etc/init.d/$(package)" >>debian/conffiles
	# echo "/etc/apm/event.d/10$(package)" >>debian/conffiles
	# sort <debian/conffiles | uniq >debian/$(package)/DEBIAN/conffiles
# Install documentation
	install -m644 AUTHORS README VERSION TODO \
	  debian/$(package)/usr/share/doc/$(package)/.
	dh_installman man/whereami.8 man/detect.conf.5 man/whereami.conf.5
	dh_installchangelogs
	dh_compress
	install -m644 debian/copyright \
	  debian/$(package)/usr/share/doc/$(package)/copyright
	dh_fixperms
	# Set permissions on our helper scripts
	chmod 755 debian/$(package)/usr/share/$(package)/*
	dh_installdeb
	dh_perl
	dh_gencontrol
	sed '/:/s/, *$$//' debian/$(package)/DEBIAN/control \
	  >debian/$(package)/DEBIAN/control.new
	mv debian/$(package)/DEBIAN/control.new debian/$(package)/DEBIAN/control
	dh_md5sums
	dh_builddeb


# Below here is fairly generic really

binary:		binary-indep binary-arch

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

.PHONY: binary-arch binary-indep clean
