# 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 ...
#
# 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 only; 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=""

# --- Stage 1: vanilla dedicated server -----------------------------------------
FROM docker.io/steamcmd/steamcmd:debian-13 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 build and server jar patching -----------------------
FROM docker.io/library/eclipse-temurin:25-jdk AS build
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
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 3: runtime ------------------------------------------------------------
FROM docker.io/library/debian:trixie-slim

# 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"]
