#!/usr/bin/make -f
# -*- coding: utf-8 -*-

#export DH_VERBOSE=1

PY3VERSIONS = $(shell py3versions -vr)

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

override_dh_auto_build:
	dh_auto_build

	set -e; for version in $(PY3VERSIONS); do \
	  python$$version setup.py build; \
	done

	$(MAKE) -C docs html

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	PYBUILD_SYSTEM=custom PYBUILD_TEST_ARGS="{interpreter} -c 'import SimPy; SimPy.test()'" dh_auto_test
endif

override_dh_installchangelogs:
	dh_installchangelogs
	# install the upstream changelog at the right place in the doc package
	dh_installchangelogs -ppython-simpy-doc docs/source/Manuals/HISTORY.rst

override_dh_compress:
	dh_compress -X.py -X.txt

override_dh_auto_install:
	# Replace broken shebangs with /usr/bin/python3
	find build/ -name '*.py' -exec sed -i -e '1s|^#!.*python.*|#!/usr/bin/python3|' '{}' ';'

	dh_auto_install

	set -e; for version in $(PY3VERSIONS); do \
	  python$$version setup.py install --no-compile -O0 --install-layout=deb --root $(CURDIR)/debian/tmp; \
	done

override_dh_auto_clean:
	dh_auto_clean

	set -e; for version in $(PY3VERSIONS); do \
	  python$$version setup.py clean; \
	done

	rm -rf SimPy/__pycache__/

	rm -rf docs/html

override_dh_python3:
	dh_python3

	mkdir -p $(CURDIR)/debian/python-simpy-gui/usr/lib/python3/dist-packages/SimPy/
	for guifile in GUIDebug.py SimGUI.py SimPlot.py SimulationGUIDebug.py tkconsole.py tkprogressbar.py ; do \
		mv $(CURDIR)/debian/python3-simpy/usr/lib/python3/dist-packages/SimPy/$$guifile $(CURDIR)/debian/python-simpy-gui/usr/lib/python3/dist-packages/SimPy/ ; \
	done