#!/usr/bin/make -f

# OpenMPI is only supported on a subset of architectures at this time.
# This list should remain synced with the Build-Depends and the Architecture
# line of openmpipython in debian/control.
export DH_VERBOSE=1

export COMPILE_CYTHON=1

OPENMPI_ARCH = alpha amd64 armel armhf arm64 i386 ia64 powerpc powerpcspe ppc64 ppc64el sparc sparc64 kfreebsd-i386 kfreebsd-amd64 hurd-i386
DEB_HOST_ARCH  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

pv = $(shell pyversions -vd)
PY2VERS=$(shell pyversions -vs)
PYTHON = /usr/bin/python #$(pv)

%:
	dh $@ --with python2

override_dh_clean:
	dh_clean
	rm -f Src/MPI/mpipython.mpich2 Src/MPI/mpipython.openmpi
# 	remove the cython generated file to force rebuild
	rm -f $(patsubst %.pyx,%.c,$(wildcard Scientific/*.pyx))

override_dh_auto_build:
	dh_auto_build
#       build the MPI part
	rm -f Src/MPI/*.o
	cd Src/MPI && PYTHONPATH=$$(echo $(CURDIR)/build/lib.*-$(pv)) USE_MPICH=yes   $(PYTHON) compile.py
ifneq (,$(findstring $(DEB_HOST_ARCH),$(OPENMPI_ARCH)))
	rm -f Src/MPI/*.o
	cd Src/MPI && PYTHONPATH=$$(echo $(CURDIR)/build/lib.*-$(pv)) USE_OPENMPI=yes $(PYTHON) compile.py
endif

override_dh_auto_test:
	dh_auto_test
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	set -e -x; \
	for py in $(PY2VERS); do \
		for f in Tests/*.py; do \
			PYTHONPATH=$$(echo $(CURDIR)/build/lib.*-$$py) python$$py $$f; \
		done \
	done
endif

override_dh_install:
#	rename the task_manager to avoid a name conflict
	mv debian/tmp/usr/bin/task_manager \
	   debian/tmp/usr/bin/task_manager_pyscientific
	dh_install -a -i -Npython-scientific
	dh_install -ppython-scientific -XMPI -X_netcdf
	dh_numpy

override_dh_installexamples:
	dh_installexamples
	rm -f python-netcdf/usr/share/doc/python-netcdf/examples/mpi.py

override_dh_installchangelogs:
	dh_installchangelogs -a -i -Npython-scientific-doc
	dh_installchangelogs -ppython-scientific-doc Doc/CHANGELOG
