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

export DH_OPTIONS

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

	touch configure-stamp

patch:
	dh_testdir
	dpatch apply-all

unpatch:
	dpatch deapply-all

build: build-stamp
build-stamp: patch
	# 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 \
			 USE_LDAP=false USE_LIBCURL=false HAVE_THREADS=false \
			 USE_XAUTH=true USE_XAUTHPAM=false
	# remove the temporary file, it will be created during install
	rm -f $(CURDIR)/debian/ipsec.secrets

	touch build-stamp

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

	[ ! -f Makefile ] || $(MAKE) 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

	rm -rf debian/openswan-modules-source-build/

	# Really clean (#356716)
	# This is a hack: should be better implemented
	rm -f lib/libopenswan/libopenswan.a || true
	rm -f lib/libopenswan/liboswlog.a || true
	rm -rf OBJ.* || true

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

	# and make sure that template are up-to-date
	debconf-updatepo

	dh_clean

	rm -rf debian/patched || true

install-openswan: DH_OPTIONS=-a
install-openswan: build
	dh_testdir
	dh_testroot
	dh_prep
	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

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

	# delete var/lock/subsys and var/run to satisfy lintian
	rm -rf $(CURDIR)/debian/openswan/var/lock
	rm -rf $(CURDIR)/debian/openswan/var/run

	# remove the already installed docs
	rm -rf "$(CURDIR)/debian/openswan/usr/share/doc"

	# fix some manpage issues
	for oldname in `find $(CURDIR)/debian/openswan/usr/share/man -name "ipsec_ipsec*"`; \
	do \
	    newname=`echo "$$oldname" | sed 's/ipsec_ipsec_/ipsec_/'`; \
	    if [ -f "$$newname" ]; then \
		rm -f "$$oldname"; \
	    else \
		mv "$$oldname" "$$newname"; \
	    fi; \
	done

	dh_installdocs -popenswan -n
	dh_installchangelogs CHANGES

	# openswan-dbg depends on openswan so no need to ship doc twice
	rm -rf $(CURDIR)/debian/openswan-dbg/usr/share/doc/openswan-dbg

	# 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
	chmod 700 -R $(CURDIR)/debian/openswan/etc/ipsec.d/private/
	# don't know why they come with +x set by default...
	chmod 644 $(CURDIR)/debian/openswan/etc/ipsec.d/policies/*
	chmod 644 $(CURDIR)/debian/openswan/etc/ipsec.d/examples/*

	# create /var/lib/openswan with permissions similar to ipsec.secrets
	mkdir -p $(CURDIR)/debian/openswan/var/lib/openswan
	chmod 700 -R $(CURDIR)/debian/openswan/var/lib/openswan

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

	# Empty directory
	rmdir $(CURDIR)/debian/openswan/usr/bin

install-openswan-doc: DH_OPTIONS=-popenswan-doc
install-openswan-doc: build
	dh_testdir
	dh_testroot

	dh_installdocs -popenswan-doc -n
	dh_installchangelogs CHANGES

	# fix some doc issues
	for oldname in `find $(CURDIR)/debian/openswan-doc/usr/share/doc -name "ipsec_ipsec*"`; \
	do \
	    newname=`echo "$$oldname" | sed 's/ipsec_ipsec_/ipsec_/'`; \
	    if [ -f "$$newname" ]; then \
		rm -f "$$oldname"; \
	    else \
		mv "$$oldname" "$$newname"; \
	    fi; \
	done

	# 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-doc/; \
	  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 )

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

install-openswan-modules-source: DH_OPTIONS=-popenswan-modules-source
install-openswan-modules-source: PKGDIR=$(CURDIR)/debian/openswan-modules-source
install-openswan-modules-source: BUILDDIR=$(CURDIR)/debian/openswan-modules-source-build
install-openswan-modules-source: patch
	dh_testdir
	dh_testroot
	dh_installdirs
	mkdir -p "$(BUILDDIR)/modules/openswan"
	mkdir -p "$(BUILDDIR)/modules/openswan/lib"
	mkdir -p "$(BUILDDIR)/modules/openswan/debian"
	mkdir -p "$(BUILDDIR)/modules/openswan/packaging"
	mkdir -p "$(BUILDDIR)/modules/openswan/patches/kernel"
	cp -r CHANGES CREDITS Makefile Makefile.top Makefile.inc Makefile.ver linux/ \
		"$(BUILDDIR)/modules/openswan"
	cp -r lib/libcrypto "$(BUILDDIR)/modules/openswan/lib/"
	cp -r packaging/makefiles packaging/linus packaging/utils packaging/defaults/ packaging/ocf \
		"$(BUILDDIR)/modules/openswan/packaging/"
	cp -r patches/kernel/2.6.32 "$(BUILDDIR)/modules/openswan/patches/kernel/"
	cp -r patches/kernel/2.6.34 "$(BUILDDIR)/modules/openswan/patches/kernel/"
	cp -r patches/kernel/2.6.35 "$(BUILDDIR)/modules/openswan/patches/kernel/"
	find "$(BUILDDIR)/modules/openswan/lib/" -name "*.o" | xargs --no-run-if-empty rm
	install --mode=644 debian/openswan-modules-source.kernel-config "$(BUILDDIR)/modules/openswan/config-all.h"
	install --mode=755 debian/openswan-modules-source.rules "$(BUILDDIR)/modules/openswan/debian/rules"
	install --mode=644 debian/openswan-modules-source.control.in "$(BUILDDIR)/modules/openswan/debian/control.in"
	install --mode=644 debian/openswan-modules-source.docs "$(BUILDDIR)/modules/openswan/debian/docs.in"
	install --mode=644 debian/openswan-modules-source.lintian-overrides.in "$(BUILDDIR)/modules/openswan/debian/lintian-overrides.in"
	install --mode=644 debian/compat "$(BUILDDIR)/modules/openswan/debian/"
	install --mode=644 debian/changelog "$(BUILDDIR)/modules/openswan/debian/"
	install --mode=644 debian/copyright "$(BUILDDIR)/modules/openswan/debian/"
	install --mode=644 debian/NEWS "$(BUILDDIR)/modules/openswan/debian/"
	install --mode=644 debian/README.Debian "$(BUILDDIR)/modules/openswan/debian/"

	tar -C $(BUILDDIR) -c modules/ | bzip2 -9 > \
		"$(PKGDIR)/usr/src/openswan-modules.tar.bz2"

	dh_installdocs -popenswan-modules-source -n
	dh_installchangelogs CHANGES

	# more lintian cleanups
	find $(PKGDIR) -o -name ".gitignore" | xargs --no-run-if-empty rm -f

install-openswan-modules-dkms: DH_OPTIONS=-popenswan-modules-dkms
install-openswan-modules-dkms: VERSION:=$(shell dpkg-parsechangelog | grep '^Version: ' | cut -d: -f3 | cut -d- -f1)
install-openswan-modules-dkms: patch
	dh_testdir
	dh_testroot
	dh_installdirs
	mkdir -p "$(CURDIR)/debian/openswan-modules-dkms/usr/src/openswan-$(VERSION)/lib"
	mkdir -p "$(CURDIR)/debian/openswan-modules-dkms/usr/src/openswan-$(VERSION)/debian"
	mkdir -p "$(CURDIR)/debian/openswan-modules-dkms/usr/src/openswan-$(VERSION)/packaging"
	cp -r Makefile Makefile.top Makefile.inc Makefile.ver linux/ \
		"$(CURDIR)/debian/openswan-modules-dkms/usr/src/openswan-$(VERSION)"
	cp -r lib/libcrypto "$(CURDIR)/debian/openswan-modules-dkms/usr/src/openswan-$(VERSION)/lib"
	cp -r packaging/makefiles packaging/linus packaging/utils packaging/defaults/ packaging/ocf \
		"$(CURDIR)/debian/openswan-modules-dkms/usr/src/openswan-$(VERSION)/packaging/"
	find "$(CURDIR)/debian/openswan-modules-dkms/usr/src/openswan-$(VERSION)/lib" -name "*.o" | xargs --no-run-if-empty rm
	install --mode=644 debian/openswan-modules-source.kernel-config "$(CURDIR)/debian/openswan-modules-dkms/usr/src/openswan-$(VERSION)/config-all.h"
	sed -e "s/#VERSION#/$(VERSION)/g" debian/openswan-modules-dkms.dkms.conf.in > "$(CURDIR)/debian/openswan-modules-dkms/usr/src/openswan-$(VERSION)/dkms.conf"

	dh_installdocs -popenswan-modules-dkms -n
	dh_installchangelogs CHANGES

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

	# Ensure +x for scrips
	find $(CURDIR)/debian/openswan-modules-dkms -type f -name "*.sh" -o -name "*.pl" | xargs --no-run-if-empty chmod 755

	# remove empty dir
	find $(CURDIR)/debian/openswan-modules-dkms -type d -name debian | xargs --no-run-if-empty rmdir

	# These are not executable scripts, more like "shell source" files
	find $(CURDIR)/debian/openswan-modules-dkms -name sshenv -o -name setup | xargs --no-run-if-empty chmod 644

binary-common:
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_link
	dh_strip --dbg-package=openswan-dbg
	dh_compress
	dh_fixperms -X etc/ipsec.secrets -X etc/ipsec.d/private -X var/lib/openswan

	dh_lintian
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

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

binary: binary-indep binary-arch
.PHONY: clean binary-indep binary-arch binary-common install-openswan-modules-dkms \
        install-openswan-modules-source install-openswan-doc install-openswan
