#!/usr/bin/make -f
# You must remove unused comment lines for the released package.
#export DH_VERBOSE = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
export JAVA_HOME=/usr/lib/jvm/default-java
export CLASSPATH=/usr/share/java/csv.jar:/usr/share/java/debug-disable.jar:/usr/share/java/itext.jar
#XXX deactivate test for now: export GTEST_DIR=/usr/src/gtest

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# mapping all i.86 to x86 for Alljoyn
CPU=$(shell dpkg-architecture -qDEB_HOST_GNU_CPU | sed -e 's/i.86/x86/')
BUILDARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
# in case of X32, we might want to set x86 as well
ifeq ($(BUILDARCH),x32) 
  export CPU = "x86"
endif
OS=linux

export VERSION := 16.04
export SONAME := 1604
export VARIANT := release
#XXX deactivate test for now: export SCONS_ARGS := CPU=$(CPU) OS=$(OS) VARIANT=$(VARIANT) V=1 GTEST_DIR=$(GTEST_DIR) CRYPTO=builtin
export SCONS_ARGS := CPU=$(CPU) OS=$(OS) VARIANT=$(VARIANT) V=1 CRYPTO=builtin
export BUILD_DIR := $(CURDIR)/build/$(OS)/$(CPU)/$(VARIANT)/dist
export OBJ_DIR := $(CURDIR)/build/$(OS)/$(CPU)/$(VARIANT)/obj

%:
#	dh $@ --with javahelper
	dh $@ --with systemd

override_dh_auto_build:
#XXX does not build yet
#	scons BINDINGS=java $(SCONS_ARGS) \
#		WS=off BT=off ICE=off 

	dpkg-architecture -l

	scons BINDINGS=c $(SCONS_ARGS) \
		WS=off BT=off ICE=off 

	scons BINDINGS=cpp $(SCONS_ARGS) \
		WS=off BT=off ICE=off 

# not able to build js bindings yet:
# "Must specify either GECKO_BASE or CORDOVA_BASE build variable"
#	scons BINDINGS=js $(SCONS_ARGS) \
#		WS=off BT=off ICE=off 

#	scons BINDINGS=c $(SCONS_ARGS) \
#		WS=off BT=off ICE=off DOCS=html
#	scons BINDINGS=c $(SCONS_ARGS) \
#		WS=off BT=off ICE=off DOCS=pdf

	touch ./to-be.removed-now.md5
	find ./* |grep '\.md5'|xargs rm
	dh_auto_build

override_dh_auto_clean:
	# see explanation of -I for dpkg-source
	# as this dir contains an {arch}, dpkg-source ignores it
	# "arm" in Alljoyn world means openWRT+cross compiling
	# so this is not needed for Debian
	rm -rf build_core/conf/linux/arm
	# these files are created during build
	rm -f .sconsign.dblite
	rm -rf .sconf_temp
	rm -f config.log
	rm -rf build
	touch ./build_core/to-be-removed-now.pyc
	find ./build_core/* -print|grep "\.pyc"|xargs rm
	touch ./to-be.removed-now.md5
	find ./* |grep '\.md5'|xargs rm
	rm -f alljoyn_c/inc/alljoyn_c/DBusStdDefines.h
	rm -f alljoyn_c/inc/alljoyn_c/Status.h
	rm -f alljoyn_c/inc/qcc/posix/platform_types.h
	rm -rf build
	dh_auto_clean

override_dh_auto_test:
	#XXX deactivate test for now: ./build/$(OS)/$(CPU)/$(VARIANT)/test/cpp/bin/cmtest
	dh_auto_test

override_dh_installchangelogs:
	dh_installchangelogs alljoyn_core/docs/ReleaseNotes.txt
	dh_installchangelogs 

override_dh_install:
	dh_install
	find
	# we put build_core into the common package but
	# don't need any compiled or other stuff, so byebye
	touch ./debian/liballjoyn-common-dev-1604/to-be-removed-now.pyc
	find ./debian/liballjoyn-common-dev-1604/* -print|grep "\.pyc"|xargs rm
	rm -f ./debian/liballjoyn-common-dev-1604/usr/share/alljoyn/build_core/tools/bin/.gitignore
	rm -f ./debian/liballjoyn-common-dev-1604/usr/share/alljoyn/build_core/tools/scons/.gitignore
	rm -f ./debian/liballjoyn-common-dev-1604/usr/share/alljoyn/build_core/tools/bin/whitespace.py


blubber:
	rm -f alljoyn_java/bin/jar/alljoyn.jar
	rm -f alljoyn_java/bin/libs/liballjoyn_java.so
	rm -f alljoyn_java/bin/libs/liballjoyn_java.so
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocIntrospectWithDescriptionClient/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocIntrospectWithDescriptionService/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocMethodsClient/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocMethodsService/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocPropertiesClient/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocPropertiesService/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocSecurityLogonClient/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocSecurityLogonService/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocSecuritySRPClient/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocSecuritySRPService/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocSignalClient/MANIFEST.MF
	rm -f alljoyn_java/samples/java/JavaSDKDoc/JavaSDKDocSignalService/MANIFEST.MF
	rm -f alljoyn_java/src/org/alljoyn/bus/Status.java
	touch ./to-be.removed-now.pdf
	find ./* |grep '\.pdf'|xargs rm
	touch ./to-be.removed-now.md5
	find ./* |grep '\.md5'|xargs rm
	rm -rf alljoyn_core/docs/latex/*
	rm -rf alljoyn_core/docs/pdf/*

	dh_auto_clean

