#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export LC_ALL=C.UTF-8
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

export PYBUILD_BEFORE_TEST=cp -r {dir}/tests/data {build_dir}
export PYBUILD_TEST_PYTEST=1
export PYBUILD_TEST_ARGS=\
 --ignore=test_app/test_plugins.py \
 -k 'not test_pickleable_member_roundtrip \
 and not test_pickleable_roundtrip \
 and not internet \
 and not test_load_from_url \
 '
export PYBUILD_NAME=cogent3
ifneq (,$(filter $(DEB_HOST_ARCH), armel armhf mipsel s390x mips64el))
export PYBUILD_DISABLE=test
endif

include /usr/share/dpkg/default.mk

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

override_dh_clean:
	#These files are rebuilt on-the-fly
	for f in `find -name '*.c'` ; do \
	    head -n 1 "$$f" | grep -q '^/\* Generated by Cython' && rm "$$f" ; \
	done
	rm -rf build build-stamp
	rm -rf doc/_build
	dh_clean

execute_after_dh_auto_build:
ifeq (,$(filter $(DEB_HOST_ARCH), armel mipsel))
	cd doc; PYTHONPATH=$(CURDIR)/src $(MAKE) html
	# remove superfluous files and dirs
	find doc/_build -name "*.doctree*" -delete
	rm -f doc/_build/html/_sources/COGENT_LICENSE.txt
	rm -f doc/_build/html/_static/jquery.js
	rm -f doc/_build/html/_static/underscore.js
	rm -rf doc/_build/html/_images/math/
endif

override_dh_sphinxdoc-indep:
	dh_sphinxdoc -i
	sed -i 's%https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js%file:///usr/share/javascript/requirejs/require.min.js%' `find $(CURDIR)/debian/python-cogent-doc/usr/share/doc/python-cogent-doc/html/ -type f -name "*.html"`
	# Tackle privacy breaches caused by MathML.
	sed -i 's%https://cdn.jsdelivr.net/npm/mathjax\@4/tex-mml-chtml.js%file:///usr/share/nodejs/mathjax-full/es5/tex-mml-chtml.js%' `find $(CURDIR)/debian/python-cogent-doc/usr/share/doc/python-cogent-doc/html/ -type f -name "*.html"`
	# Clean fonts that made their way to the documentation package.
	rm -rvf $(DOCDIR)/html/_static/fonts/

execute_before_dh_auto_test:
	for d in .pybuild/cpython3_*_cogent3; do \
	    cp pyproject.toml $$d/; \
	done

execute_after_dh_shlibdeps:
	dh_numpy3

override_dh_compress:
	dh_compress --exclude=.js --exclude=.fasta --exclude=.txt --exclude=.pdb

execute_after_dh_installdocs:
	rm -f debian/*/usr/share/doc/*/html/.nojekyll
	find debian -name doctrees -type d | xargs rm -rf

execute_after_dh_fixperms:
	find debian -path '*/cogent3/evolve/coevolution.py' -exec chmod -x {} +

