#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Get the supported Python versions
#PY2VERS=$(shell pyversions -vr)
PY3VERS=$(shell py3versions -vr)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
export CFLAGS ?= -O0
endif


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


build-python%:
	python$* setup.py build
	python$*-dbg setup.py build


override_dh_auto_build: $(PY3VERS:%=build-python%)
	dh_auto_build


override_dh_auto_clean:
	dh_auto_clean
	$(RM) src/epr.c
	$(RM) -r doc/_build doc/html
	$(RM) -r build
	$(RM) -r *.egg-info


# suppress dh_auto_test since it requires a large download
override_dh_auto_test:
#ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
#	set -e -x;\
#	for py in $(PY2VERS); do \
#		PYTHONPATH=$(CURDIR)/build/lib.*-$$py python$$py test/test_all.py ;\
#		PYTHONPATH=$(CURDIR)/build/lib.*-$$py-pydebug python$$py-dbg test/test_all.py ;\
#	for py in $(PY3VERS); do \
#		PYTHONPATH=$(CURDIR)/build/lib.*-$$py python$$py test/test_all.py ;\
#		PYTHONPATH=$(CURDIR)/build/lib.*-$$py-pydebug python$$py-dbg test/test_all.py ;\
#	done
#endif


override_dh_install:
	# Install everything excluding the *_d.so debug extensions to python-epr
	dh_install -X"*_d.so" "debian/tmp/usr/lib/python2*" -p python-epr
	dh_install -X"*dmu.so" -X"*dm.so" "debian/tmp/usr/lib/python3*" -p python3-epr

	# Install the debug extensions to python-epr-dbg
	dh_install "debian/tmp/usr/lib/python2*/*-packages/*_d.so" -p python-epr-dbg
	dh_install "debian/tmp/usr/lib/python3*/*-packages/*.cpython-??dm*.so" -p python3-epr-dbg
	find debian/python3-epr -name '*.cpython-??dm-*.so' | xargs -r rm -f

	# Continue with regular dh_install
	dh_install
	dh_numpy
	dh_numpy3


install-python%:
	python$* setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb
	python$*-dbg setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb


override_dh_auto_install: $(PY3VERS:%=install-python%)
	dh_auto_install


override_dh_installchangelogs:
	dh_installchangelogs -k doc/NEWS.txt


override_dh_compress:
	dh_compress -X.txt


override_dh_sphinxdoc:
ifneq "$(shell dh_listpackages | grep -- -doc)" ""
	dh_sphinxdoc
endif


override_dh_installdocs:
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -C doc html
else
	mkdir -p doc/html
endif
	dh_installdocs


.PHONY: override_dh_auto_build override_dh_auto_clean override_dh_auto_test \
        override_dh_installchangelogs override_dh_compress override_dh_install \
        override_dh_auto_install dh_sphinxdoc override_dh_installdocs
