#!/bin/sh
# Build the local-only scala3-stage0 .deb used by the one-time bootstrap
# build (build profile pkg.scala.stage0). It holds upstream's pinned
# reference compiler jars, checked against the checksums below; see
# debian/README.source. This is a maintainer tool, run by hand. It is not
# used by debian/rules and the .deb it produces is never uploaded.
#
# Usage: debian/make-stage0-deb OUTDIR
set -e

SEED=3.8.4
BASE=https://repo1.maven.org/maven2/org/scala-lang

OUT=${1:?usage: $0 OUTDIR}
mkdir -p "$OUT"
OUT=$(cd "$OUT" && pwd)
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT

ROOT=$WORK/root
mkdir -p "$ROOT/DEBIAN" "$ROOT/usr/share/scala3-stage0"

for a in scala3-compiler_3 scala3-interfaces tasty-core_3 scala-library; do
	curl -fsSL -o "$WORK/$a.jar" "$BASE/$a/$SEED/$a-$SEED.jar"
done

( cd "$WORK" && sha256sum -c - ) <<EOF
740be02b51e815ef2a927a422d61fd851e25403655fb9ee2e076b4f9ce1fb34e  scala3-compiler_3.jar
eac968f8ff5f7aed55fb9ffb535bb6effaef4c9d3d8593a45b6dd98b7ce0b9d9  scala3-interfaces.jar
eaea300a6fa4ccd24c979f76e1356b10b27151b395ad3249f9ad8afb4ba8ae20  tasty-core_3.jar
1b8330de5774c15874173f168b674255caa00c5ef3648ab0b6cb763bb7fd2de7  scala-library.jar
EOF

install -m644 "$WORK"/*.jar "$ROOT/usr/share/scala3-stage0/"

cat > "$ROOT/DEBIAN/control" <<EOF
Package: scala3-stage0
Version: $SEED
Architecture: all
Maintainer: Debian Java Maintainers <pkg-java-maintainers@lists.alioth.debian.org>
Description: Scala $SEED reference compiler jars for bootstrapping src:scala3 (local only)
 Upstream's pinned reference compiler, used once by the pkg.scala.stage0
 build profile. Not for the archive.
EOF

dpkg-deb --root-owner-group --build "$ROOT" "$OUT/scala3-stage0_${SEED}_all.deb"
