#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Find all Python versions
PYTHON3=$(shell py3versions -vd)
ALLPY=$(shell py3versions -vs)

%:
	# NOTE: although Odil correctly builds in parallel, it requires a huge
	# amount of RAM to do so. Disable parallel building to avoid build
	# failures.
	dh $@ --builddirectory=build --with python3 --no-parallel

# Base build: no wrappers
override_dh_auto_configure-arch:
	dh_auto_configure -- \
	  -DCMAKE_CXX_STANDARD=17 \
	  -DBUILD_EXAMPLES=OFF \
	  -DBUILD_PYTHON_WRAPPERS=OFF -DBUILD_JAVASCRIPT_WRAPPERS=OFF

override_dh_auto_build-nopy:
	dh_auto_build
	mv build build-nopy

# Reconfigure base build for specific Python version and build wrappers
# WARNING: builds for Python versions MUST be sequential since they use the
# same build directory
override_dh_auto_build-py: override_dh_auto_build-nopy
	set -e; \
	for Python in $(ALLPY); do \
	  echo "=== Building for Python $${Python} ==="; \
	  cp -a build-nopy build; \
	  dh_auto_configure -- \
	    -DCMAKE_CXX_STANDARD=17 \
	    -DBUILD_EXAMPLES=OFF \
	    -DBUILD_PYTHON_WRAPPERS=ON -DBUILD_JAVASCRIPT_WRAPPERS=OFF \
	    -DPYTHON_EXECUTABLE=/usr/bin/python$${Python}; \
	  dh_auto_build; \
	  mv build build-py$${Python}; \
	done

override_dh_auto_build-arch: override_dh_auto_build-nopy override_dh_auto_build-py

override_dh_auto_build-indep:
	dh_auto_configure --builddirectory=build-indep -- \
	  -DBUILD_EXAMPLES=OFF \
	  -DCMAKE_CXX_STANDARD=17 \
	  -DBUILD_PYTHON_WRAPPERS=ON -DBUILD_JAVASCRIPT_WRAPPERS=OFF \
	  -DPYTHON_EXECUTABLE=/usr/bin/python$(PYTHON3)
	mkdir -p documentation/_build
	cd documentation && doxygen
	find documentation/_build -name "*.md5" -delete

# WARNING: tests for Python versions MUST be sequential since they use the
# same build directory
override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e; \
	for Python in $(ALLPY); do \
	  echo "=== Testing for Python $${Python} ==="; \
	  ln -s build-py$${Python} build; \
	  ln -sr ./build/wrappers/python ./build/odil; \
	  ln -sr ./wrappers/python/*.py ./build/odil; \
	  cd build && \
	    $(MAKE) install DESTDIR=../install && \
	    WORKSPACE=$(CURDIR) ../.ci/deb/post_build || true;\
	  cd ..; \
	  rm ./build/odil/*py; \
	  rm ./build/odil; \
	  rm build; \
	  rm -rf install; \
	done
endif

override_dh_auto_install-arch-nopy:
	ln -s build-nopy build
	$(MAKE) -C build/src install DESTDIR=$(CURDIR)/debian/tmp
	rm build

# WARNING: installs for Python versions MUST be sequential since they use the
# same build directory
override_dh_auto_install-arch-py: override_dh_auto_install-arch-nopy
	set -e; \
	for Python in $(ALLPY); do \
	  echo "=== Installing for Python $${Python} ==="; \
	  ln -s build-py$${Python} build; \
	  $(MAKE) -C build/wrappers/python install DESTDIR=$(CURDIR)/debian/tmp; \
	  rm build; \
	done

override_dh_auto_install-arch: override_dh_auto_install-arch-nopy override_dh_auto_install-arch-py

override_dh_auto_install-indep:
	ln -s build-indep build
	$(MAKE) -C build/applications install DESTDIR=$(CURDIR)/debian/tmp
	rm build

override_dh_install-arch:
	dh_install
	d-shlibmove \
		--t64 \
		--devunversioned \
		--commit --multiarch --exclude-a --exclude-la \
		--movedev debian/tmp/usr/include/* usr/include \
		debian/tmp/usr/lib/*.so
	find debian -name "lib*.la" -delete

override_dh_clean:
	dh_clean
	find tests -name "*pyc" -delete
	rm -rf build-nopy $(ALLPY:%=build-py%) build-indep documentation/_build
	rm -f build

# These steps are not needed for arch-independent packages
override_dh_auto_configure-indep:
override_dh_auto_test-indep:
