# panel-native Enshrouded runtime image.
#
# Enshrouded's dedicated server is WINDOWS-ONLY (SteamCMD app 2278520 has no
# Linux depot — "Invalid platform"; AMP runs the exe under Proton GE). We run
# it under Wine + Xvfb, same pattern as the verified windrose module.
#
# Expected runtime layout inside the container:
#   /game                          — SteamCMD-installed Windows build (panel volume)
#   /game/enshrouded_server.exe    — dedicated server binary
#   /game-saves                    — enshrouded_server.json + savegame/ + logs/
#   /entrypoint.sh                 — boot Xvfb, launch exe under Wine

FROM debian:12-slim

ENV DEBIAN_FRONTEND=noninteractive \
    TZ=Etc/UTC \
    LANG=en_US.UTF-8 \
    LC_ALL=en_US.UTF-8 \
    WINEDEBUG=-all \
    WINEPREFIX=/home/panel/.wine \
    DISPLAY=:99

# winehq-stable (Wine 9+) rather than Debian's wine 8.0~repack — matches the
# windrose module's rationale (IOCP fixes, and what CubeCoders' AMP wine
# images ship). cabextract + winbind are commonly needed by Wine helpers.
RUN dpkg --add-architecture i386 \
 && apt-get update \
 && apt-get install -y --no-install-recommends \
    ca-certificates \
    locales \
    tini \
    curl \
    gnupg \
    xvfb \
    xauth \
    winbind \
    cabextract \
    procps \
 && mkdir -pm755 /etc/apt/keyrings \
 && curl -fsSL https://dl.winehq.org/wine-builds/winehq.key \
    | gpg --dearmor -o /etc/apt/keyrings/winehq-archive.key \
 && curl -fsSL https://dl.winehq.org/wine-builds/debian/dists/bookworm/winehq-bookworm.sources \
    -o /etc/apt/sources.list.d/winehq-bookworm.sources \
 && apt-get update \
 && apt-get install -y --no-install-recommends winehq-stable \
 && sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
 && locale-gen \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

RUN groupadd -g 1000 panel \
 && useradd -u 1000 -g 1000 -m -s /bin/bash panel \
 && mkdir -p /game /game-saves \
 && chown -R panel:panel /game /game-saves /home/panel

# Pre-warm the Wine prefix during the image build (skip Mono/Gecko — the
# server doesn't need .NET/HTML). See windrose Dockerfile for the full
# rationale (interrupted first-boot wineboot leaves a broken prefix that
# crash-loops with kernel32 c0000135).
USER panel
ENV HOME=/home/panel WINEPREFIX=/home/panel/.wine WINEDEBUG=-all
RUN Xvfb :99 -screen 0 1024x768x24 -nolisten tcp & XVFB_PID=$! \
 && sleep 2 \
 && DISPLAY=:99 WINEDLLOVERRIDES="mscoree=;mshtml=" wineboot --init \
 && wineserver -w \
 && kill "$XVFB_PID" 2>/dev/null || true
USER root

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 15637/udp

ENTRYPOINT ["/usr/bin/tini", "--", "/entrypoint.sh"]
