#!/usr/bin/make -f

export DEB_BUILD_HARDENING=1
export DH_OPTIONS

# These are used for cross-compiling and for saving the configure script
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_ARCH   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g -fno-strict-aliasing -isystem /usr/include -D_FORTIFY_SOURCE=2

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

#NAT-T=--enable-natt
#
#ifeq '$(DEB_HOST_ARCH)' 'kfreebsd-amd64'
#NAT-T=
#endif

#ifeq '$(DEB_HOST_ARCH)' 'kfreebsd-i386'
#NAT-T=
#endif

%:
	dh $@

patch:
	quilt push -a

unpatch:
	quilt pop -a

override_dh_auto_configure:
	CFLAGS="$(CFLAGS)" ./configure --verbose \
					--host=$(DEB_HOST_GNU_TYPE) \
					--build=$(DEB_BUILD_GNU_TYPE) \
					--prefix=/usr \
					--sysconfdir=/etc/racoon \
					--libdir=/usr/lib/ipsec-tools \
					--mandir=\$${prefix}/share/man \
					--infodir=\$${prefix}/share/info \
					--localstatedir=/var/run \
					--enable-shared \
					--disable-static \
					--enable-frag \
					--enable-gssapi \
					--enable-hybrid \
					--with-libldap \
					--enable-dpd \
					--enable-adminport \
					--with-kernel-headers=/usr/include \
					--with-libpam \
					--without-readline \
					--disable-security-context \
					--enable-natt

override_dh_clean:
	dh_testdir
	dh_auto_clean
	dh_clean
	# Remove this after make clean as it upsets git-buildpackage
	# make clean will invoke configure!!
	- rm -f config.log

override_dh_install:
	chrpath -r /usr/lib/ipsec-tools  debian/tmp/usr/lib/ipsec-tools/*.so.0.* \
                   debian/tmp/usr/sbin/*
	dh_install
	mkdir -p debian/racoon/var/lib/racoon
	install -m 755 -o root -g root debian/racoon-tool.pl \
	    debian/racoon/usr/sbin/racoon-tool

	install -D -m 600 -o root -g root src/racoon/samples/psk.txt.sample \
	    debian/racoon/etc/racoon/psk.txt
	install -m 644 -o root -g root  debian/racoon-tool.conf \
	    debian/racoon/etc/racoon
	install -m 644 -o root -g root debian/racoon.conf \
            debian/racoon/etc/racoon/racoon.conf
	mkdir -p debian/ipsec-tools/etc
	install -m 755 -o root -g root debian/ipsec-tools.conf \
            debian/ipsec-tools/etc/ipsec-tools.conf

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog

override_dh_installexamples:
	dh_installexamples -pracoon -X.in src/racoon/samples
	dh_installexamples

override_dh_installinit:
	dh_installinit -R -pracoon -- start 40 S . stop 07 0 1 6 .
	dh_installinit -R -pipsec-tools --name=setkey --no-start -- start 14 S .

override_dh_installman:
	dh_installman
	dh_installman -pracoon debian/racoon-tool.8 debian/racoon-tool.conf.5

override_dh_makeshlibs:
	dh_makeshlibs -n

