#! /usr/bin/make -f

export DH_OPTIONS
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

INSTDIR = debian/tmp

%:
	dh $@ -Bbuild -Scmake --with python3

ifeq "" "$(filter %-doc,$(shell dh_listpackages))"
  WEBSITE = OFF
else
  WEBSITE = ON
endif

# Helper variables
PY3VERS = $(shell py3versions -vr)
PY3DEF = $(shell py3versions -vd)
# All supported python3 versions except the default one
PY3EXTRA = $(filter-out $(PY3DEF),$(PY3VERS))

override_dh_auto_configure:
	# Default interpreter -> main build dir "build" (also builds the C++
	# library and, for -indep, the website).
	dh_auto_configure -- \
		-DBUILD_WEBSITE=$(WEBSITE) \
		-DCMAKE_BUILD_RPATH_USE_ORIGIN=ON \
		-DPYTHON=ON \
		-DPython3_EXECUTABLE=/usr/bin/python$(PY3DEF)
	# One additional CMake build dir per extra python3 version.
	set -e; for v in $(PY3EXTRA); do \
		dh_auto_configure --builddirectory=build-py$$v -- \
			-DBUILD_WEBSITE=OFF \
			-DCMAKE_BUILD_RPATH_USE_ORIGIN=ON \
			-DPYTHON=ON \
			-DPython3_EXECUTABLE=/usr/bin/python$$v ; \
	done

override_dh_auto_build-arch:
	dh_auto_build
	set -e; for v in $(PY3EXTRA); do \
		dh_auto_build --builddirectory=build-py$$v ; \
	done

override_dh_auto_build-indep:
	dh_auto_build -- website

override_dh_auto_install-indep:
	dh_auto_install -- -C website

override_dh_auto_install-arch:
	dh_auto_install
	set -e; for v in $(PY3EXTRA); do \
		dh_auto_install --builddirectory=build-py$$v ; \
	done

override_dh_auto_test:

override_dh_install-arch:
	dh_install
	dh_numpy3 --package=python3-imath

override_dh_install-indep:
	rm -f $(INSTDIR)/usr/share/doc/Imath/sphinx/_static/doctools.js
	rm -f $(INSTDIR)/usr/share/doc/Imath/sphinx/_static/jquery.js
	rm -f $(INSTDIR)/usr/share/doc/Imath/sphinx/_static/language_data.js
	rm -f $(INSTDIR)/usr/share/doc/Imath/sphinx/_static/searchtools.js
	rm -f $(INSTDIR)/usr/share/doc/Imath/sphinx/_static/underscore.js
	rm -f $(INSTDIR)/usr/share/doc/Imath/sphinx/.buildinfo
	rm -rf $(INSTDIR)/usr/share/doc/Imath/sphinx/.doctrees
	dh_install
