#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

build3vers := $(shell py3versions -sv)

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

export DH_OPTIONS
export PKG_CONFIG_LIBDIR=${CURDIR}
export PKG_CONFIG_ALLOW_SYSTEM_LIBS=true
export PKG_CONFIG_SYSROOT_DIR=${CURDIR}/debian/tmp/
export PERL_MM_OPT=INSTALLDIRS=vendor
export PYBUILD_BUILD_ARGS=build_ext --rpath "${CURDIR}/debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}"

export DEB_LDFLAGS_MAINT_APPEND = -lpthread

%:
	dh $@ # --with python3

override_dh_auto_install:
	dh_auto_install -- install-data

override_dh_installexamples:
	dh_installexamples
	sed '/SRCDIR=/d' debian/jellyfish-examples/usr/share/doc/jellyfish-examples/examples/compat.sh*
	sed '/BUILDDIR=/d' debian/jellyfish-examples/usr/share/doc/jellyfish-examples/examples/compat.sh*
	rm -f debian/jellyfish-examples/usr/share/doc/jellyfish-examples/examples/swig*

override_dh_install:
	# dh_install -X*.a -X*.la -Xpkgconfig
#	dh_install -ppython3-dna-jellyfish
#	pybuild -d swig/python --name dna-jellyfish
##	pybuild -d swig/python --name dna-jellyfish --disable test/python3
##	pybuild -d swig/python --name dna-jellyfish --test --test-args "${CURDIR}/swig/python/ test_string_mers.py"
##	pybuild -d swig/python --name dna-jellyfish --test --test-args "${CURDIR}/swig/python/ test_hash_counter.py"
#	chrpath --delete debian/*/usr/lib/python*/dist-packages/_dna_jellyfish*.so
	
	dh_auto_configure --sourcedirectory=swig/perl5
##	dh_auto_configure --sourcedirectory=swig/python
	dh_auto_build --sourcedirectory=swig/perl5
	dh_auto_install --sourcedirectory=swig/perl5
	chrpath --delete debian/tmp/usr/lib/*/perl5/*/auto/jellyfish/jellyfish.so
	dh_install
	d-shlibmove --commit \
		    --multiarch \
		    --devunversioned \
		    --movedev "debian/tmp/usr/include/$(DEB_SOURCE)*/$(DEB_SOURCE)/*.hpp" usr/include/$(DEB_SOURCE) \
		    --movedev "debian/tmp/usr/include/$(DEB_SOURCE)*/$(DEB_SOURCE)/*.h" usr/include/$(DEB_SOURCE) \
			debian/tmp/usr/lib/*/lib$(DEB_SOURCE)-*.so
	sed -i "/dependency_libs/ s/'.*'/''/" ./debian/libjellyfish-2.0-dev/usr/lib/${DEB_HOST_MULTIARCH}/*.la

override_dh_auto_configure:
	dh_auto_configure -- --enable-swig --enable-perl-binding # --enable-python-binding

override_dh_clean:
	dh_clean
	if [ -d debian/tmp_save_tests ] ; then \
	    mv debian/tmp_save_tests/* tests ; \
	    rmdir debian/tmp_save_tests ; \
	fi
	rm -f tests/compat.sh
	rm -f swig/python/jellyfish* # swig/python/dna_jellyfish.py

override_dh_auto_build:
	mkdir -p debian/tmp_save_tests
	cp -a tests/* debian/tmp_save_tests
	dh_auto_build --no-parallel -- all bin/generate_sequence bin/test_all bin/fastq2sam
#	cd doc ;  RUBYLIB=$PWD PATH=$PATH:../bin make clean all ; cd ..
#	broken

# jellyfish contains a *really* big test which should *not* run on autobuilders
# if you have a *really* large machine you can try to activate this test by
# uncommenting the following.
#override_dh_auto_test:
#	BIG=1 dh_auto_test	
