#!/usr/bin/make -f

export DH_VERBOSE=1
export DH_OPTIONS

%:
	dh  $@ --buildsystem=gradle --with javahelper,maven_repo_helper

override_dh_link:
	rm -f debian/libpicard-java-doc/usr/share/doc/libpicard-java/api/jquery/external/jquery/jquery.js
	dh_link

override_dh_auto_build:
	@echo 'blhc: ignore-line-regexp: .*gradle.*ScriptTransformer.*'
	dh_auto_build -- jar javadoc

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# We execute the tests in a copy of the full build tree in a place with a path
	# having no '+' in its name: else the '+' in the version number of
	# picard-tools is changed into a space by barclay and the classes are not
	# found.
	# Also, tests do not work with locales with a different decimal separator
	# (for example ',')
	TESTDIR=$$(mktemp -d);\
	    cp -a . $$TESTDIR/picardTest;\
	    cd $$TESTDIR/picardTest;\
	    env LC_ALL=C \
	    dh_auto_build -- test
endif

override_dh_clean:
	dh_clean
	# also remove bai files that are left after tests
	# find testdata -iname "*.bai" -exec rm {} \;  # This needs to be more fine grained since testdata/picard/indices/index_test_b.bam.bai needs to stay!
	$(RM) debian/bd-temp-substvars
	$(RM) debian/bd.substvars

override_dh_gencontrol:
	cat debian/control \
	| sed -e '/^Build-.*Depends:/,/^[^[:space:]#]/{s/^Build-.*Depends:/ /;p};d' \
	| grep '^[[:space:]]' \
	| tr ',' '\n' \
	> debian/bd-temp-substvars
	echo -n "bd:libhtsjdk-java=" > debian/bd.substvars
	grep 'libhtsjdk-java ' debian/bd-temp-substvars \
	| tr "\n" "," \
	| sed -e 's/[[:space:]]\+/ /g;s/,$$/\n/' \
	>> debian/bd.substvars
	echo -n "bd:libguava-java=" >> debian/bd.substvars
	grep 'libguava-java ' debian/bd-temp-substvars \
	| tr "\n" "," \
	| sed -e 's/[[:space:]]\+/ /g;s/,$$/\n/' \
	>> debian/bd.substvars
	dh_gencontrol -O--buildsystem=gradle -- -Tdebian/bd.substvars
