#!/usr/bin/make -f

export LC_ALL=C.UTF-8

include /usr/share/dpkg/default.mk

%:
	dh $@ --with python3 --buildsystem=pybuild

override_dh_auto_configure:
	mkdir -p tools/bundled/Linux
	mkdir -p tools/merge/lib
	# Building the jars for sepp first, tipp then.
	cd tools/merge && \
	CLASSPATH=/usr/share/java/jenkins-json-lib.jar:/usr/share/java/gson.jar ant && \
	CLASSPATH=/usr/share/java/jenkins-json-lib.jar:/usr/share/java/gson.jar ant -f tipp.xml
	# dh_auto_configure only calls the config for sepp, we need it for tipp and
	# upp as well.
	pybuild --configure --configure-args "tipp upp"

override_dh_auto_build:
	dh_auto_build
	# Preparing a conffile with Debian paths for sepp.
	sed 's,path=.*\(pplacer\)$$,path=/usr/bin/\1,' `find . -name main.config` | \
	        sed 's,path=.*\(hmm[a-z]*\)$$,path=/usr/bin/\1,' | \
	        sed 's,path=.*\(seppJsonMerger.jar\)$$,path=/usr/share/sepp/\1,' > sepp.config
	# Preparing a conffile with Debian paths for tipp.
	sed 's,path=.*\(pplacer\)$$,path=/usr/bin/\1,' `find . -name tipp.config` | \
	        sed 's,path=.*\(hmm[a-z]*\)$$,path=/usr/bin/\1,' | \
	        sed 's,path=.*\(tippJsonMerger.jar\)$$,path=/usr/share/sepp/\1,' > tipp.config
	# Preparing a conffile with Debian paths for upp.
	sed 's,path=.*\(pplacer\)$$,path=/usr/bin/\1,' `find . -name upp.config` | \
	        sed 's,path=.*\(hmm[a-z]*\)$$,path=/usr/bin/\1,' | \
	        sed 's,path=.*\(seppJsonMerger.jar\)$$,path=/usr/share/sepp/\1,' > upp.config

override_dh_auto_clean:
	dh_auto_clean
	$(RM) -rf tools/bundled
	$(RM) -rf tools/merge/build
	$(RM) sepp.config tipp.config upp.config
	find . -name "*.class" -delete
	find . -name "*.jar" -delete
	rm -f  home.path sepp.egg-info/PKG-INFO sepp.egg-info/SOURCES.txt sepp.egg-info/dependency_links.txt sepp.egg-info/requires.txt sepp.egg-info/top_level.txt


override_dh_auto_install:
	dh_auto_install
	# Deleting upstream configuration file, which we are not packaging.
	$(RM) debian/sepp/usr/home.path

	# Moving split_sequences into the subdirectory /usr/share/sepp/util, as it is
	# not really part of the software but instead a helper script.
	mkdir -p debian/sepp/usr/share/sepp
	mv debian/sepp/usr/bin/split_sequences.py debian/sepp/usr/share/sepp

	# Not installing upp for now.
	$(RM) debian/sepp/usr/bin/run_upp.py

# To test, we move the test tree into a subdir of /tmp and we proceed to the
# following changes:
# - change paths of configuration files from /etc to build tree;
# - change path of jar in /usr/share to build tree;
# - adapt relative path of jar to its correct place in build tree.
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	ORIGIN_FOLDER=`pwd` && \
	        TEST_FOLDER=`mktemp -d` && \
	        cp -r test/ $$TEST_FOLDER && \
	        cd $$TEST_FOLDER && \
	        mkdir sepp/ && \
	        for F in $$(ls $$ORIGIN_FOLDER/sepp/*.py); do \
	                sed "s,/etc/sepp,$$TEST_FOLDER," $$F > sepp/$${F##*/}; \
	        done && \
	        for F in $$(ls $$ORIGIN_FOLDER/*.config); do \
	                sed "s,/usr/share/sepp,$$ORIGIN_FOLDER/tools/merge," $$F > $${F##*/}; \
	        done && \
	        cd test/unittest/ && \
	        sed "s,\".*\.jar\",\"$$ORIGIN_FOLDER/tools/merge/seppJsonMerger.jar\"," testMerge.py > testMergeCp.py && \
	        mv testMergeCp.py testMerge.py && \
	        PYTHONPATH=$$PYTHONPATH:../../ python3 -m unittest discover -v
endif

# Invoking dh_installman with --language=C to get "traditional" manpages
# although the files we put in /usr/bin end with ".py".
override_dh_installman:
	dh_installman --language=C
