#!/usr/bin/make -f

B = debian/.dh-scala
J = /usr/share/java
TESTS = fastparse/test/src fastparse/test/src-2.12+
TCP_2.13 = $(J)/scala-reflect-2.13.jar:$(J)/sourcecode-2.13.jar:$(J)/geny-2.13.jar:$(J)/utest-2.13.jar:$(J)/scala-collection-compat_2.13.jar:$(J)/sbt-test-interface.jar
TCP_3 = $(J)/sourcecode-3.jar:$(J)/geny-3.jar:$(J)/utest-3.jar:$(J)/scala-collection-compat_3.jar:$(J)/sbt-test-interface.jar

%:
	dh $@ --buildsystem=scala

# Upstream generates SequencerGen.scala at build time.
execute_before_dh_auto_build:
	perl debian/sequencer-gen debian/generated

# Upstream's utest suites, compiled here and not as scala-build stanzas,
# which would need utest under the nocheck profile too.
# $(1): Scala version, $(2): scalac options.
define run_tests
	mkdir -p $(B)/tests_$(1)
	find $(TESTS) -name '*.scala' | LC_ALL=C sort > $(B)/tests_$(1).files
	echo debian/tests/RunTests.scala >> $(B)/tests_$(1).files
	scalac-$(1) $(2) -nowarn -d $(B)/tests_$(1) \
		-cp $(B)/fastparse_$(1)/classes:$(TCP_$(1)) @$(B)/tests_$(1).files
	java -cp $(J)/scala-library-$(1).jar:$(B)/fastparse_$(1)/classes:$(TCP_$(1)):$(B)/tests_$(1):fastparse/test/resources \
		debian.utest.RunTests utest.runner.Framework $(B)/tests_$(1)
endef

# Scala 3.9 rejects upstream's p(_) for context-bound parsers: -source:3.4.
execute_after_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(call run_tests,2.13,)
	$(call run_tests,3,-source:3.4)
endif
