#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

export PYBUILD_NAME=sasview

# Enable additional hardening options for the binaries but not PIE since it is
# not wanted for shared libraries.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# SasView insists on writing to ~/sasview.log
export HOME = $(CURDIR)/build/

# Force pdflatex to use SOURCE_DATE_EPOCH for \today too
export FORCE_SOURCE_DATE = 1


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

override_dh_auto_clean:
	dh_auto_clean
	# clean up docs
	rm -rf docs/sphinx-docs/build docs/sphinx-docs/source-temp otherdocs
	# clean up tests
	find test -name logs -type d -exec rm -rf {} +
	rm -rf test/tests.log .cache test/.cache

override_dh_auto_build-indep:
	# build the arch-dep packages to be able to build the docs
	dh_auto_build
	# reassemble the docs from dependent packages to make the overall
	./debian/fetchdocs
	python3 setup.py docs
	# Strip absolute build paths from HTML documentation
	mkdir -p docs/sphinx-docs/build/html/_static/
	find -type f -name '*.html' -print0 | \
	  xargs -0r sed -i -e "s@$(CURDIR)/`echo build/lib.*`@path/to@g"
	ln -sf /usr/share/javascript/mathjax/ docs/sphinx-docs/build/html/_static/

override_dh_auto_test-indep:
	# nothing to do

override_dh_auto_test-arch:
	# Run most of the utests with pytest
	dh_auto_test
	# Run some additional GUI tests
	# they fail upstream at present so run them without failing the build
	PYBUILD_SYSTEM=custom \
	PYBUILD_BEFORE_TEST="cp -r {dir}/src/sas/qtgui {build_dir}" \
	PYBUILD_TEST_ARGS="cd {build_dir}/qtgui; xvfb-run -a --server-args='-screen 0 1024x768x24' python3 GUITests.py" \
	PYBUILD_AFTER_TEST="rm -rf {build_dir}/qtgui" \
	dh_auto_test || true

override_dh_install:
	mkdir -p debian/sasview/usr/bin
	find debian/python3-sasview \( -name convert\*.sh -o -name convert\*.bat -o -name Thumbs.db \) -delete
	mv debian/python3-sasview/usr/bin/sasview debian/sasview/usr/bin/sasview
	dh_install -p sasview debian/sasview.desktop usr/share/applications
	dh_install -p sasview debian/sasview.{png,svg} usr/share/pixmaps
	dh_numpy3

override_dh_python3:
	dh_python3
	rm -rf debian/python3-sasview/usr/lib/python3/dist-packages/qtgui
