#!/usr/bin/make -f
DEB_DEBIAN_DIR=$(dir $(firstword $(MAKEFILE_LIST)))
DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog -l$(DEB_DEBIAN_DIR)/changelog | sed -rne 's,^Version: ([^+]+).*,\1,p')

%:
	dh $@

build_java_apps_antitheft:
	cd ./apps/AntiTheft/java/
	ant
	cd ../../..

build_java_apps_oscilloscope: apps/tosthreads/apps/TestCollection/java apps/tosthreads/capps/TestCollection/java apps/Oscilloscope/java apps/MultihopOscilloscope/java apps/MultihopOscilloscopeLqi/java
	cd $<
	make veryclean
	make oscilloscope.jar

build_java_tinyos:
	cd support/sdk/java
	make tinyos.jar

override_dh_auto_clean:
	# run normal dh_auto_clean
	dh_auto_clean

override_dh_build: build_java_apps_antitheft build_java_apps_oscilloscope

override_dh_install:
	# run normal dh_install
	dh_install
	# remove apps/tests/LinkBench
	# It needs some external java dependencies, which are currently
	# not available in Debian.
	rm -rf debian/tinyos-source/usr/src/tinyos/apps/tests/LinkBench
	# fix file permissions
	find debian/tinyos-source -name "*.nc" -exec chmod 644 \{\} \;
	find debian/tinyos-source -name "*.h" -exec chmod 644 \{\} \;
	find debian/tinyos-source -name "*.java" -exec chmod 644 \{\} \;
	find debian/tinyos-source -name "*.txt" -exec chmod 644 \{\} \;
	find debian/tinyos-source -name "*.xml" -exec chmod 644 \{\} \;
	find debian/tinyos-source -name "*.extra" -exec chmod 644 \{\} \;
	find debian/tinyos-source -name "README" -exec chmod 644 \{\} \;
	find debian/tinyos-source -name "Makefile" -exec chmod 644 \{\} \;
	# make scripts executable
	chmod 755 debian/tinyos-source/usr/src/tinyos/apps/Oscilloscope/oscilloscope.py
	chmod 755 debian/tinyos-source/usr/src/tinyos/apps/UDPEcho/tests/echotest.pl
	chmod 755 debian/tinyos-source/usr/src/tinyos/apps/tests/TestDhv/gentest.py
	chmod 755 debian/tinyos-source/usr/src/tinyos/apps/tests/TestDip/gentest.py
	chmod 755 debian/tinyos-source/usr/src/tinyos/tos/lib/ppp/tests/Ipv6/pppd.sh
	chmod 755 debian/tinyos-source/usr/src/tinyos/tos/lib/ppp/tests/Lcp/pppd.sh
	chmod 755 debian/tinyos-source/usr/src/tinyos/tos/lib/timer/generate.sh
	chmod 755 debian/tinyos-source/usr/src/tinyos/tos/lib/tossim/generate-swig.bash
	chmod 755 debian/tinyos-source/usr/src/tinyos/tos/lib/tossim/sf/examples/TestSerialSFClient.py
	chmod 755 debian/tinyos-source/usr/src/tinyos/tos/lib/tossim/sf/generate-swig.bash
	chmod 755 debian/tinyos-source/usr/src/tinyos/tos/lib/tossim/topologies/layout.py
	chmod 755 debian/tinyos-source/usr/src/tinyos/tos/lib/zigbee/pmote.sh
	chmod 755 debian/tinyos-source/usr/src/tinyos/tos/lib/zigbee/pnetwork.sh
	# remove executable bit from files, which can't be executed
	chmod 644 debian/tinyos-source/usr/src/tinyos/tos/lib/ppp/tests/Ipv6/test.sh
	chmod 644 debian/tinyos-source/usr/src/tinyos/support/sdk/c/sf/bootstrap
	chmod 644 debian/tinyos-source/usr/src/tinyos/support/make/m16c60/crt_62p.S
	chmod 644 debian/tinyos-source/usr/src/tinyos/support/make/mulle.target
	chmod 644 debian/tinyos-source/usr/src/tinyos/apps/tosthreads/tinyld/SerialLoader/tinyos.py
	chmod 644 debian/tinyos-source/usr/src/tinyos/apps/tests/TestFtsp/Ftsp/FtspDataAnalyzer.m
	chmod 644 debian/tinyos-source/usr/src/tinyos/tos/platforms/mulle/.platform
	chmod 644 debian/tinyos-source/usr/src/tinyos/apps/tests/TestFtsp/FtspLpl/TestFtspMsg.py
	chmod 644 debian/tinyos-source/usr/src/tinyos/support/make/m16c60/linker_62p.x
	chmod 644 debian/tinyos-source/usr/src/tinyos/apps/tosthreads/tinyld/SerialLoaderFlash/tinyos.py
	chmod 644 debian/tinyos-source/usr/src/tinyos/support/make/m16c60/m16c60.rules
	chmod 644 debian/tinyos-source/usr/src/tinyos/tos/lib/ppp/tests/Lcp/test.sh
	# remove extra license files
	rm debian/tinyos-source/usr/src/tinyos/support/sdk/c/coap/LICENSE
	# replace Jama-1.0.2.jar with symlink
	rm debian/tinyos-source/usr/src/tinyos/apps/tests/rfxlink/TestTimeStamping/Jama-1.0.2.jar
	ln -s ../../../../../../share/java/jama.jar debian/tinyos-source/usr/src/tinyos/apps/tests/rfxlink/TestTimeStamping/Jama-1.0.2.jar
	# install tinyos profile
	mkdir -p debian/tinyos-source/etc/profile.d
	install -m 644 debian/tinyos.profile debian/tinyos-source/etc/profile.d/tinyos.sh

override_dh_installchangelogs:
	dh_installchangelogs release-notes.txt

get-orig-source:
	uscan --noconf --force-download --download-current-version --destdir=. $(DEB_DEBIAN_DIR)/..
	rm -rf tinyos-$(DEB_UPSTREAM_VERSION)
	tar -xf tinyos_$(DEB_UPSTREAM_VERSION).orig.tar.gz
	rm -rf tinyos-$(DEB_UPSTREAM_VERSION).tar.gz
	rm tinyos_$(DEB_UPSTREAM_VERSION).orig.tar.gz
	# remove prebuilt binaries. We will use the libraries built
	# by tinyos-tools instead.
	rm -f tinyos-$(DEB_UPSTREAM_VERSION)/support/sdk/java/net/tinyos/util/*.lib
	# create new orig.tar.gz
	GZIP=--best tar -cz --owner root --group root --mode a+rX -f \
		tinyos_$(DEB_UPSTREAM_VERSION)+dfsg.orig.tar.gz \
		tinyos-$(DEB_UPSTREAM_VERSION)
	rm -rf tinyos-$(DEB_UPSTREAM_VERSION)