#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

PYBUILD_NAME_SERIAL=h5py_serial
PYBUILD_NAME_MPI=h5py_mpi

BUILD_DIR_SERIAL=build-$(PYBUILD_NAME_SERIAL)
BUILD_DIR_MPI=build-$(PYBUILD_NAME_MPI)

SUBMODULE_SERIAL=_debian_$(PYBUILD_NAME_SERIAL)
SUBMODULE_MPI=_debian_$(PYBUILD_NAME_MPI)

PYBUILD_DIR_SERIAL=$(shell PYBUILD_NAME=$(PYBUILD_NAME_SERIAL) pybuild -p `py3versions -dv` --print {build_dir})
PYBUILD_DIR_MPI=$(shell PYBUILD_NAME=$(PYBUILD_NAME_MPI) pybuild -p `py3versions -dv` --print {build_dir})

export OMPI_MCA_plm_rsh_agent=/bin/false
export OMPI_MCA_rmaps_base_oversubscribe=1

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

override_dh_clean:
	dh_clean
	rm -rf $(BUILD_DIR_SERIAL) $(BUILD_DIR_MPI)

override_dh_auto_clean:
	dh_auto_clean -D $(BUILD_DIR_SERIAL) || /bin/true
	dh_auto_clean -D $(BUILD_DIR_MPI) || /bin/true
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -C docs clean
	$(MAKE) -C docs_api clean
endif

override_dh_auto_configure:
	mkdir $(BUILD_DIR_SERIAL); bash -O extglob -c "cp -ra ./!($(BUILD_DIR_SERIAL)) $(BUILD_DIR_SERIAL)"
	sed "s/'h5py'/'h5py.$(SUBMODULE_SERIAL)'/g; s/'h5py._hl'/'h5py.$(SUBMODULE_SERIAL)._hl'/g; s/'h5py.tests/'h5py.$(SUBMODULE_SERIAL).tests/g; s/'h5py.x/'h5py.$(SUBMODULE_SERIAL).x/g" -i $(BUILD_DIR_SERIAL)/setup.py
	sed "s/flavour=''/flavour='$(SUBMODULE_SERIAL)'/" -i $(BUILD_DIR_SERIAL)/setup_build.py
	mv $(BUILD_DIR_SERIAL)/h5py $(BUILD_DIR_SERIAL)/$(SUBMODULE_SERIAL)
	cp -r debian/wrapper_module/h5py $(BUILD_DIR_SERIAL)
	mv $(BUILD_DIR_SERIAL)/$(SUBMODULE_SERIAL) $(BUILD_DIR_SERIAL)/h5py
	mkdir $(BUILD_DIR_MPI); bash -O extglob -c "cp -ra ./!($(BUILD_DIR_SERIAL)|$(BUILD_DIR_MPI)) $(BUILD_DIR_MPI)"
	sed "s/'h5py'/'h5py.$(SUBMODULE_MPI)'/g; s/'h5py._hl'/'h5py.$(SUBMODULE_MPI)._hl'/g; s/'h5py.tests/'h5py.$(SUBMODULE_MPI).tests/g; s/'h5py.x/'h5py.$(SUBMODULE_MPI).x/g" -i $(BUILD_DIR_MPI)/setup.py
	sed "s/flavour=''/flavour='$(SUBMODULE_MPI)'/" -i $(BUILD_DIR_MPI)/setup_build.py
	mv $(BUILD_DIR_MPI)/h5py $(BUILD_DIR_MPI)/$(SUBMODULE_MPI)
	cp -r debian/wrapper_module/h5py $(BUILD_DIR_MPI)
	mv $(BUILD_DIR_MPI)/$(SUBMODULE_MPI) $(BUILD_DIR_MPI)/h5py
	cd $(BUILD_DIR_SERIAL); PYBUILD_NAME=$(PYBUILD_NAME_SERIAL) python3 setup.py configure --hdf5-pc=hdf5-serial
	cd $(BUILD_DIR_MPI); PYBUILD_NAME=$(PYBUILD_NAME_MPI) CC=h5pcc python3 setup.py configure --mpi --hdf5-pc=hdf5-mpi

build-mpi:
	PYBUILD_NAME=$(PYBUILD_NAME_MPI) CC=h5pcc dh_auto_build -D $(BUILD_DIR_MPI)
	for DIR in $$(find .pybuild/cpython3*mpi -name build); do \
		cp -r $(CURDIR)/debian/wrapper_module/h5py/* $$DIR/h5py/; \
	done

override_dh_auto_build-arch: export http_proxy=127.0.0.1:9
override_dh_auto_build-arch: export https_proxy=127.0.0.1:9
override_dh_auto_build-arch: build-mpi
	PYBUILD_NAME=$(PYBUILD_NAME_SERIAL) dh_auto_build -a -D $(BUILD_DIR_SERIAL)
	for DIR in $$(find .pybuild/cpython3*serial -name build); do \
		cp -r $(CURDIR)/debian/wrapper_module/h5py/* $$DIR/h5py/; \
	done

override_dh_auto_build-indep: export http_proxy=127.0.0.1:9
override_dh_auto_build-indep: export https_proxy=127.0.0.1:9
override_dh_auto_build-indep: build-mpi
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	PYTHONPATH=$(PYBUILD_DIR_MPI) mpirun -n 1 $(MAKE) -C docs html
	PYTHONPATH=$(PYBUILD_DIR_MPI) mpirun -n 1 $(MAKE) -C docs_api html
endif

test-mpi:
ifneq (nocheck,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	set -e; \
	for DIR in $$(find .pybuild/cpython3*mpi -name build); do \
		echo "Running MPI tests on $$DIR"; \
		cd $$DIR; PYTHONPATH=. mpirun -n 2 python$$(echo $$DIR | sed -e 's@.*cpython._\([[:digit:].]*\(_dbg\)*\).*_h5py.*@\1@g; s/_/-/') \
		$(CURDIR)/debian/tests/test_file_mpi.py; \
	cd ../../..; done;
else
	echo "MPI tests not run since nocheck was set in DEB_BUILD_OPTIONS"
endif

override_dh_auto_test-indep: test-mpi

override_dh_auto_test-arch: test-mpi
ifneq (nocheck,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	set -e; \
	for DIR in $$(find .pybuild/cpython3*serial -name build); do \
		echo "Running serial tests on $$DIR"; \
		cd $$DIR; PYTHONPATH=. python$$(echo $$DIR | sed -e 's@.*cpython._\([[:digit:].]*\(_dbg\)*\).*_h5py.*@\1@g; s/_/-/') \
		-c "from sys import exit; import h5py; exit(h5py.run_tests('-v'))"; \
	cd ../../..; done;
else
	echo "Tests not run since nocheck was set in DEB_BUILD_OPTIONS"
endif

override_dh_auto_install-indep:

override_dh_auto_install-arch: $(eval SHELL := /bin/bash)
	PYBUILD_NAME=$(PYBUILD_NAME_SERIAL) dh_auto_install -a -D $(BUILD_DIR_SERIAL)
	PYBUILD_NAME=$(PYBUILD_NAME_MPI) CC=h5pcc dh_auto_install -a -D $(BUILD_DIR_MPI)

override_dh_python3-arch:
	PYBUILD_NAME=$(PYBUILD_NAME_SERIAL) dh_python3
	PYBUILD_NAME=$(PYBUILD_NAME_MPI) dh_python3
	dh_numpy3
	
	serial_egg=debian/python3-h5py-serial/usr/lib/python3/dist-packages/h5py.$(SUBMODULE_SERIAL)-*.egg-info; \
	h5py_egg=$$(echo $${serial_egg} | sed "s/h5py.$(SUBMODULE_SERIAL)/h5py/" ); \
	cp -ra $${serial_egg} $${h5py_egg}; \
	sed "s/Name: h5py.*/Name: h5py/" -i $${h5py_egg}/PKG-INFO
	
	rm -f debian/python3-h5py-mpi/usr/lib/python3/dist-packages/h5py/__init__.py

override_dh_installdocs-indep:
	dh_installdocs
	grep "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js" debian/python-h5py-doc/usr/share/doc/python-h5py-doc/html/* -R --files-with-matches | xargs sed "s|https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js|file://usr/share/javascript/mathjax/unpacked/latest.js|g" -i

override_dh_strip-arch:
	dh_strip --package=python3-h5py-serial \
		--dbg-package=python3-h5py-serial-dbg
	dh_strip --package=python3-h5py-mpi \
		--dbg-package=python3-h5py-mpi-dbg
