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

#export DH_VERBOSE=1

VERSION = $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(\([0-9]\+\):\)\?\(.*\)-.*/\3/p')

PY2VERSIONS = $(shell pyversions -vr)
PY3VERSIONS = $(shell py3versions -vr)

%:
	dh $@ --with python2,python3,sphinxdoc

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)))
	set -e; for version in $(PY2VERSIONS) $(PY3VERSIONS); do \
	  python$$version -c 'import SimPy; SimPy.test()'; \
	done
endif

override_dh_install:
	dh_install --fail-missing

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/python
	find build/ -name '*.py' -exec sed -i -e '1s|^#!.*python.*|#!/usr/bin/python|' '{}' ';'

	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

	# Remove deprecated python3 gui stuff
	cd $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/SimPy && \
	    rm GUIDebug.py SimGUI.py SimPlot.py SimulationGUIDebug.py tkconsole.py tkprogressbar.py

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
