FROM docker.io/library/eclipse-temurin:21-jdk-alpine

ARG SCALA_VERSION
ENV SCALA_VERSION ${SCALA_VERSION:-3.4.0}
ARG SBT_VERSION
ENV SBT_VERSION ${SBT_VERSION:-1.9.9}

# git is required for checkout
# python3 is required for pre-commit
# node is required for Forgejo checkout
RUN apk add curl bash git docker python3 py3-pip nodejs npm
RUN pip3 install pre-commit --break-system-packages

# Create the user - builds do not run as root.
RUN addgroup -S -g 1000 builder
RUN adduser -S -h /home/builder -s /bin/bash -G builder -u 1000 builder

# Switch to the build user. Everything else is installed at this level.
USER builder
WORKDIR /home/builder

# Coursier provides all Scala development requirements.
RUN curl -fLo coursier https://github.com/coursier/launchers/raw/master/coursier
RUN chmod +x coursier
RUN ./coursier setup --yes
RUN rm ./coursier
RUN mkdir -p /home/builder/.local/bin
ENV PATH="${PATH}:/home/builder/.local/share/coursier/bin"
ENV PATH="${PATH}:/home/builder/.local/bin"

# Make Git not complain.
RUN git config --global init.defaultBranch main

# Warm up the SBT cache (taken from official SBT Docker image).
RUN \
  sbt sbtVersion && \
  mkdir -p project && \
  echo "scalaVersion := \"${SCALA_VERSION}\"" > build.sbt && \
  echo "sbt.version=${SBT_VERSION}" > project/build.properties && \
  echo "// force sbt compiler-bridge download" > project/Dependencies.scala && \
  echo "case object Temp" > Temp.scala && \
  sbt compile && \
  rm -r project && rm build.sbt && rm Temp.scala && rm -r target