#!/usr/bin/make -f

export PYBUILD_NAME=pysam

DEBPKGNAME := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}')
TESTPKG    := $(DEBPKGNAME)-tests

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

# Cython is recreating some c-files.  To enable building twice in a row these
# will be saved in advance and restored afterwards
debian/savefiles:
	mkdir -p debian/savefiles
	cp -a `grep -l "Generated by Cython" pysam/*.c` debian/savefiles

override_dh_clean:
	dh_clean
	# restore cython generated files
	if [ -d debian/savefiles ] ; then \
	    mv debian/savefiles/* pysam ; \
	    rm -rf debian/savefiles ; \
	fi

override_dh_auto_build: debian/savefiles
	dh_auto_build

override_dh_auto_test:
	dh_auto_test  # default tests
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	PYBUILD_SYSTEM=custom \
	PYBUILD_TEST_ARGS="cd tests && PYTHONPATH={build_dir} {interpreter} ./pysam_test_offline.py" \
	dh_auto_test --buildsystem=pybuild
endif

override_dh_install-indep:
	dh_install -p $(TESTPKG)
	cd debian/$(TESTPKG)/usr/share/doc/python-pysam/tests; \
	    make clean; \
	    rm -f log.txt ; \
	    chmod a+x tabix_test.py

override_dh_auto_clean:
	dh_auto_clean
	cd tests; make clean
	rm -f tests/log.txt
