#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=+lto abi=+time64
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic

%:
ifeq ($(filter nopython,$(DEB_BUILD_PROFILES)),)
	dh $@ --with python3 --exclude=.la
else
	dh $@ --exclude=.la
endif

override_dh_auto_configure:
	dh_auto_configure -- \
		--enable-tools=yes \
		--enable-bindings-cxx \
		$(NULL)
	if ! echo "$(DEB_BUILD_PROFILES)" | grep -q "nopython"; then \
		pybuild --configure -d bindings/python/; \
	fi

override_dh_auto_build:
	dh_auto_build
	if ! echo "$(DEB_BUILD_PROFILES)" | grep -q "nopython"; then \
		CFLAGS="$(shell dpkg-buildflags --get CFLAGS) -I $(CURDIR)/include -I $(CURDIR)/tests/gpiosim/" \
		LDFLAGS="$(shell dpkg-buildflags --get LDFLAGS) -L $(CURDIR)/lib/.libs/ -L $(CURDIR)/tests/gpiosim/.libs/" \
		pybuild --build -s pyproject -d bindings/python/; \
	fi
	if ! echo "$(DEB_BUILD_OPTIONS)" | grep -q "nodoc"; then \
		if command -v doxygen >/dev/null; then \
			cd docs && doxygen Doxyfile; \
		else \
			echo "Warning: doxygen not found. Skipping documentation build."; \
		fi; \
	fi

# Architecture: all only: build the doxygen documentation
override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_auto_build -- doc
endif

# doxygen documentation is installed using dh_installdocs
override_dh_auto_install-indep:

override_dh_auto_install:
	dh_auto_install
	if ! echo "$(DEB_BUILD_PROFILES)" | grep -q "nopython"; then \
		pybuild --install -s pyproject -d bindings/python/; \
	fi

override_dh_installdocs:
	# fix lintian useless-autogenerated-doxygen-file
	dh_installdocs -X.md5

override_dh_clean:
	dh_clean
	rm -rf confdefs.h
	rm -rf doc/*
