#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --with python3 --buildsystem pybuild

lfs:
	cd pyx/data/lfs; python3 createlfs.py

doc: lfs
	make -C faq all
	make -C manual all

override_dh_auto_clean:
	make -C faq clean
	make -C manual clean
	rm -rf manual/_build faq/_build
	cd examples; rm -f *eps */*eps *svg */*svg *pdf */*pdf *png */*png
	rm -f output
	cd pyx/data/lfs; rm -f *.lfs *.aux *.log
	dh_auto_clean

override_dh_auto_build-indep: doc

override_dh_auto_build-arch: lfs
	dh_auto_build

override_dh_auto_install:
	dh_auto_install
	rm $(CURDIR)/debian/tmp/usr/lib/python*/*-packages/pyx/data/lfs/createlfs.*
	rm -f $(CURDIR)/debian/tmp/usr/lib/python*/*-packages/output

override_dh_installdocs-indep:
	mv manual/_build/html/ manual/_build/manual/
	mv faq/_build/html/ faq/_build/faq/
	dh_installdocs -Xobjects.inv
	ln -sf /usr/share/javascript/mathjax/ \
	  debian/python3-pyx-doc/usr/share/doc/python3-pyx/manual/_static/mathjax
	dh_sphinxdoc

execute_after_dh_installexamples-indep:
	# A handful of examples have "#!/usr/bin/env python" that needs
	# cleaning up in the package.
	find debian/python*doc/usr/share/doc/python3-pyx/examples/ \
	  -name \*py \
	  -exec grep -l -e '#!/usr/bin/env' {} + | \
	  xargs sed -i -r 's/(#!\/usr\/bin\/env python)$$/\13/'

override_dh_compress:
	dh_compress --all -X.pdf -X.ipynb

override_dh_auto_test-arch:
	# Get pybuild to build the examples as a test that the build went OK
	PYBUILD_SYSTEM=custom \
	  PYBUILD_BEFORE_TEST="cp -r {dir}/examples {build_dir}" \
	  PYBUILD_AFTER_TEST="rm -rf {build_dir}/examples" \
	  PYBUILD_TEST_ARGS='cd {build_dir} && {interpreter} -u {dir}/debian/examples-testrunner' \
	  dh_auto_test --buildsystem=pybuild

override_dh_auto_test-indep:

.PHONY: doc lfs
