#!/usr/bin/make -f

# Hint:
#   https://wiki.debian.org/Python/LibraryStyleGuide
# suggests:
export PYBUILD_NAME=pysam
# which probably does not harm but due to the fact that there is
# some additional binary package a debian/python-pysam.install
# file is definitely needed.

DEBPKGNAME := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}')
TESTPKG    := $(DEBPKGNAME)-tests
pybuilddir := $(CURDIR)/build/lib.$(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)-$(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)

%:
	dh $@ --with python2

# 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
	chmod a+x tests/pysam_test_offline.py
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e -x;\
	for pyv in `pyversions -dv` ; do \
	    cd tests && env PYTHONPATH=$(pybuilddir)-$${pyv} ./pysam_test_offline.py ; \
	done
endif

override_dh_install-indep:
	dh_install -p $(TESTPKG)
	cd debian/$(TESTPKG)/var/lib/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
