#!/usr/bin/make -f

# This has to be exported to make some magic below work.
export DH_OPTIONS

CONFIGURE = cmake

# Files to delete when cleaning
FILESREGEXTOCLEAN = *.so *.so.* *.a *.cxx cmake_install.cmake *.hh *.o CMakeFiles Makefile
FILESTOCLEAN += CMakeCache.txt
FILESTOCLEAN += cmake.check_cache
FILESTOCLEAN += CMakeOutput.log
FILESTOCLEAN += config.log
FILESTOCLEAN += install_manifest.txt
FILESTOCLEAN += warning.log
FILESTOCLEAN += source/ikea/about.h
FILESTOCLEAN += source/ikea/conflict.h
FILESTOCLEAN += source/ikea/ikea
FILESTOCLEAN += source/ikea/root.h
FILESTOCLEAN += source/ikea/site.h
FILESTOCLEAN += source/ikea/topology.h
FILESTOCLEAN += source/ikec/banner.h
FILESTOCLEAN += source/ikec/filepass.h
FILESTOCLEAN += source/ikec/ikec
FILESTOCLEAN += source/ikec/root.h
FILESTOCLEAN += source/iked/conf.parse.cpp
FILESTOCLEAN += source/iked/conf.parse.hpp
FILESTOCLEAN += source/iked/conf.token.cpp
FILESTOCLEAN += source/iked/etkey
FILESTOCLEAN += source/iked/iked

# Files to delete from the source archive to be in accordance with the Debian free software guidelines
DFSG_FILESTOCLEAN = ike/docs/*

VERSION:=$(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)+[^+]*$$/\1/p')
URL = http://www.shrew.net/download/ike/ike-$(VERSION)-release.tgz

DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS 2>/dev/null)

ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
	# Debian FreeBSD kernel is missing IPSEC_NAT_T support, see README.TXT
	DEB_CONFIGURE_EXTRA_FLAGS += -DNATT=NO
else
	DEB_CONFIGURE_EXTRA_FLAGS += -DNATT=YES
endif

get-orig-source:
	mkdir tmp
	wget -O tmp/ike.tar.gz "$(URL)"
	tar zxvfC tmp/ike.tar.gz tmp
	$(RM) $(addprefix tmp/,$(DFSG_FILESTOCLEAN))
	mv tmp/ike tmp/ike-$(VERSION)+dfsg
	tar cvfC ike_$(VERSION)+dfsg.orig.tar tmp ike-$(VERSION)+dfsg
	gzip -9 ike_$(VERSION)+dfsg.orig.tar
	$(RM) -R tmp

configure: configure-stamp
configure-stamp:
	dh_testdir
	$(CONFIGURE) -DQTGUI=YES -DNATT=YES -DCMAKE_INSTALL_PREFIX:PATH=/usr -DETCDIR:PATH=/etc -DMANDIR:PATH=/usr/share/man -DDEBUG=YES $(DEB_CONFIGURE_EXTRA_FLAGS)
	touch $@

#Architecture
build: build-arch

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp
	$(MAKE)
	touch $@

clean:
	dh_testdir
	$(RM) build-arch-stamp configure-stamp
# Cmake does not provide clean target. I have to do it manually.
	$(RM) $(FILESTOCLEAN)
	$(RM) -R $(foreach regex, $(FILESREGEXTOCLEAN), $(shell find . -name $(regex) -print))
	dh_clean

install: install-arch

install-arch:
	dh_testdir
	dh_testroot
	dh_clean -k -s
	dh_installdirs -s

	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install

# Rename the executables files
	mv debian/tmp/usr/bin/ikea debian/tmp/usr/bin/ikea.real
	mv debian/tmp/usr/bin/ikec debian/tmp/usr/bin/ikec.real
	mv debian/tmp/usr/sbin/iked debian/tmp/usr/sbin/iked.real
	dh_install -s
ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
	sed -e 's/socket natt/#&/' source/iked/iked.conf.sample >debian/ike/etc/iked.conf
else
	cat source/iked/iked.conf.sample >debian/ike/etc/iked.conf
endif
	mv debian/ike/etc/init.d/iked debian/ike/etc/init.d/ike

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	dh_testdir
	dh_testroot
	dh_installchangelogs TODO.TXT
	dh_installdocs
	dh_installinit -p ike --onlyscripts --error-handler=ike_error
	dh_installman
	dh_lintian
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -n
	dh_installdeb
	dh_shlibdeps -l/usr/lib/ike
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep:

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure get-orig-source
