#!/usr/bin/make -f

PY2VERS=$(shell pyversions -vr)
PY3VERS=$(shell py3versions -vr)
PYVERS=$(PY2VERS) $(PY3VERS)
DPKG_EXPORT_BUILDFLAGS=1
include /usr/share/dpkg/buildflags.mk

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	rm -f build-*
	rm -rf build
	rm -rf docs/_build
	find . -name *\.py[co] -exec rm {} \;
	dh_clean

build: build-indep build-arch

build-arch: $(PYVERS:%=build-python%)
	touch $@

build-python%:
	dh_testdir
	python$* setup.py build
	touch $@

build-indep:
	make -C $(CURDIR)/docs man
	touch $@

install: $(PYVERS:%=install-python%)

install-python%: build
	if echo "$*" | grep -q 3\..* ; then \
		python$* setup.py install --install-layout=deb --root $(CURDIR)/debian/python3-gmpy2/; \
	else \
		python$* setup.py install --install-layout=deb --root $(CURDIR)/debian/python-gmpy2/; \
	fi

test: $(PYVERS:%=test-python%)

test-python%: install
	if echo "$*" | grep -q 3\..* ; then \
		PYTHONPATH=$(CURDIR)/debian/python3-gmpy2/usr/lib/python3/dist-packages:$(PYTHONPATH) python$* test/runtests.py; \
	else \
		PYTHONPATH=$(CURDIR)/debian/python-gmpy2/usr/lib/python$*/dist-packages:$(PYTHONPATH) python$* test/runtests.py; \
	fi

binary-indep: build install test

binary-arch: build install test
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installman
	dh_installexamples
	dh_python2
	dh_python3
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary test install
