#!/usr/bin/make -f

# Enable debugging (uncomment to see more details during build)
# export DH_VERBOSE=1

# Architecture variables
DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)

# Standard build rules
%:
	dh $@ --parallel --buildsystem=cmake

# Override configure step to ensure out-of-source build
override_dh_auto_configure:
	mkdir -p obj-$(DEB_HOST_MULTIARCH)
	cd obj-$(DEB_HOST_MULTIARCH) && \
	cmake .. -DCMAKE_INSTALL_PREFIX=/usr \
	      -DCMAKE_BUILD_TYPE=Release \
	      -DCMAKE_VERBOSE_MAKEFILE=ON \
	      -DCMAKE_INSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH)

# Override build step to use the correct build directory
override_dh_auto_build:
	cd obj-$(DEB_HOST_MULTIARCH) && $(MAKE) -j$(shell nproc)

# Override test step to handle tests gracefully
override_dh_auto_test:
	@if [ -f obj-$(DEB_HOST_MULTIARCH)/CTestTestfile.cmake ]; then \
		cd obj-$(DEB_HOST_MULTIARCH) && ctest --output-on-failure; \
	else \
		echo "No tests found, skipping test phase."; \
	fi || true

# Override install step to ensure installation to debian/tmp
override_dh_auto_install:
	cd obj-$(DEB_HOST_MULTIARCH) && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

# Ensure that missing files warnings are treated as errors
override_dh_missing:
	dh_missing --fail-missing
