# 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=unstable ...   # non-public Steam branch

ARG PZ_BRANCH=public

# --- 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
COPY docker/install-server.sh /usr/local/bin/install-server
RUN install-server /server "${PZ_BRANCH}"

# --- 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 agent agent
COPY patcher patcher
COPY --from=server /server/java/projectzomboid.jar /pz/projectzomboid.jar
RUN ./gradlew --no-daemon -Ppz.jar=/pz/projectzomboid.jar build :patcher:patchServerJar

# --- Stage 3: runtime ------------------------------------------------------------
FROM docker.io/library/debian:trixie-slim

# ca-certificates: required by steamclient.so to connect to Steam. Unpinned so
# the image picks up the current Debian CA bundle.
# hadolint ignore=DL3008
RUN apt-get update \
    && apt-get install --yes --no-install-recommends ca-certificates \
    && rm --recursive --force /var/lib/apt/lists/*

RUN useradd --create-home --uid 1000 pz \
    && mkdir /data \
    && chown pz:pz /data

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

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