#!/usr/bin/make -f
# Copyright (C) 2026 David Nichols
# SPDX-License-Identifier: MIT

include /usr/share/dpkg/architecture.mk

builddir = obj-$(DEB_HOST_GNU_TYPE)
python_version := $(shell /usr/bin/python3 -c 'import sys; print("%s.%s" % sys.version_info[:2])')
python_suffix := $(shell /usr/bin/python3 -c 'import sysconfig; print(sysconfig.get_config_var("EXT_SUFFIX"))')
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export TZ = UTC
export LC_ALL = C.UTF-8
unexport CMAKE_PREFIX_PATH
unexport LD_LIBRARY_PATH
unexport LD_PRELOAD
unexport QORE_MODULE_DIR
unexport QORE_INCLUDE_DIR
unexport QORE_INCLUDE_DIRS
unexport QORE_LIBDIR
unexport DOXYGEN_EXECUTABLE
unexport PYTHONPATH
unexport PYTHONHOME
unexport Python3_ROOT
unexport Python3_LIBRARIES
unexport Python3_INCLUDE_DIRS
unexport Python3_EXECUTABLE
export PYTHONNOUSERSITE = 1
export PYTHONDONTWRITEBYTECODE = 1
# All Qore tools must use this build and the packaged standard library only.
export QORE_MODULE_DIR_ONLY = 1
stdlib_paths := $(shell env -u QORE_MODULE_DIR -u QORE_MODULE_DIR_ONLY /usr/bin/qore --module-path)
export QORE_MODULE_DIR = $(CURDIR)/$(builddir):$(stdlib_paths)

build_docs = $(if $(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)),,$(filter qore-python-module-doc,$(shell dh_listpackages)))

%:
	dh $@ --buildsystem=cmake --builddirectory=$(builddir) --without=autoreconf

override_dh_auto_configure:
	# qpp and Qore run during a native build; cross builds need a separate host SDK.
	test '$(DEB_BUILD_GNU_TYPE)' = '$(DEB_HOST_GNU_TYPE)'
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_SKIP_RPATH=ON \
		-DCMAKE_SKIP_INSTALL_RPATH=ON \
		-DCMAKE_IGNORE_PREFIX_PATH=/usr/local \
		-DQore_DIR:PATH=/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Qore \
		-DPython3_EXECUTABLE:FILEPATH=/usr/bin/python3 \
		-DPython3_ROOT_DIR:PATH=/usr \
		-DQORE_EXECUTABLE:FILEPATH=/usr/bin/qore \
		-DQORE_QPP_EXECUTABLE:FILEPATH=/usr/bin/qpp \
		-DCMAKE_DISABLE_FIND_PACKAGE_Doxygen=$(if $(build_docs),OFF,ON)

override_dh_auto_build:
	dh_auto_build
ifneq (,$(build_docs))
	dh_auto_build -- docs
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	/usr/bin/qore -l xml -e 'exit(0);'
	timeout 600 /usr/bin/qore -l $(CURDIR)/$(builddir)/python-api-$$(/usr/bin/qore --latest-module-api).qmod test/python.qtest -v
	ln -sf python-api-$$(/usr/bin/qore --latest-module-api).qmod $(builddir)/qoreloader$(python_suffix)
	PYTHONPATH=$(CURDIR)/$(builddir) timeout 60 /usr/bin/python3 debian/tests/standalone.py
endif

override_dh_auto_install:
	dh_auto_install
	# Stage the alias before dh_python3 scans extensions and calculates dependencies.
	mkdir -p debian/tmp/usr/lib/python3/dist-packages
	ln -s ../../$(DEB_HOST_MULTIARCH)/qore-modules/python-api-$$(/usr/bin/qore --latest-module-api).qmod \
		debian/tmp/usr/lib/python3/dist-packages/qoreloader$(python_suffix)

override_dh_python3:
	# This embeds the full CPython ABI, not the stable limited API.
	dh_python3 --skip-private -V $(python_version)-$(python_version)

override_dh_installdocs:
	dh_installdocs -pqore-python-module
ifneq (,$(build_docs))
	dh_installdocs -pqore-python-module-doc --doc-main-package=qore-python-module-doc $(builddir)/docs/python/html
else
ifneq (,$(filter qore-python-module-doc,$(shell dh_listpackages)))
	dh_installdocs -pqore-python-module-doc --doc-main-package=qore-python-module-doc
endif
endif

override_dh_missing:
	dh_missing --fail-missing

override_dh_gencontrol:
	# Require the Qore runtime and SONAME package matching the build SDK.
	# dh_shlibdeps also supplies libc, libstdc++ and Python dependencies.
	if dh_listpackages | grep -qx qore-python-module; then \
		qore_lib_dep=$$(dpkg-query -W -f='$${Depends}' libqore-dev | tr ',' '\n' | \
			sed -n 's/^ *\(libqore[0-9][0-9]*\) (= \([^)]*\))$$/\1 (>= \2)/p'); \
		test -n "$$qore_lib_dep" || exit 1; \
		qore_version=$$(dpkg-query -W -f='$${Version}' libqore-dev); \
		dh_gencontrol -pqore-python-module -- "-Vqore:Depends=$$qore_lib_dep, qore (>= $$qore_version)"; \
	fi
	dh_gencontrol --remaining-packages
