#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# Inclusion of mpi headers has to be added, we use Debian-packaged libs and not
# embedded ones.
export DEB_CXXFLAGS_MAINT_APPEND=-I /usr/lib/$(DEB_HOST_MULTIARCH)/openmpi/include -I /usr/lib/$(DEB_HOST_MULTIARCH)/mpich/include

%:
	dh $@

override_dh_auto_build:
	$(MAKE) -f make/standalone math-libs

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# I cannot have the tests run the way upstream means it, by launching
	# ./runChecks.py.
	# Thus, build and run the 150 first unit tests,
	# and fail if "FAILED" is detected in the output.
	touch outputsOfTests
	for F in $$(find test/ -name "*.cpp" | head -n150); do \
	    g++ -DTBB_INTERFACE_NEW -D_REENTRANT -I . -I /usr/include/eigen3 -I /usr/lib/$(DEB_HOST_MULTIARCH)/openmpi/include -I /usr/lib/$(DEB_HOST_MULTIARCH)/mpich/include -O2 $$F -lgtest_main -lgtest -ltbb -lsundials_core -lsundials_cvodes -lsundials_kinsol -lsundials_idas -lmpi -o $${F%.cpp}.out; \
	    $${F%.cpp}.out > outputOfThisTest; \
	    cat outputOfThisTest; \
	    cat outputOfThisTest >> outputsOfTests; \
	done
	! grep "FAILED" outputsOfTests
#	./runChecks.py
#	./runTests.py test/unit
#	./runTests.py test/prob
#	./runTests.py test-headers
endif

execute_after_dh_install:
	# Remove non-header files that have been planned for installation.
	find debian/libstan-math-dev/usr/include -name "*.cpp" -delete

execute_before_dh_clean:
	-rm outputOfThisTest
	-rm outputsOfTests
