#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

ENABLE_PQ_ALGORITHMS = $(shell dpkg --compare-versions "$$(dpkg-query -f '$${source:Upstream-Version}' -W libnss3-dev)" ge "3.116" && echo ON || echo OFF)

override_dh_auto_clean:
	dh_auto_clean --buildsystem=maven
	dh_auto_clean --builddirectory=build2 --buildsystem=cmake

override_dh_auto_configure:
	dh_auto_configure --buildsystem=maven
	dh_auto_configure --builddirectory=build2 --buildsystem=cmake -- \
		-DWITH_MLDSA=$(ENABLE_PQ_ALGORITHMS) \
		-DWITH_MLKEM=$(ENABLE_PQ_ALGORITHMS) \
		-DWITH_JAVA=OFF \
		-DWITH_JAVADOC=OFF

override_dh_auto_build:
	dh_auto_build --buildsystem=maven

	# create links to Maven-built classes for CMake
	mkdir -p build2/classes/jss
	ln -sf -t build2/classes/jss ../../../base/target/classes/org
	mkdir -p build2/classes/tests
	ln -sf -t build2/classes/tests ../../../base/target/test-classes/org

	# create links to Maven-built JAR files for CMake
	ln -sf -T ../base/target/jss.jar build2/jss.jar
	ln -sf -T ../base/target/jss-tests.jar build2/jss-tests.jar

	# create links to Maven-built headers for CMake
	mkdir -p build2/include/jss
	rmdir build2/include/jss/_jni || true # CMake may create this directory
	ln -sf -t build2/include/jss/ ../../../base/target/include/_jni

	dh_auto_build --builddirectory=build2 --buildsystem=cmake -- native

override_dh_auto_install:
	dh_auto_install --buildsystem=maven --destdir=debian/tmp
	dh_auto_install --builddirectory=build2 --destdir=debian/tmp

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_PROFILES)))
	cd build && ctest --output-on-failure || true
endif

# For maintainer use only, generate a tarball:
gentarball:
	git archive --format=tar upstream --prefix=jss-$(DEB_VERSION_UPSTREAM)/ ":(exclude)debian" | \
		xz --best \
		> ../jss_$(DEB_VERSION_UPSTREAM).orig.tar.xz

%:
	dh $@ --with javahelper
