#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

#export PYBUILD_NAME=biopython
#export PYBUILD_DESTDIR_python3=debian/python3-$(PYBUILD_NAME)/

# export DEB_BUILD_MAINT_OPTIONS = hardening=+all ... does not build
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

# Fails to build with this option switched on
# export DEB_BUILD_MAINT_OPTIONS = hardening=+all

BUILDARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)

# If `$(CURDIR)/setup.py test` would be called these tests need to be excluded since they require to be online
#EXCLUDEONLINE=Entrez_online HotRand NCBI_qblast SCOP_online SeqIO_online TogoWS
# Since `run_texts.py --online` is used the test requiring online connections are excluded by the test suite
EXCLUDEONLINE=

# avoid PAML_tools and EmbossPhylipNew since paml resp. embassy-phylip are non-free
# This would not trigger a failure but bloats the build log with irrelevant noise
EXCLUDENONFREE=PAML_tools EmbossPhylipNew

# avoid testing tools that are not packaged for Debian yet (or will never be packaged since they might be outdated)
# This would not trigger a failure but bloats the build log with irrelevant noise
EXCLUDENOTPACKAGED=MSAProbs_tool NACCESS_tool PopGen_GenePop PopGen_GenePop_EasyController XXmotif_tool PDB_ResidueDepth mmtf mmtf_online

# avoid amd64 only tools on other architectures
ifeq ($(BUILDARCH),amd64)
    EXCLUDEARCHamd64=
else
    EXCLUDEARCHamd64=raxml_tool
endif

# avoid tests requiring data base connections if no server is running while we are doing the build
# This would not trigger a failure but bloats the build log with irrelevant noise
EXCLUDEDATABASE=BioSQL_MySQLdb BioSQL_psycopg2

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

override_dh_auto_clean:
	dh_auto_clean
	cd Doc && make distclean
	rm -f Doc/install/Installation.pdf
	rm -f Doc/install/Installation.txt
	rm -f Doc/install/Installation.html
	rm -f Doc/Tutorial.pdf Doc/biopdb_faq.pdf
	rm -f Doc/Tutorial/*.aux
	find Doc -name "*.pygtex" -o -name "*.pygstyle" -delete
	rm -rf biopython.egg-info
	rm -rf .pybuild

	if [ -d Tests_avoid ] ; then \
	    mv Tests_avoid/* Tests ; \
	    rmdir Tests_avoid ; \
	fi

	rm -rf debian/tmp_tests

override_dh_auto_build:
	dh_auto_build
	cd Doc && make

override_dh_auto_install:
	dh_auto_install
	dh_numpy3 -ppython3-biopython

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p Tests_avoid
	for avoid in $(EXCLUDEONLINE) $(EXCLUDENONFREE) $(EXCLUDENOTPACKAGED) $(EXCLUDEARCHamd64) $(EXCLUDEDATABASE) $(EXCLUDEFAILINGTESTS) $(EXCLUDEPACKAGEINFLUX) \
		    ; do \
	    mv Tests/test_$${avoid}.py Tests_avoid ; \
	done
	# For the doc package we need a clean testsuite without all the remaining files.  So keep a clean copy here
	mkdir -p debian/tmp_tests
	cp -a Tests debian/tmp_tests
	# remove duplicated file
	rm -f debian/tmp_tests/Tests/Quality/example.fastq.gz
	# We also keep the tests we need to avoid for later inspection
	cp -a Tests_avoid debian/tmp_tests
	# in the Debian package dialign it is not needed to set DIALIGN2_DIR but the test is verifying this dir
	# to run the EMBOSS test test_Emboss also requires to have the environment variable EMBOSS_ROOT set
	LC_ALL=C.UTF-8 dh_auto_test -- --test --system=custom \
		--test-args='set -e; \
                             mkdir -p {build_dir}/home; \
                             mkdir -p {build_dir}/Doc/examples; \
                             cp -a Doc/Tutorial.tex {build_dir}/Doc; \
                             cp -a Doc/Tutorial {build_dir}/Doc; \
                             cp -a Doc/examples {build_dir}/Doc; \
                             cp -a Tests {build_dir}; \
                             cd {build_dir}/Tests; \
                             env DIALIGN2_DIR=/usr/share/dialign EMBOSS_ROOT=/usr/lib/emboss HOME={build_dir}/home {interpreter} run_tests.py --offline'
	mv Tests_avoid/* Tests
	rmdir Tests_avoid
endif

override_dh_installdocs-indep:
	dh_installdocs -i
	find debian -name __pycache__ -type d | xargs rm -rf
	find debian -name example.fastq.gz
	# dh_installdocs seems to refuse copying zero length files (for instance Tests/Blast/tab_2226_tblastn_002.txt) but these are needed for the tests as well
	cp -a debian/tmp_tests/* $(CURDIR)/debian/python-biopython-doc/usr/share/doc/python-biopython-doc/
	find debian -name .cvsignore -delete
	# no idea why Doc dir ends up in /usr/share/doc/python-biopython since biopython 1.71 - just move it into right place
	if [ -d debian/python-biopython-doc/usr/share/doc/python-biopython/Doc ] ; then \
	    mv debian/python-biopython-doc/usr/share/doc/python-biopython/Doc debian/python-biopython-doc/usr/share/doc/python-biopython-doc/Doc ; \
	fi
	cd debian/python-biopython-doc/usr/share/doc/python-biopython-doc/Doc && make clean
	find debian/python-biopython-doc/usr/share/doc/python-biopython-doc/Doc -name Makefile -delete
	find debian/python-biopython-doc/usr/share/doc/python-biopython-doc/Doc -name "*.aux" -delete
	find debian/python-biopython-doc/usr/share/doc/python-biopython-doc/Doc -name "*.lyx" -delete
	find debian/python-biopython-doc/usr/share/doc/python-biopython-doc/Doc -type d -empty -delete

override_dh_fixperms:
	dh_fixperms
	find debian -name "*.dtd" -exec chmod -x \{\} \;