#!/usr/bin/make -f

PYTHON2=$(shell pyversions -dv)
#PYTHON3=$(shell py3versions -sv)

# $HOME must exist for tox to be happy, but an sbuild environment sets it to a
# non-existent directory.  Use this tempdir as the fake $HOME for testing
# purposes.
FAKEHOME=$(shell mktemp -dq)

# Prevent setuptools/distribute from accessing the internet.
export http_proxy = http://127.0.9.1:9

%:
	dh $@ --buildsystem=python_distutils --with=python2,sphinxdoc

override_dh_installdocs:
	python setup.py build_sphinx
	dh_installdocs build/sphinx/html

override_dh_installman:
	(cd debian/manpage; \
	 sphinx-build -b man -d _build/doctrees . _build/man)
	dh_installman debian/manpage/_build/man/tox.1

override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build
	rm -rf *.egg-info
	rm -rf debian/manpage/_build

# $HOME must exist for tox to be happy.  
#
# https://bitbucket.org/hpk42/tox/issue/105/hidden-dependency-on-home#comment-5176027
#
# Also, because the tox(1) script doesn't exist until the package is
# installed (although I suppose we could run it out of the built
# location), invoke the main() function explicitly. 

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
test-python%:
	PYTHONPATH=.:/usr/lib/python$*/dist-packages \
	HOME=/tmp python$* -c 'from tox._cmdline import main; main(["-e", "py"+"$*".replace(".", "")])'

override_dh_auto_test: $(PYTHON2:%=test-python%) $(PYTHON3:%=test-python%)
endif
