#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DOCS_DIR = $(shell dh_assistant which-build-system | sed -e 's/.*"build-directory":"//' -e 's/".*//')/docs-gh
PY_VERS := $(filter-out $(shell py3versions -d),$(shell py3versions -r))

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -Smeson+ninja -- --buildtype=debugoptimized -Ddefault_library=shared -Dtesting=enabled -Dpython=true -Ddocs=true
	set -e; for py in $(PY_VERS); do \
	  D=debian/bin-$$py; \
	  mkdir -p $$D; \
	  ln -sf $$(which $$py) $$D/python3; \
	  echo '#!/bin/sh\nexec `dirname $$0`/python3 /usr/bin/meson "$$@"\n' >$$D/meson; \
	  chmod +x $$D/meson; \
	  PATH=$(CURDIR)/$$D:$$PATH \
	    dh_auto_configure -Smeson+ninja -Bbuild-$$py -- --buildtype=debugoptimized -Ddefault_library=shared -Dtesting=enabled -Dpython=true -Ddocs=true; \
	done

execute_after_dh_auto_build:
	set -e; for py in $(PY_VERS); do \
	  PATH=$(CURDIR)/debian/bin-$$py:$$PATH dh_auto_build -Bbuild-$$py; \
	done

execute_after_dh_auto_install:
	set -e; for py in $(PY_VERS); do \
	  PATH=$(CURDIR)/debian/bin-$$py:$$PATH dh_auto_install -Bbuild-$$py -- --tags python-bindings; \
	done
	cd $(CURDIR)/debian/tmp && LD_LIBRARY_PATH=usr/lib/$(DEB_HOST_MULTIARCH) help2man -n "SMT solver for bit vectors and arrays" usr/bin/bitwuzla >bitwuzla.1
	find $(DOCS_DIR) -name '*kind.html' | xargs -r sed -i -e '/cdn.jsdelivr.net.*mathjax.*js/ { s_<script_<!--script_; s_/script>_/script-->_ }'
	$(RM) -rf $(DOCS_DIR)/cli_usage.txt $(DOCS_DIR)/static/fonts $(DOCS_DIR)/static/jquery.js

execute_after_dh_auto_clean:
	set -e; for py in $(PY_VERS); do dh_auto_clean -Bbuild-$$py; done
	$(RM) -rf debian/bin-*
