#!/usr/bin/make -f
BUILDDIR = $(CURDIR)/debian/build

%:
	dh $@ --buildsystem=cmake --builddirectory=$(BUILDDIR)

export OMPI_MCA_plm_rsh_agent=/bin/false		#workaround to start MPI-applications in chroot
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

disable_auto_test_archs_simpl = armel armhf mips64el
disable_auto_test_archs_mpi = hurd-i386 mips mipsel mips64el s390 s390x powerpc

# mpirun/prte can hang after the app already finished (Debian \#1145693,
# https://github.com/openpmix/prrte/issues/1839). Grade success from the
# app's output instead of mpirun's exit status; timeout still bounds and
# kills the hang so the shell doesn't block forever.
run_mpi_test = \
	expected=$$(grep -c '^run[[:space:]]' $(2)); \
	timeout -k 10 120 $(1) < $(2) > test.log 2>&1; \
	status=$$?; cat test.log; \
	got=$$(grep -c '^Loop time of' test.log); rm -f test.log; \
	[ "$$got" -ge "$$expected" ] || { echo "liggghts: MPI test failed (exit $$status, see debian/rules)" >&2; exit 1; }

override_dh_auto_test:
ifeq (,$(filter $(DEB_HOST_ARCH),$(disable_auto_test_archs_mpi)))
	mkdir test
	cp -r examples/LIGGGHTS/Tutorials_public/heatTransfer_1/* test/
	cd test; $(call run_mpi_test,mpirun $(BUILDDIR)/src/liggghts,in.heatGran)
	rm -rf test
endif
ifeq (,$(filter $(DEB_HOST_ARCH),$(disable_auto_test_archs_simpl)))
	mkdir test
	cp -r  examples/LIGGGHTS/Tutorials_public/heatTransfer_1/* test/
	cd test; $(call run_mpi_test,$(BUILDDIR)/src/liggghts,in.heatGran)
	rm -rf test
endif

override_dh_installexamples:
	dh_installexamples
	find $(CURDIR)/debian/ -name ".gitignore" | xargs rm -f
