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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export QT_API = PyQt5

DESTDIR = debian/geophar

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

override_dh_auto_clean:
	rm -rf .pybuild/ Geophar.egg-info/ debian/.debhelper/ \
	 debian/debhelper-build-stamp debian/files \
	 debian/geophar.postinst.debhelper \
	 debian/geophar.prerm.debhelper \
	 debian/geophar.substvars \
	 debian/geophar/ \
	 $$(find . -type d -name __pycache__) \
	 $$(find wxgeometrie/ -name tools) \
	 config/log/bienvenue_historique.log

PIXMAPDIR = debian/geophar/usr/share/pixmaps
override_dh_install:
	dh_install
	# install an icon
	install -d $(PIXMAPDIR)
	install -m 644 debian/geophar.png $(PIXMAPDIR)/geophar.png
	#######################################################
	# replace javascript libraries which were bundled in the
	# upstream package by symlinks
	#######################################################

	###################### jsMath #########################
	ln -s /usr/share/jsmath \
	  debian/geophar/usr/share/geophar/wxgeometrie/doc/html/_static/jsMath

	# remove extra license files
	find debian/geophar -name "LICENSE" -exec rm {} \;
	find debian/geophar -name "license*" -exec rm {} \;
	# fix a few wrong permissions
	find debian/geophar -name "*.py" -exec chmod 644 {} \;
	chmod 644 debian/geophar/usr/share/geophar/tools/bash_autocompletion
	chmod 755 debian/geophar/usr/share/geophar/tools/resources/geophar
	chmod 755 debian/geophar/usr/share/geophar/tools/hooks/pre-commit
	chmod 755 debian/geophar/usr/share/geophar/tools/hooks/post-commit
	# remove files which cannot be compiled by python 2
	rm -rf debian/geophar/usr/share/geophar/wxgeometrie/sympy/mpmath/tests
	rm -f debian/geophar/usr/share/geophar/wxgeometrie/sympy/mpmath/libmp/exec_py3.py
	# replace all the privacy-breaches
	for f in $$(grep -rl 'script.*src.*mathjax' $(DESTDIR)/usr/share/geophar/wxgeometrie/doc/html); do \
	  sed -i 's%https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML%http://localhost/javascript/mathjax/MathJax.js%' $$f; \
	done
	# modify the version number
	date=$$(date --utc --date="@$$(dpkg-parsechangelog --show-field Timestamp)" +"(%Y, %-m, %-d)"); \
	version=$$(dpkg-parsechangelog --show-field Version); \
	fileToChange=debian/geophar/usr/share/geophar/wxgeometrie/version.py; \
	echo version = $$version, date_version = $$date; \
	sed -i -e "s/^version =.*/version = '$$version'/" \
	  -e "s/^date_version =.*/date_version = $$date/" $$fileToChange

override_dh_auto_test:
	echo "no test for now"
	: # export QT_QPA_PLATFORM=offscreen; \
	: # export QTWEBENGINE_DISABLE_SANDBOX=1; \
	: # export MPLCONFIGDIR=.pybuild/tmp; \
	: # ln -s ../../../../tools  wxgeometrie/modules/tablatex/tests/; \
	: # ln -s ../../../../tools  wxgeometrie/modules/probabilites/tests/; \
	: # ln -s ../../../tools  wxgeometrie/geolib/tests/; \
	: # ln -s ../../../tools  wxgeometrie/mathlib/tests/; \
	: # python3 -m unittest discover -v --pattern="test_*.py"

