#!/usr/bin/make -f

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

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

# Fix partially build reproducibility issues.  Also, refrain from
# meddling with build user's home directory.
export HOME = /nonexistent

# Directory mainly holding chemical components data for dssp.
export LIBCIFPP_DATA_DIR = $(CURDIR)/libcifpp-data

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

# 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 \
	PopGen_GenePop \
	PopGen_GenePop_EasyController \
	XXmotif_tool \
	PDB_ResidueDepth \
	mmtf \
	mmtf_online \
	GraphicsBitmaps

# avoid amd64 only tools on other architectures
ifneq ($(BUILDARCH),amd64)
    EXCLUDEARCH=raxml_tool
endif

ifeq ($(BUILDARCH),riscv64)
    # FIXME: the below test is affected by #1080479, and can be
    # restored once it is resolved.
    EXCLUDEARCH+=NCBI_BLAST_tools
endif

ifeq ($(BUILDARCH),mips64el)
    # FIXME: bwa on mips64el is affected by the following issue, so
    # skipping for now:
    #
    #   Bio.Application.ApplicationError: Non-zero return code 138 from
    #   'bwa mem BWA/human_g1k_v37_truncated.fasta
    #   BWA/HNSCC1_1_truncated.fastq BWA/HNSCC1_2_truncated.fastq',
    #   message '[M::bwa_idx_load_from_disk] read 0 ALT contigs'
    EXCLUDEARCH+=BWA_tool
endif

ifeq ($(BUILDARCH),s390x)
    # PDB binary cif parsing is non-working on big endian platforms.
    # The parsing is fed to C code which would need potientially
    # heavier-than-first-thought rewrite to get the issue sorted.
    EXCLUDEARCH+=PDB_binary_cif
    EXCLUDEARCH+=Tutorial
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 clean
	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
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	PYTHONPATH=$(CURDIR)/.pybuild/cpython3_$(shell py3versions -dv)/build \
	$(MAKE) -C Doc html
endif

override_dh_auto_install:
	dh_auto_install
	dh_numpy3 -ppython3-biopython

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e \
	; for avoid in \
		$(EXCLUDENONFREE) \
		$(EXCLUDENOTPACKAGED) \
		$(EXCLUDEARCH) \
		$(EXCLUDEDATABASE) \
	; do mkdir -p Tests_avoid \
	;    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
	# Prepare resources necessary to dssp.
	mkdir -pv $(LIBCIFPP_DATA_DIR)
	cp -v /usr/share/libcifpp/mmcif_pdbx.dic $(LIBCIFPP_DATA_DIR)
	cp -v /usr/share/libcifpp/mmcif_ma.dic $(LIBCIFPP_DATA_DIR)
	cp -v debian/tests/mini-ccd.cif $(LIBCIFPP_DATA_DIR)/components.cif
	# We also keep the tests we need to avoid for later inspection
	test ! -d Tests_avoid || 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 {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'
	# Cleanup steps after tests.
	set -e \
	; if [ -d Tests_avoid ] \
	; then mv Tests_avoid/* Tests \
	;      rmdir Tests_avoid \
	; fi
	rm -r $(LIBCIFPP_DATA_DIR)
endif

DOCPKG = python-biopython-doc
DOCDIR = $(CURDIR)/debian/$(DOCPKG)/usr/share/doc/$(DOCPKG)
override_dh_installdocs-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_installdocs -i
	find debian -name __pycache__ -type d | xargs rm -rf
	# 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/* $(DOCDIR)

	# Tackle privacy breaches caused by MathML.
	sed -i 's@https://cdn.jsdelivr.net/npm/mathjax\@3/es5/tex-mml-chtml.js@file:///usr/share/nodejs/mathjax-full/es5/tex-mml-chtml.js@' \
		$(DOCDIR)/html/Tutorial/*.html \
		$(DOCDIR)/html/index.html

	# Clean fonts that made their way to the documentation package.
	rm -rvf $(DOCDIR)/html/_static/fonts/

	# Use Debian provided Javascript files.
	rm -v $(DOCDIR)/html/_static/jquery.js
	ln -sv /usr/share/javascript/jquery/jquery.js \
		$(DOCDIR)/html/_static/jquery.js
	rm -v $(DOCDIR)/html/_static/doctools.js
	ln -sv /usr/share/javascript/sphinxdoc/1.0/doctools.js \
		$(DOCDIR)/html/_static/doctools.js
	rm -v $(DOCDIR)/html/_static/language_data.js
	ln -sv /usr/share/javascript/sphinxdoc/1.0/language_data.js \
		$(DOCDIR)/html/_static/language_data.js
	rm -v $(DOCDIR)/html/_static/searchtools.js
	ln -sv /usr/share/javascript/sphinxdoc/1.0/searchtools.js \
		$(DOCDIR)/html/_static/searchtools.js
endif

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