#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# This is the debhelper compatability version to use.
export DH_COMPAT=3

export DH_OPTIONS

ifeq (,$(wildcard /usr/bin/po2debconf))
	PO2DEBCONF := no
	MINDEBCONFVER := 0.5
else
	PO2DEBCONF := yes
	MINDEBCONFVER := 1.2.0
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch configure-stamp

patch-stamp:
	dh_testdir

	# and replace all calls of 'awk' by 'gawk'
	-echo "Replacing all calls of awk by gawk"
	if [ ! -e "patch-awk-to-gawk.stamp" ]; then \
		for f in `grep "awk " --recursive --files-with-match programs/*`; \
		do \
			cp $$f $$f.awk-to-gawk.save; \
			sed 's/awk /gawk /' $$f.awk-to-gawk.save > $$f; \
		done ; \
		touch patch-awk-to-gawk.stamp; \
	fi
	
	touch patch-stamp

unpatch: 
	# undo the replacement of all calls of 'awk' by 'gawk'
	-echo "Undoing the replacement of awk by gawk"
	if [ -e "patch-awk-to-gawk.stamp" ]; then \
		for f in `find $(CURDIR) -name "*.awk-to-gawk.save"`; \
		do \
			sh -c "oldf=\`expr $$f : '\(.*\)\.awk-to-gawk.save'\`; \
			echo \"Restoring \$$oldf\"; \
			mv $$f \$$oldf" ; \
		done ; \
		rm -f patch-awk-to-gawk.stamp; \
	fi

	rm -f patch-stamp

build: build-stamp
build-stamp: patch-stamp
	# create a dummy ipsec.secrets file before building the package so
	# that no RSA keys are created during the build process
	# (a package should not include a RSA key, it should produce the key
	# on demand, e.g. in the postinst script)
	touch $(CURDIR)/debian/ipsec.secrets
	$(MAKE) programs INC_USRLOCAL=/usr \
        		 FINALBINDIR=/usr/lib/ipsec \
			 FINALLIBEXECDIR=/usr/lib/ipsec \
			 PUBDIR=/usr/sbin \
			 MANTREE=/usr/share/man \
			 CONFDIR=$(CURDIR)/debian
	# remove the temporary file, it will be created during install
	rm -f $(CURDIR)/debian/ipsec.secrets

	# here we re-generate the upstream HTML documentation
	$(MAKE) -C doc/ index.html
	
	# also generate the fswcert tool
	$(MAKE) -C debian/fswcert/

	touch build-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	-$(MAKE) clean
	-$(MAKE) -C debian/fswcert/ clean
	# after a make clean, no binaries _should_ be left, but ....
	-find $(CURDIR) -name "*.o" | xargs --no-run-if-empty rm
	-find $(CURDIR)/lib/libcrypto -name "*.a" | xargs --no-run-if-empty rm

	-$(MAKE) -f debian/rules unpatch

	# just in case something went wrong
	rm -f $(CURDIR)/debian/ipsec.secrets

	dh_clean

ifeq ($(PO2DEBCONF),yes)       
	# Hack for woody compatibility. This makes sure that the       
	# debian/templates file shipped in the source package doesn't 
	# specify encodings, which woody's debconf can't handle. If building
	# on a system with po-debconf installed (conveniently debhelper (>=
	# 4.1.16) depends on it), the binary-arch target will generate a
	# better version for sarge.       
	echo 1 > debian/po/output
	po2debconf debian/openswan.templates.master > debian/openswan.templates
	rm -f debian/po/output
endif

install-openswan: DH_OPTIONS=-a
install-openswan: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	$(MAKE) install INC_USRLOCAL=/usr \
        		FINALBINDIR=/usr/lib/ipsec \
			FINALLIBEXECDIR=/usr/lib/ipsec \
			PUBDIR=$(CURDIR)/debian/openswan/usr/sbin \
			MANTREE=$(CURDIR)/debian/openswan/usr/share/man \
			DESTDIR=$(CURDIR)/debian/openswan
	rm -rf $(CURDIR)/debian/openswan/usr/local
	install --mode=0600 $(CURDIR)/debian/ipsec.secrets.proto $(CURDIR)/debian/openswan/etc/ipsec.secrets

	# use bash for init.d and _plutorun
	patch $(CURDIR)/debian/openswan/etc/init.d/ipsec < debian/use-bash.diff
	patch $(CURDIR)/debian/openswan/usr/lib/ipsec/_plutorun < debian/use-bash.diff

	# install the fswcert tool
	install $(CURDIR)/debian/fswcert/fswcert $(CURDIR)/debian/openswan/usr/bin

	rm -f $(CURDIR)/debian/openswan/etc/init.d/ipsec?*
	rm -f $(CURDIR)/debian/openswan/usr/lib/ipsec/_startklips?*

	# this is handled by update-rc.d
	rm -rf $(CURDIR)/debian/openswan/etc/rc?.d
        
	dh_installdocs -popenswan -n
	# change the paths in the installed doc files (but only in regular 
	# files, not in links to the outside of the build tree !)
	( cd $(CURDIR)/debian/openswan/; \
	  for f in `grep "/usr/local/" --recursive --files-with-match *`; \
	  do \
		if [ -f $$f -a ! -L $$f ]; then \
		    cp $$f $$f.old; \
 		    sed 's/\/usr\/local\//\/usr\//' $$f.old > $$f; \
		    rm $$f.old; \
		fi; \
	  done )
	# but remove the doc/src dir, which just duplicates the HTML files
	rm -r $(CURDIR)/debian/openswan/usr/share/doc/openswan/doc/src
	  
	# the logcheck ignore files
	install -D --mode=0600 $(CURDIR)/debian/logcheck.ignore.paranoid $(CURDIR)/debian/openswan/etc/logcheck/ignore.d.paranoid/openswan
	install -D --mode=0600 $(CURDIR)/debian/logcheck.ignore.server $(CURDIR)/debian/openswan/etc/logcheck/ignore.d.server/openswan
	install -D --mode=0600 $(CURDIR)/debian/logcheck.ignore.server $(CURDIR)/debian/openswan/etc/logcheck/ignore.d.workstation/openswan
	install -D --mode=0600 $(CURDIR)/debian/logcheck.violations.ignore $(CURDIR)/debian/openswan/etc/logcheck/violations.ignore.d/openswan

	# set permissions on ipsec.secrets
	chmod 600 $(CURDIR)/debian/openswan/etc/ipsec.secrets
	chmod 644 $(CURDIR)/debian/openswan/etc/ipsec.conf
        # this also only works with the X.509 patch applied
	-chmod 700 -R $(CURDIR)/debian/openswan/etc/ipsec.d/private/

	# more lintian cleanups
	find $(CURDIR)/debian/openswan -name ".cvsignore" | xargs --no-run-if-empty rm

binary-common:
	#dh_testversion 2
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installchangelogs CHANGES
	dh_link
	dh_strip
	dh_compress
	dh_fixperms -X etc/ipsec.conf -X etc/ipsec.secrets -X etc/ipsec.d

#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

## Build architecture-independent files here.
#binary-indep: install-kernel-patch-openswan install-kernel-patch-openswan-ext
#	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture-dependent files here.
binary-arch: install-openswan
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Any other binary targets build just one binary package at a time.
#binary-%: build install
#	make -f debian/rules binary-common DH_OPTIONS=-p$*
             
binary: binary-arch
.PHONY: clean binary-arch
