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

PY2 = $(shell pyversions -vd)
PY2VERS = $(shell pyversions -vr)
PY3VERS = $(shell py3versions -vr)

CYTHON_VER := $(shell dpkg -l cython 2>/dev/null | awk '/^ii/{print $$3;}' || echo 0)
RECONF = $(shell dpkg --compare-versions $(CYTHON_VER) ge 0.19.1 && echo ",autoreconf" || echo "")

LAST_CHANGE = $(shell dpkg-parsechangelog -S Date)
BUILD_DATE  = $(shell LC_ALL=C date -u "+%B %d, %Y" -d "$(LAST_CHANGE)")

# Specify a specific version of MPI to use in the build, otherwise
# just use the default.
# MPI=.openmpi

export  http_proxy=http://127.0.0.1:9/
export  https_proxy=http://127.0.0.1:9/

%:
	dh $@ --with python2,python3$(RECONF),sphinxdoc --buildsystem python_distutils

cythonize:
	@echo "D: removing previously generated by Cython sources"
	find -iname *.c | xargs grep -l 'Generated by Cython' | xargs -r rm
	python setup.py build_src

override_dh_autoreconf:
	dh_autoreconf debian/rules -- cythonize

# Enforce distutils build system to build for all supported versions
# then build documentation
override_dh_auto_build:
	dh_auto_build $@ -- \
		--mpicc=/usr/bin/mpicc$(MPI) --mpicxx=/usr/bin/mpicxx$(MPI)

	: # Build for Python 3
	set -e; for v in $(PY3VERS); do \
		python$$v setup.py build \
			--mpicc=/usr/bin/mpicc$(MPI) --mpicxx=/usr/bin/mpicxx$(MPI); \
	done

	: # Build documentation now
	PYTHONPATH=`/bin/ls -d $(CURDIR)/build/lib.*$(PY2)` \
	 make -C docs/source/usrman/ html SPHINXOPTS="-D today=\"$(BUILD_DATE)\""

override_dh_auto_install:
	dh_auto_install

	: # Install for Python 3
	set -e; for v in $(PY3VERS); do \
		python$$v setup.py install  --root=$(CURDIR)/debian/tmp --install-layout=deb; \
	done

	: # Remove python-mpi binaries
	find $(CURDIR)/debian/tmp -name python-mpi -delete
	find $(CURDIR)/debian/tmp -type d -empty -delete

override_dh_install:
	dh_install

	: # create symlinks for .h files
	set -e; for v in $(PY2VERS); do \
	[ -d $(CURDIR)/debian/python-mpi4py/usr/include/python$$v ] || \
		mkdir -p $(CURDIR)/debian/python-mpi4py/usr/include/python$$v; \
		dh_link usr/lib/python$$v/dist-packages/mpi4py/include/mpi4py usr/include/python$$v/mpi4py; \
	done

	: # Python 3
	: # Can have python$$v symlink pointing to python3.?m or python3.?mu
	: # see #700995 for more details.  So first look where it points to
	: # and use that directory
	set -e; for v in $(PY3VERS); do \
	[ -d $(CURDIR)/debian/python3-mpi4py/usr/include/python$$v ] || \
		pythonv_inc_dir=$$(readlink -f /usr/include/python$$v); \
		mkdir -p $(CURDIR)/debian/python3-mpi4py$$pythonv_inc_dir; \
		dh_link -ppython3-mpi4py usr/lib/python3/dist-packages/mpi4py/include/mpi4py $${pythonv_inc_dir#/}/mpi4py; \
	done

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e; for v in $(PY2VERS) $(PY3VERS); do \
     echo "I: testing using python$$v"; \
	echo "Note: test errors here are treated as warnings"; \
	 PYTHONPATH=`/bin/ls -d $(CURDIR)/build/lib.*-$$v` \
	  /usr/bin/python$$v /usr/bin/nosetests -v --exclude='testPackUnpackExternal' -A "not network" || /bin/true ; \
	done
else
	: # Skip unittests due to nocheck
endif

override_dh_sphinxdoc:
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	dh_sphinxdoc -ppython-mpi4py-doc
endif

override_dh_installchangelogs:
	dh_installchangelogs CHANGES.rst

## Immediately useable documentation
override_dh_compress:
	dh_compress -X.py -X.html -X.css -X.jpg -X.txt -X.js -X.json -X.rtc -X.par -X.bin -Xobjects.inv

override_dh_auto_clean:
	dh_auto_clean $@
	rm -rf `find -name build -type d`
	rm -rf `find -name _build -type d`

	: # Remove Cython generated files
	rm -f src/mpi4py.MPE.c src/mpi4py.MPI.c src/include/mpi4py/mpi4py.MPI_api.h

get-orig-dev-source:
	cd ../mpi4py-hg/; \
	uver=`sed -ne "/__version__/s,.*= *'\(.*\)' *$$,\1,gp" src/__init__.py`; \
	tarball=../tarballs/mpi4py_$$uver+hg`date +"20%y%m%d"`.orig.tar.gz; \
	echo "I: Generating $$tarball"; \
	hg archive --exclude 'misc' -t tgz $$tarball;
