#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

export PYBUILD_NAME=pysam

TESTPKG    := $(DEB_SOURCE)-tests

HTSLIBDIR  := /usr/lib/$(DEB_HOST_MULTIARCH)
export HTSLIB_LIBRARY_DIR=$(HTSLIBDIR)
export HTSLIB_INCLUDE_DIR=/usr/include

export PYBUILD_TEST_NOSE=1
export PYBUILD_TEST_ARGS={dir}/tests

# DEB_BUILD_OPTIONS := nocheck

clean: clean-tests
%:
	LC_ALL=C.UTF-8 dh $@ --with python2,python3 --buildsystem=pybuild

override_dh_install: clean-tests
	dh_install -Xtest.gtf.gz

override_dh_auto_test:
	cd tests && make -C pysam_data
	# dh_auto_test	# unfortunately this does not work out of the box
			# see https://lists.debian.org/debian-python/2015/07/msg00064.html
			# thus trying manually with unusual tricks
	PYBUILD_SYSTEM=custom \
	PYBUILD_TEST_ARGS="cd tests && if [ {version} = 2.7 ] ; then nosetests -s ; else nosetests3 -s -v ; fi" dh_auto_test || \
		echo "Ignoring test failures"

get-orig-source:
	uscan --verbose --force-download --repack --compression xz

clean-tests:
	$(MAKE) -C tests/pysam_data clean
	find . -name "*.pyc" -delete
	find . -name "*.pyxbldc" -delete
	find . -name "tmp_*" -delete
	find . -name "*.bai*" -delete
	find . -name "*.cram*" -delete
	find . -name "*.bam" -delete
	rm -rf	tests/pysam_test_work \
		tests/example_htslib.gtf.gz.tbi \
		tests/log.txt \
		tests/test.bam \
		tests/_compile_test.c \
		tests/pysam_ex2.sam \
		tests/__pycache__ \
		pysam/calignmentfile.c \
		pysam/cbcf.c

.PHONY: clean-tests
