#!/usr/bin/make -f

# DH_VERBOSE := 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

export JAVA_HOME  := /usr/lib/jvm/default-java

include /usr/share/dpkg/pkg-info.mk

%:
	dh $@ --with maven_repo_helper

override_dh_auto_clean:
	dh_auto_clean
	if [ -e src/main/c/org_broadinstitute_hellbender_utils_bwa_BwaMemIndex.c.save ]; then \
	        mv src/main/c/org_broadinstitute_hellbender_utils_bwa_BwaMemIndex.c.save src/main/c/org_broadinstitute_hellbender_utils_bwa_BwaMemIndex.c; \
	fi
	-rm src/test/resources/ref.fa.img
	# Cleaning the C part
	cd src/main/c/ && make clean
	-rm -rf src/main/c/bwaComponent
	# Replacing the modified build.gradle file having a version number by the original one.
	if [ -e build.gradle.old ]; then mv build.gradle.old build.gradle; fi

override_dh_auto_configure:
	# Putting the version in the JNI file so that is can output version information.
	cp src/main/c/org_broadinstitute_hellbender_utils_bwa_BwaMemIndex.c src/main/c/org_broadinstitute_hellbender_utils_bwa_BwaMemIndex.c.save
	sed -i "s/BWA_COMMIT/\"$(DEB_VERSION)\"/" src/main/c/org_broadinstitute_hellbender_utils_bwa_BwaMemIndex.c
	# Copying the component sources to src/main/c for the JNI build
	mkdir -p src/main/c/bwaComponent/
	cp -a bwa-apache2/* src/main/c/bwaComponent/
	# Adding a version number in the build.gradle file.
	cp build.gradle build.gradle.old
	sed -i "s/\(^group.*\)/\1\nversion = '$(DEB_VERSION_UPSTREAM)'/ ; s/\+dfsg[[:digit:]]*//" build.gradle
	dh_auto_configure

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# The shared lib should be looked for in its build directory.
	LD_LIBRARY_PATH="$$LD_LIBRARY_PATH:$$(readlink -f src/main/c)" dh_auto_test
endif