#!/usr/bin/make -f

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

# The upstream version without the +ds repack suffix
VERSION := $(firstword $(subst +, ,$(DEB_VERSION_UPSTREAM)))

TEST_CP := /usr/share/java/junit4.jar:/usr/share/java/hamcrest.jar:/usr/share/java/easymock.jar:/usr/share/java/objenesis.jar:/usr/share/java/byte-buddy.jar

%:
	dh $@ --with javahelper,maven-repo-helper

# The upstream build uses Gradle 8, which Debian does not have; the library
# has no dependencies, so it is compiled with javac directly.  The class
# files target Java 8, as upstream's.
override_dh_auto_build-indep:
	mkdir -p build/classes build/res
	cp -r src/main/resources/. build/res/
	sed -i 's/^version=.*/version=$(VERSION)/' build/res/com/subgraph/orchid/version.properties
	find src/main/java -name '*.java' | LC_ALL=C sort > build/sources
	javac --release 8 -Xlint:-options -encoding UTF-8 -d build/classes @build/sources
	printf 'Main-Class: com.subgraph.orchid.TorClient\nAutomatic-Module-Name: com.subgraph.orchid\n' > build/manifest
	jar --create --file build/orchid.jar --manifest build/manifest -C build/classes . -C build/res .
	# The POM gets the version of this upload, so that it cannot fall behind.
	sed 's/@VERSION@/$(VERSION)/g' debian/pom.xml.in > build/pom.xml
	grep -q '<version>$(VERSION)</version>' build/pom.xml

override_dh_auto_test-indep:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p build/test
	find test -name '*.java' | LC_ALL=C sort > build/test-sources
	javac -Xlint:-options -encoding UTF-8 -cp build/orchid.jar:$(TEST_CP) -d build/test @build/test-sources
	java -cp build/test:build/orchid.jar:$(TEST_CP) org.junit.runner.JUnitCore \
	    $$(cd test && find . -name '*Test.java' | LC_ALL=C sort | sed 's|^\./||; s|\.java$$||; s|/|.|g')
endif

override_dh_auto_clean:
	rm -rf build
