#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/dpkg/pkg-info.mk

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

export LC_ALL=C.UTF-8

# For automating the d/*.links stuff.
export ISOSPEC_LIB_VERSION=$(DEB_VERSION_UPSTREAM)
export ISOSPEC_LIB_SOVERSION=2

export PYBUILD_NAME=isospec

export PYBUILD_SYSTEM=pyproject


# Taken from CDBS file for R.
rversion	:= $(shell dpkg-query -W -f='$${Version}' r-base-dev)
rapiversion	:= $(shell dpkg-query -W -f='$${Provides}' r-base-core | grep -o 'r-api[^, ]*')

%:
	dh $@ --with python3 --buildsystem=pybuild

override_dh_clean:
	dh_quilt_unpatch
	rm -rf obj-$(DEB_HOST_MULTIARCH)
	rm -rf build
	rm -f compile_commands.json
	rm -rf .pc
	rm -rf IsoSpecPy.egg-info
	rm -rf .pybuild/
	rm -rf tmp-install/
	rm -rf IsoSpec++/IsoSpec++
	rm -rf IsoSpecPy/build/

	# Now the default target
	dh_clean

configure_cpp_library:
	dh_auto_configure -O--buildsystem=cmake  --sourcedirectory=./src/IsoSpec++ -- \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DISOSPEC_LIB_VERSION=$(ISOSPEC_LIB_VERSION) -DISOSPEC_LIB_SOVERSION=$(ISOSPEC_LIB_SOVERSION)

configure_python_module:
	dh_auto_configure -O--buildsystem=pybuild -O--system=distutils --sourcedirectory=./src/IsoSpecPy

configure_cpp_library_doc:
	dh_auto_configure -O--buildsystem=cmake -- \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DISOSPEC_LIB_VERSION=$(ISOSPEC_LIB_VERSION) -DISOSPEC_LIB_SOVERSION=$(ISOSPEC_LIB_SOVERSION)

override_dh_auto_configure:
	dh_quilt_patch
	dh_auto_configure -O--buildsystem=cmake -- \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DISOSPEC_LIB_VERSION=$(ISOSPEC_LIB_VERSION) -DISOSPEC_LIB_SOVERSION=$(ISOSPEC_LIB_SOVERSION)

override_dh_auto_build:
	# The C++ lib
	dh_auto_build -O--buildsystem=cmake
	# The Python module
	dh_auto_build --buildsystem=pybuild
	# The Doxygen-based HTML format documentation
	dh_auto_build -O--buildsystem=cmake -- html_doc
	# The Doxygen-based PDF format documentation
	# As of 20251016, pdflatex refman.tex fails.
	# dh_auto_build -O--buildsystem=cmake -- pdf_doc

	# sed-based replacement of the cloudflare URL with the MathJax.js file from the
	# libjs-mathjax package
	sh debian/replace-mathjax-cloudflare-url-local-file.sh

override_dh_auto_install:
	# The C++ lib
	dh_auto_install -O--buildsystem=cmake
	# The Python module
	dh_auto_install -O--buildsystem=pybuild
	# Now remove the links, that we'll provide using d/*.links
	# So that we can use libIsoSpec++.so.* in d/*.install
	rm -f debian/tmp/usr/lib/*/libIsoSpec++.so
	rm -f debian/tmp/usr/lib/*/libIsoSpec++.so.$(ISOSPEC_LIB_SOVERSION)

