#!/usr/bin/make -f

#include /usr/share/cdbs/1/rules/debhelper.mk
#include /usr/share/cdbs/1/class/cmake.mk

# debhelper7
PATH:=/usr/bin/dh7:/usr/bin:$(PATH)
export PATH
SBOX_REDIRECT_IGNORE=/usr/bin/perl
export SBOX_REDIRECT_IGNORE

# architecture we're building for
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifneq (,$(filter debug,$(DEB_BUILD_OPTIONS)))
	DEBUG_BUILD=1
endif

configure: configure-stamp
configure-stamp: 
	dh_testdir

ifeq ($(DEB_HOST_ARCH), arm)
	# Configure Applauncherd to be build with tests
	BUILD_TESTS=1 HARMATTAN=1 DEBUG_BUILD=$(DEBUG_BUILD) ./configure
else ifeq ($(DEB_HOST_ARCH), armel)
	# Configure Applauncherd to be build with tests
	BUILD_TESTS=1 HARMATTAN=1 DEBUG_BUILD=$(DEBUG_BUILD) ./configure
else
	# Configure Applauncherd to be build with tests and without checking invoker credentials
	BUILD_TESTS=1 DISABLE_VERIFICATION=1 HARMATTAN=1 DEBUG_BUILD=$(DEBUG_BUILD) ./configure
endif

	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	make
	make doc
	touch build-stamp
    
clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f CMakeCache.txt
	dh_clean

install: build
#install: build tests/harmattan/testapps/applauncherd-token-test_1.0_armel.deb
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	DESTDIR=debian/tmp make install
	cp tests/harmattan/testapps/applauncherd-token-test_1.?_armel.deb debian/tmp/usr/share/fala_images
	cp tests/harmattan/testapps/applauncherd-usrlib-test_1.?_armel.deb debian/tmp/usr/share/fala_images

#tests/harmattan/testapps/applauncherd-token-test_1.0_armel.deb:
#	cd tests/harmattan/testapps/applauncherd-token-test-1.0 && dpkg-buildpackage

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_install --sourcedir=debian/tmp -v 
#	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
#	dh_installman
	dh_link
	dh_strip -p applauncherd-launcher --dbg-package=applauncherd-launcher-dbg
	dh_strip -p applauncherd-invoker --dbg-package=applauncherd-invoker-dbg
	dh_strip -p applauncherd-testapps --dbg-package=applauncherd-testapps-dbg
	dh_strip -p applauncherd-single-instance --dbg-package=applauncherd-single-instance-dbg
	dh_strip -p applauncherd-tests 
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	maemo-optify
	dh_md5sums
	dh_builddeb 

# This enables Aegis for arm and armel
ifeq ($(DEB_HOST_ARCH), arm)
	aegis-deb-add -control debian/applauncherd-launcher/DEBIAN/control .. debian/applauncherd-launcher.aegis=_aegis
	aegis-deb-add -control debian/applauncherd-invoker/DEBIAN/control .. debian/applauncherd-invoker.aegis=_aegis
	aegis-deb-add -control debian/applauncherd-testapps/DEBIAN/control .. debian/applauncherd-testapps.aegis=_aegis
else ifeq ($(DEB_HOST_ARCH), armel)
	aegis-deb-add -control debian/applauncherd-launcher/DEBIAN/control .. debian/applauncherd-launcher.aegis=_aegis
	aegis-deb-add -control debian/applauncherd-invoker/DEBIAN/control .. debian/applauncherd-invoker.aegis=_aegis
	aegis-deb-add -control debian/applauncherd-testapps/DEBIAN/control .. debian/applauncherd-testapps.aegis=_aegis
endif

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure

