# Local Repertoire image. The build helper pulls or reuses this fixed base image.
FROM amazoncorretto:21-alpine

ARG REPERTOIRE_VERSION=2024.0

# Java AWT needs fontconfig and at least one system font to lay out text ("Fontconfig head is null").
RUN apk add --no-cache fontconfig ttf-dejavu

ENV REPERTOIRE_HOME=/opt/elixir/Repertoire \
    HEALTHCHECK_URL=http://127.0.0.1:1730/alive

WORKDIR ${REPERTOIRE_HOME}/bin

# The build script normalises the selected archive to this directory name.
COPY --chown=1000:1000 Repertoire/ ${REPERTOIRE_HOME}/

RUN addgroup -S -g 1000 repertoire \
    && adduser -S -D -u 1000 -G repertoire repertoire \
    && mkdir -p "${REPERTOIRE_HOME}/log" "${REPERTOIRE_HOME}/git-repos" \
      "${REPERTOIRE_HOME}/data/codex/Agenda" "${REPERTOIRE_HOME}/data/codex/Identities" \
      "${REPERTOIRE_HOME}/data/codex/Privileges" "${REPERTOIRE_HOME}/data/codex/RunInfo" \
      "${REPERTOIRE_HOME}/data/codex/Users" \
    && chown -R 1000:1000 "${REPERTOIRE_HOME}/log" "${REPERTOIRE_HOME}/git-repos" "${REPERTOIRE_HOME}/data/codex" \
    && chmod 0755 "${REPERTOIRE_HOME}/bin/run-server" "${REPERTOIRE_HOME}/bin/repertoire-cli"

LABEL org.opencontainers.image.title="Repertoire" \
      org.opencontainers.image.version="${REPERTOIRE_VERSION}" \
      org.opencontainers.image.description="Locally built Repertoire base image"

HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
  CMD ["sh", "-c", "wget -q -O /dev/null \"$HEALTHCHECK_URL\""]

USER 1000:1000
EXPOSE 1730
ENTRYPOINT ["./run-server"]
