#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie

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}"

%:
	dh $@ --with autoreconf,python3 #--parallel

override_dh_auto_install:
	dh_auto_install -- install-data

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_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/$(DEBPKGNAME)*/$(DEBPKGNAME)/*.hpp" usr/include/$(DEBPKGNAME) \
		    --movedev "debian/tmp/usr/include/$(DEBPKGNAME)*/$(DEBPKGNAME)/*.h" usr/include/$(DEBPKGNAME) \
			debian/tmp/usr/lib/*/lib$(DEBPKGNAME)-*.so

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

override_dh_auto_clean:
	set -e && for i in $(build3vers); do \
	  cd swig/python ; \
          python$$i ./setup.py clean --all 2> /dev/null; \
	  cd ../.. ; \
	done
	dh_auto_clean

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 -- all bin/generate_sequence bin/test_all
#	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	
