#!/usr/bin/make -f

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

# Build only the projects installed by the .poms file
GRADLE_TASKS := $(shell sed -n 's#^cnf/generated/m2/org/osgi/\([^/]*\)/.*#:\1:jar#p' debian/libosgi-compendium-java.poms)

BND_CACHE := cnf/cache/$(shell dpkg-query -W -f='$${Version}' bnd | sed 's/-[^-]*$$//')/bnd-cache

%:
	dh $@ --buildsystem=gradle --with maven_repo_helper

execute_before_dh_auto_build:
	# Link the dependencies of the projects to the jars installed in Debian
	for bsn in osgi.core org.osgi.framework org.osgi.resource org.osgi.dto org.osgi.service.condition org.osgi.service.log; do \
		mkdir -p $(BND_CACHE)/$$bsn && ln -sf /usr/share/java/osgi.core.jar $(BND_CACHE)/$$bsn/$$bsn-latest.jar; \
	done
	mkdir -p $(BND_CACHE)/osgi.annotation && ln -sf /usr/share/java/osgi.annotation.jar $(BND_CACHE)/osgi.annotation/osgi.annotation-8.1.0.jar
	mkdir -p $(BND_CACHE)/jakarta.servlet-api && ln -sf /usr/share/java/jakarta-servlet-api.jar $(BND_CACHE)/jakarta.servlet-api/jakarta.servlet-api-latest.jar
	mkdir -p $(BND_CACHE)/org.eclipse.persistence.jpa_spec && ln -sf /usr/share/java/eclipselink-jpa-2.1-spec.jar $(BND_CACHE)/org.eclipse.persistence.jpa_spec/org.eclipse.persistence.jpa_spec-latest.jar
	mkdir -p $(BND_CACHE)/org.apache.geronimo.specs.geronimo-jcdi_2.0_spec && ln -sf /usr/share/java/cdi-api.jar $(BND_CACHE)/org.apache.geronimo.specs.geronimo-jcdi_2.0_spec/org.apache.geronimo.specs.geronimo-jcdi_2.0_spec-latest.jar
	mkdir -p $(BND_CACHE)/org.apache.geronimo.specs.geronimo-atinject_1.0_spec && ln -sf /usr/share/java/atinject-jsr330-api.jar $(BND_CACHE)/org.apache.geronimo.specs.geronimo-atinject_1.0_spec/org.apache.geronimo.specs.geronimo-atinject_1.0_spec-latest.jar
	mkdir -p $(BND_CACHE)/com.icl.saxon && ln -sf /usr/share/java/saxon.jar $(BND_CACHE)/com.icl.saxon/com.icl.saxon-latest.jar

override_dh_auto_build:
	# Configure only the projects needed, and skip the unused bnd plugins of the workspace
	dh_auto_build -- $(GRADLE_TASKS) -x :cnf:compileJava --configure-on-demand

override_dh_auto_test:
	# The tests depend on libraries that are not packaged
