# ZomboidForge dedicated server image (linux/amd64).
#
# steamcmd downloads the vanilla server during the build; the repository
# contains no game files.
#
#   podman build --platform linux/amd64 -t zomboidforge .
#   podman build --build-arg PZ_BRANCH=42.19 --build-arg PZ_VERSION=42.19 ...
#
# The stage game-version (--target game-version) downloads only the server jar of
# PZ_BRANCH and writes its game version to /game-version; ci/release.sh uses it to
# name the image before the full build.
#
# Build arguments:
#   PZ_BRANCH      Steam branch of app 380870: public, unstable, or a version branch (42.19, ...)
#   PZ_VERSION     Expected Project Zomboid version; the build fails when the downloaded
#                  server reports another one. Default: pz.version in gradle.properties
#   STEAMCLIENT    bundled (default) or steamcmd: replace the server's steamclient.so
#                  files with steamcmd's newer copies
#   FORGE_VERSION  ZomboidForge version (jar manifest, image label); set by ci/release.sh.
#                  Default: 0.0.0-prerelease
#   STEAM_BUILDID  Image label; in the stage game-version it keeps the cached result until
#                  Steam publishes a new build. Set by ci/release.sh
#   DISABLED_PATCHES  Comma-separated simple class names of build-time patches to skip
#                  (debug builds only; ci/release.sh never sets it)

ARG PZ_BRANCH=public
ARG PZ_VERSION=""
ARG STEAMCLIENT=bundled
ARG FORGE_VERSION="0.0.0-prerelease"
ARG STEAM_BUILDID=""

# Base images are pinned by digest; the tag in front of the digest is for readers only.
# The same steamcmd image is in ci/steam-buildid.sh and the Gradle plugin's DownloadGame.

# --- Stage 1: vanilla dedicated server -----------------------------------------
FROM docker.io/steamcmd/steamcmd:debian-13@sha256:b0b49ef3101c304ed6e3fbc3ea930b2940d7c489563a1ebac524ad5bb46d336d AS server

# steamcmd updates itself on its first run. This runs in its own layer so the
# update is cached and separate from the app installation.
RUN steamcmd +quit

ARG PZ_BRANCH
ARG STEAMCLIENT
COPY docker/install-server.sh /usr/local/bin/install-server
RUN install-server /server "${PZ_BRANCH}" "${STEAMCLIENT}"

# --- Stage 2: ZomboidForge sources and Gradle -----------------------------------
FROM docker.io/library/eclipse-temurin:25-jdk@sha256:8c0a84ea11c8f6ed52600fc19f1040121f2a162998e9f50a5faebbbad9172dcc AS sources
WORKDIR /src

# Gradle distribution download, cached independently of the sources.
COPY gradlew gradle.properties settings.gradle.kts ./
COPY gradle gradle
RUN ./gradlew --no-daemon --version

COPY build-logic build-logic
COPY api api
COPY core core
COPY compat compat
COPY agent agent
COPY patcher patcher
COPY mod-tools mod-tools
COPY gradle-plugin gradle-plugin

# --- Game version of a Steam branch, from its server jar alone ------------------
# Not part of the image; built on its own by ci/release.sh.
FROM sources AS game-version

# DepotDownloader downloads single files of a depot; steamcmd only whole apps.
ADD --checksum=sha256:a999dec66b4850fc961bd50366696d23c2d0fad7b18790e6a5647b2f19097a53 \
    https://github.com/SteamRE/DepotDownloader/releases/download/DepotDownloader_3.4.0/DepotDownloader-linux-x64.zip \
    /opt/depotdownloader/depotdownloader.zip
RUN cd /opt/depotdownloader \
    && jar xf depotdownloader.zip \
    && chmod +x DepotDownloader \
    && rm depotdownloader.zip

# FORGE_VERSION only keeps Gradle from asking git (not in this stage) for the version.
ARG PZ_BRANCH
ARG STEAM_BUILDID
ARG FORGE_VERSION
RUN echo java/projectzomboid.jar > /tmp/files \
    && /opt/depotdownloader/DepotDownloader \
        -app 380870 -depot 380871 -branch "${PZ_BRANCH}" -filelist /tmp/files -dir /pz \
    && ./gradlew --no-daemon --quiet \
        -Ppz.jar=/pz/java/projectzomboid.jar \
        -Pversion=${FORGE_VERSION} \
        :patcher:printGameVersion > /game-version

# --- Stage 3: ZomboidForge build and server jar patching -----------------------
FROM sources AS build

COPY --from=server /server/java/projectzomboid.jar /pz/projectzomboid.jar

# An empty PZ_VERSION keeps pz.version from gradle.properties. FORGE_VERSION always has a
# value (default 0.0.0-prerelease): this stage has no git, so the version cannot come from a tag.
ARG PZ_VERSION
ARG FORGE_VERSION
ARG DISABLED_PATCHES=""
RUN ./gradlew --no-daemon \
        -Ppz.jar=/pz/projectzomboid.jar \
        ${PZ_VERSION:+-Ppz.version=${PZ_VERSION}} \
        ${FORGE_VERSION:+-Pversion=${FORGE_VERSION}} \
        ${DISABLED_PATCHES:+-Pzf.disabledPatches=${DISABLED_PATCHES}} \
        build -x :gradle-plugin:test :patcher:patchServerJar

# --- Stage 4: runtime ------------------------------------------------------------
FROM docker.io/library/debian:trixie-slim@sha256:a99cfc517144bc59b1978475ec53b46ecabec7e43635402ee5b77cc54cd1b20a

# ca-certificates: required by steamclient.so to connect to Steam.
# sqlite3: the entrypoint checks the admin account in the server database.
# Unpinned so the image picks up current Debian security updates.
# hadolint ignore=DL3008
RUN apt-get update \
    && apt-get install --yes --no-install-recommends ca-certificates sqlite3 \
    && rm --recursive --force /var/lib/apt/lists/*

# /forge/mods holds Java mods (one folder per mod); usually a mounted directory.
RUN useradd --create-home --uid 1000 pz \
    && mkdir --parents /data /forge/mods \
    && chown --recursive pz:pz /data /forge

COPY --from=server /server /opt/pz
COPY --from=build /src/patcher/build/server/projectzomboid.jar /opt/pz/java/projectzomboid.jar
COPY --from=build /src/agent/build/libs/zomboidforge-agent-*.jar /opt/pz/zomboidforge-agent.jar
COPY docker/entrypoint.sh /usr/local/bin/entrypoint

# Steam downloads Workshop items into <install dir>/steamapps/workshop. /opt/pz is
# owned by root, so steamapps points into the data volume, which also keeps the
# downloads across container recreation.
RUN ln --symbolic /data/steamapps /opt/pz/steamapps

ARG PZ_BRANCH
ARG PZ_VERSION
ARG FORGE_VERSION
ARG STEAM_BUILDID

LABEL org.opencontainers.image.title="ZomboidForge" \
      org.opencontainers.image.description="Project Zomboid dedicated server with Java mod support." \
      org.opencontainers.image.version="${FORGE_VERSION}" \
      dev.overlord.zomboidforge.pz-version="${PZ_VERSION}" \
      dev.overlord.zomboidforge.steam-branch="${PZ_BRANCH}" \
      dev.overlord.zomboidforge.steam-buildid="${STEAM_BUILDID}"

USER 1000:1000
VOLUME /data
# 16261/udp game, 16262/udp direct connect, 27015/tcp RCON
EXPOSE 16261/udp 16262/udp 27015/tcp
ENTRYPOINT ["/usr/local/bin/entrypoint"]
