#!/usr/bin/make -f

PY2VERS := $(shell pyversions -r)
PY3VERS := $(shell py3versions -r)

%:
	dh $(@) --with python2,python3,sphinxdoc -Spython_distutils

override_dh_auto_clean:
	rm -rf build
	rm -rf docs/build/html
	rm -rf *.egg-info
	find . -name *.coverage.* -delete
	rm -f man/circuits.*3.1
	dh_auto_clean

override_dh_auto_build:
	set -e -x; \
	for python in $(PY2VERS); do \
		$$python setup.py build -e /usr/bin/python; \
	done
	for python in $(PY3VERS); do \
		$$python setup.py build -e /usr/bin/python3; \
	done

	PYTHONPATH=. sphinx-build -N -bhtml docs/source docs/build/html

	# Copy manpages for Python3 scripts
	cp man/circuits.bench.1 man/circuits.bench3.1
	cp man/circuits.web.1 man/circuits.web3.1
	sed -i 's/circuits.bench/circuits.bench3/g' man/circuits.bench3.1
	sed -i 's/circuits.web/circuits.web3/g' man/circuits.web3.1

override_dh_auto_install:
	set -e -x; \
	for python2 in $(PY2VERS); do \
		$$python2 setup.py install --skip-build --root debian/python-circuits \
			--install-layout deb; \
	done

	set -e -x; \
	for python3 in $(PY3VERS); do \
		$$python3 setup.py install --skip-build --root debian/python3-circuits \
			--install-layout deb; \
	done

	# Remove tests to not pollute namespace.
	rm -rf debian/python*-circuits/usr/lib/python*/dist-packages/tests

	# Rename scripts for Python3
	mv debian/python3-circuits/usr/bin/circuits.bench \
	   debian/python3-circuits/usr/bin/circuits.bench3
	mv debian/python3-circuits/usr/bin/circuits.web \
	   debian/python3-circuits/usr/bin/circuits.web3

	# --excutable option in build has no effect for circuits.web's entrypoint
	sed -i '1c#!/usr/bin/python' debian/python-circuits/usr/bin/circuits.web
	sed -i '1c#!/usr/bin/python3' debian/python3-circuits/usr/bin/circuits.web3

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	# Upstream is calling directly py.test inside a Popen so, for now, tests
	# are run only for the default version
	set -e; python setup.py test;
endif

override_dh_installchangelogs:
	dh_installchangelogs CHANGES.rst
