#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

export CC=mpicc
export CXX=mpicxx

export PRTE_MCA_plm_ssh_agent=/bin/false
export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe

%:
	dh $@

# Strip the +dfsg repack suffix: upstream's build system parses this string.
UPSTREAM_VERSION := $(patsubst %+dfsg,%,$(DEB_VERSION_UPSTREAM))

override_dh_autoreconf:
	echo $(UPSTREAM_VERSION) > .tarball-version
	echo $(UPSTREAM_VERSION) > sc/.tarball-version
	dh_autoreconf ./bootstrap

override_dh_auto_configure:
	dh_auto_configure -- \
		--enable-mpi \
		--enable-mpiio

# The GitHub tarball of p4est ships libsc (sc/) only as an empty git
# submodule, so assemble and orig tarball the p4est and libsc release
# tarballs and drop the files listed in Files-Excluded in debian/copyright.
ORIG_NAME := p4est-$(DEB_VERSION_UPSTREAM)
get-orig-source:
	test -f p4est-$(UPSTREAM_VERSION).tar.gz || wget -O p4est-$(UPSTREAM_VERSION).tar.gz \
		https://github.com/cburstedde/p4est/archive/v$(UPSTREAM_VERSION).tar.gz
	test -f libsc-$(UPSTREAM_VERSION).tar.gz || wget -O libsc-$(UPSTREAM_VERSION).tar.gz \
		https://github.com/cburstedde/libsc/archive/v$(UPSTREAM_VERSION).tar.gz
	set -e; tmp=$$(mktemp -d); \
		mkdir -p $$tmp/$(ORIG_NAME)/sc; \
		tar --strip-components=1 -C $$tmp/$(ORIG_NAME) -xzf p4est-$(UPSTREAM_VERSION).tar.gz; \
		tar --strip-components=1 -C $$tmp/$(ORIG_NAME)/sc -xzf libsc-$(UPSTREAM_VERSION).tar.gz; \
		cd $$tmp/$(ORIG_NAME); \
		rm -f doc/*.pdf sc/doc/*.pdf doc/2002siamnews/*.pdf; \
		cd $$tmp; \
		tar --sort=name --owner=0 --group=0 --numeric-owner \
			-cJf $(CURDIR)/../p4est_$(DEB_VERSION_UPSTREAM).orig.tar.xz $(ORIG_NAME); \
		rm -rf $$tmp

.PHONY: get-orig-source
