#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

export PYBUILD_NAME=pysam
export LC_ALL = C.UTF-8

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

TESTPKG    := $(DEB_SOURCE)-tests

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

export PYBUILD_TEST_NOSE=1
# pybuild calls nosetests as `python{version} -m nose tests`
#
# The "tests" argument (passed by pybuild as shown above) fails,
# so don't pass anything:
export PYBUILD_TEST_ARGS=
# This is how we pass parameters to nose without command-line flags
export NOSE_WHERE=$(CURDIR)/tests
export NOSE_VERBOSE=2
export NOSE_NOCAPTURE=1

# DEB_BUILD_OPTIONS := nocheck

clean: clean-tests
%:
	dh $@ --with python2,python3 --buildsystem=pybuild

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

override_dh_auto_test: pysam_data.all cbcf_data.all
	dh_auto_test

override_dh_auto_clean:
	dh_auto_clean
	$(RM) \
		pysam/config.py \
		pysam/config.h \
		pysam/lib*.c \
		samtools/config.h
	rm -rf pysam.egg-info

.PHONY: pysam_data.% cbcf_data.%
cbcf_data.%:
	cd tests/$(basename $@) && $(MAKE) $*
pysam_data.%:
	cd tests/$(basename $@) && $(MAKE) $*

.PHONY: clean-tests
clean-tests: pysam_data.clean cbcf_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 \
		tests/GRCh38_full_analysis_set_plus_decoy_hla.fa.fai \
		tests/pysam_data/ex1.fa.gz \
		tests/pysam_data/ex1.fa.gz.gzi \
		tests/pysam_data/ex1_csi.bam.csi

