#!/usr/bin/make -f
include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk

# 2.12 does not build on JDK 26 (https://github.com/scala/bug/issues/13127).
export JAVA_HOME = /usr/lib/jvm/java-25-openjdk-$(DEB_HOST_ARCH)
export PATH := $(JAVA_HOME)/bin:$(PATH)
# Upstream version without the +dsN repack suffix, and its series (2.12).
VERSION = $(firstword $(subst +, ,$(DEB_VERSION_UPSTREAM)))
SERIES = $(word 1,$(subst ., ,$(VERSION))).$(word 2,$(subst ., ,$(VERSION)))
# Version-independent alias shipped by scala-asm, so a scala-asm upload that
# changes the Maven version does not break the build or the wrappers.
ASM = /usr/share/maven-repo/org/scala-lang/modules/scala-asm/debian/scala-asm-debian.jar
JLINE = /usr/share/java/jline2.jar
ANT = /usr/share/java/ant.jar

# Two-pass self-host build (no sbt):
#   pass 1 STARR = the scala already in the archive (Build-Depends), or, for
#                  the one-time bootstrap with the pkg.scala.stage0 profile,
#                  upstream's pinned STARR jars from $(SCALA_STAGE0)
#   pass 2 STARR = the pass-1 output; only pass-2 jars are shipped
# Nothing prebuilt is part of the source package; see debian/README.source.
SCALA_STAGE0 ?= /usr/share/scala-stage0
ifneq ($(filter pkg.scala.stage0,$(DEB_BUILD_PROFILES)),)
STARR0_SRC = $(SCALA_STAGE0)
else
STARR0_SRC = /usr/share/java
endif
SEED = $(CURDIR)/debian/starr0
STARR1 = $(CURDIR)/debian/starr1
OUT1 = $(CURDIR)/debian/buildout1
OUT2 = $(CURDIR)/debian/buildout2
J2 = $(OUT2)/jars
MAN = $(CURDIR)/debian/manout
DRV  = sh debian/build-driver.sh

D = debian/scala
DLIB = debian/scala-library

%:
	dh $@

override_dh_auto_build:
	# Stage the pass-1 STARR (build-time only, never shipped).
	rm -rf $(SEED) && mkdir -p $(SEED)/lib
	cp -L $(STARR0_SRC)/scala-library.jar $(STARR0_SRC)/scala-reflect.jar \
	   $(STARR0_SRC)/scala-compiler.jar $(SEED)/lib/
	# Pass 1: build the jars from that STARR.
	$(DRV) $(CURDIR) $(SEED) $(ASM) $(JLINE) $(ANT) $(OUT1) $(VERSION)
	# Pass 2: self-host -- rebuild from the pass-1 jars. The seed is gone now.
	rm -rf $(STARR1) && mkdir -p $(STARR1)/lib
	cp $(OUT1)/jars/scala-library.jar $(OUT1)/jars/scala-reflect.jar \
	   $(OUT1)/jars/scala-compiler.jar $(STARR1)/lib/
	$(DRV) $(CURDIR) $(STARR1) $(ASM) $(JLINE) $(ANT) $(OUT2) $(VERSION)
	# Manual pages from upstream's sources in src/manual, compiled and run
	# with the pass-2 compiler (EmitManPage needs neither ant nor scala-xml).
	rm -rf $(MAN) && mkdir -p $(MAN)/classes
	java -cp $(J2)/scala-library.jar:$(J2)/scala-reflect.jar:$(J2)/scala-compiler.jar:$(ASM) \
	  scala.tools.nsc.Main -nowarn -d $(MAN)/classes -classpath $(J2)/scala-library.jar \
	  src/manual/scala/tools/docutil/ManPage.scala \
	  src/manual/scala/tools/docutil/EmitManPage.scala \
	  src/manual/scala/man1/*.scala
	set -e; for c in scala scalac fsc scalap; do \
	  java -cp $(MAN)/classes:$(J2)/scala-library.jar \
	    scala.tools.docutil.EmitManPage scala.man1.$$c $(MAN)/$$c.1; \
	done
	# POMs for the Maven repository (installed by maven-repo-helper).
	for p in scala-library scala-reflect scala-compiler; do \
	  sed "s/@VERSION@/$(VERSION)/g" debian/poms/$$p.pom.xml.in > debian/poms/$$p.pom.xml; \
	done

override_dh_auto_test:

override_dh_auto_install:
	# scala-library, scala-reflect and scala-compiler are installed by
	# maven-repo-helper (debian/*.poms): /usr/share/java/<name>-$(VERSION).jar,
	# the unversioned link, and /usr/share/maven-repo. scalap has no POM.
	install -d $(D)/usr/share/java
	install -m644 $(J2)/scalap.jar $(D)/usr/share/java/scalap-$(VERSION).jar
	# Wrappers live under a versioned bin dir; /usr/bin entries are created by
	# update-alternatives in the postinst (so future scala-2.13 can coexist).
	install -d $(D)/usr/share/scala/bin
	install -m755 debian/scalac $(D)/usr/share/scala/bin/scalac
	install -m755 debian/scala-runner $(D)/usr/share/scala/bin/scala
	install -m755 debian/fsc $(D)/usr/share/scala/bin/fsc
	install -m755 debian/scalap-wrapper $(D)/usr/share/scala/bin/scalap
	# Manpages, gzipped (slaved via update-alternatives in the postinst; they
	# live outside /usr/share/man so dh_compress will not touch them).
	install -d $(D)/usr/share/scala/man/man1
	install -m644 $(MAN)/scala.1 $(MAN)/scalac.1 $(MAN)/fsc.1 $(MAN)/scalap.1 \
	  $(D)/usr/share/scala/man/man1/
	gzip -9n $(D)/usr/share/scala/man/man1/*.1

execute_after_dh_link:
	# Series links (<name>-2.12.jar), as the 2.11 package had, so users can
	# pin a Scala series while a later one owns the unversioned names.
	dh_link -pscala-library \
	  usr/share/java/scala-library-$(VERSION).jar usr/share/java/scala-library-$(SERIES).jar \
	  usr/share/java/scala-reflect-$(VERSION).jar usr/share/java/scala-reflect-$(SERIES).jar
	dh_link -pscala \
	  usr/share/java/scala-compiler-$(VERSION).jar usr/share/java/scala-compiler-$(SERIES).jar \
	  usr/share/java/scalap-$(VERSION).jar usr/share/java/scalap-$(SERIES).jar \
	  usr/share/java/scalap-$(VERSION).jar usr/share/java/scalap.jar
	# The commands as <cmd>-2.12, so the 2.12 tools stay reachable on PATH
	# when a later series owns /usr/bin/scala* through update-alternatives.
	set -e; for c in scala scalac fsc scalap; do \
	  dh_link -pscala usr/share/scala/bin/$$c usr/bin/$$c-$(SERIES); \
	  dh_link -pscala usr/share/scala/man/man1/$$c.1.gz usr/share/man/man1/$$c-$(SERIES).1.gz; \
	done

override_dh_auto_clean:
	rm -rf debian/buildout1 debian/buildout2 debian/starr0 debian/starr1 \
	  debian/manout debian/poms/*.pom.xml
