#!/usr/bin/make -f

PYTHON2=$(shell pyversions -r)
PYTHON3=$(shell py3versions -r)

%:
	dh $@ --with python2,python3

override_dh_auto_build:
	set -e; \
	for python in $(PYTHON2) $(PYTHON3); do \
		$$python setup.py build; \
		$$python-dbg setup.py build; \
	done

override_dh_install:
	set -e; \
	for python in $(PYTHON2); do \
		$$python     setup.py install --install-layout=deb --root=$(CURDIR)/debian/python-ujson; \
		$$python-dbg setup.py install --install-layout=deb --root=$(CURDIR)/debian/python-ujson-dbg; \
	done
	for python in $(PYTHON3); do \
		$$python     setup.py install --install-layout=deb --root=$(CURDIR)/debian/python3-ujson; \
		$$python-dbg setup.py install --install-layout=deb --root=$(CURDIR)/debian/python3-ujson-dbg; \
	done

override_dh_strip:
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -ppython-ujson  --dbg-package=python-ujson-dbg
	dh_strip -ppython3-ujson --dbg-package=python3-ujson-dbg
endif

override_dh_installdocs:
	dh_installdocs -ppython-ujson-dbg  --link-doc=python-ujson
	dh_installdocs -ppython3-ujson-dbg --link-doc=python3-ujson
	dh_installdocs
	dh_installdocs README.rst

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	-for python in $(PYTHON2); do \
		LIB=$$($$python -c "from distutils.command.build import build ; from distutils.core import Distribution ; b = build(Distribution()) ; b.finalize_options() ; print (b.build_platlib)") ; \
		PYTHONPATH=$(CURDIR)/$$LIB $$python tests/tests.py ; \
		LIB=$$($$python-dbg -c "from distutils.command.build import build ; from distutils.core import Distribution ; b = build(Distribution()) ; b.finalize_options() ; print (b.build_platlib)") ; \
		PYTHONPATH=$(CURDIR)/$$LIB $$python-dbg tests/tests.py ; \
	done
	2to3 --no-diffs -n --add-suffix='3' -w tests/tests.py
	-for python in $(PYTHON3); do \
		LIB=$$($$python -c "from distutils.command.build import build ; from distutils.core import Distribution ; b = build(Distribution()) ; b.finalize_options() ; print (b.build_platlib)") ; \
		PYTHONPATH=$(CURDIR)/$$LIB $$python tests/tests.py3 ; \
		LIB=$$($$python-dbg -c "from distutils.command.build import build ; from distutils.core import Distribution ; b = build(Distribution()) ; b.finalize_options() ; print (b.build_platlib)") ; \
		PYTHONPATH=$(CURDIR)/$$LIB $$python-dbg tests/tests.py3 ; \
	done
	rm tests/tests.py3
endif
