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

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

# git and node are required for checkout
# python3 is required for pre-commit
# docker is required for certain DinD cases
RUN apt-get update
RUN apt-get install -y curl git docker python3 python3-pip nodejs ca-certificates
RUN pip3 install pre-commit

# Ensure that this image recognizes the self-signed registry certificate.
COPY registry.garrity.co.crt /usr/local/share/ca-certificates
RUN update-ca-certificates

# Create the user - builds do not run as root.
RUN groupadd -g 1111 -r builder
RUN useradd -u 1111 -m -g 1111 -r -s /bin/bash 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