#!/usr/bin/make -f
export DH_VERBOSE=1
export PYBUILD_NAME=khmer
export PYBUILD_INSTALL_ARGS='--install-scripts=/usr/lib/khmer/bin'
export PYBUILD_DESTDIR_python3=debian/khmer
export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow
export DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
# Force C++ library build to use Debian zlib1g-dev/libbz2-dev
export USE_SYSTEM_ZLIB=true
export USE_SYSTEM_LIBBZ2=true

export HOME=$(shell echo $$PWD"/fakehome")

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

override_dh_auto_build:
	dh_auto_build
	ln -sf `pybuild --print build_dir|awk -F: '{print $$2}'`/khmer/*.so khmer && \
		./setup.py develop --user && sphinx-build doc build/sphinx/html
	asciidoctor --backend manpage -a reproducible debian/khmer.1.adoc
	$(MAKE) -C src/oxli clean
	$(MAKE) -C src/oxli all USE_SYSTEM_ZLIB="true" USE_SYSTEM_LIBBZ2="true"

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_install
	PYBUILD_SYSTEM=custom \
	    PYBUILD_TEST_ARGS="cd {dir}; export PATH={destdir}/usr/lib/khmer/bin:$$PATH ; \
	    cd {build_dir}; export PYTHONPATH=$$(pwd); {interpreter} -m pytest \
	    -m 'not known_failing and not jenkins and not huge and not noroot'\
	    -q" dh_auto_test
endif

override_dh_auto_clean:
	dh_auto_clean || /bin/true
	cd src/oxli && $(MAKE) clean
	rm -rf __pycache__ .eggs

override_dh_install:
	cd src/oxli && $(MAKE) PREFIX=../debian/tmp install
	dh_install
	find debian/khmer -name .gitignore -delete

override_dh_sphinxdoc:
	dh_sphinxdoc
	rdfind -makesymlinks true -followsymlinks true\
		/usr/lib/python3/dist-packages/guzzle_sphinx_theme \
		$(shell find debian/khmer-common | grep -i -v citation)
	if [ -f debian/khmer-common/usr/share/doc/khmer-common/html/_sources/citations.rst.txt ]; then \
	  rm debian/khmer-common/usr/share/doc/khmer-common/html/_sources/citations.rst.txt && \
	  cd debian/khmer-common/usr/share/doc/khmer-common/html/_sources/ && \
		ln -s ../../CITATION citations.rst.txt; fi
