# panel-native VEIN runtime image.
#
# UE4-class dedicated server. Per the AMP template the Linux server needs
# libasound2, libatomic1 and libpulse0 in addition to the usual 32-bit
# SteamCMD runtime libs. On debian:12 the ALSA package is `libasound2`
# (the `libasound2t64` rename only happened on Ubuntu 24.04 / Debian trixie).
FROM debian:12-slim

ENV DEBIAN_FRONTEND=noninteractive \
    TZ=Etc/UTC \
    LANG=en_US.UTF-8 \
    LC_ALL=en_US.UTF-8

RUN dpkg --add-architecture i386 \
 && apt-get update \
 && apt-get install -y --no-install-recommends \
    ca-certificates \
    locales \
    tini \
    curl \
    lib32gcc-s1 \
    lib32stdc++6 \
    libc6:i386 \
    libatomic1 \
    libasound2 \
    libpulse0 \
 && 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/*

# Bake the Steam client SDK into the image. VEIN's depot ships libsteam_api.so
# but NOT steamclient.so — the runtime SDK module SteamAPI_Init dlopen's from
# ~/.steam/sdk64/. The panel's SteamCMD sidecar only installs the GAME into the
# volume and is then removed, so steamclient.so must come from the image. We
# fetch steamcmd and run it once (+quit) to self-bootstrap, which downloads
# linux32/ + linux64/steamclient.so; we stash both at /opt/steamsdk for the
# entrypoint to symlink. (Without this the server runs but never registers with
# Steam — "Steam Dedicated Server API failed to initialize" + no heartbeat.)
RUN mkdir -p /opt/steamcmd /opt/steamsdk/linux64 /opt/steamsdk/linux32 \
 && curl -fsSL "https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz" | tar -xz -C /opt/steamcmd \
 && ( /opt/steamcmd/steamcmd.sh +quit || true ) \
 && for f in $(find /opt/steamcmd /root/.steam /root/.local/share/Steam -name steamclient.so 2>/dev/null); do \
        echo "$f" | grep -qi linux32 && cp -f "$f" /opt/steamsdk/linux32/steamclient.so || cp -f "$f" /opt/steamsdk/linux64/steamclient.so ; \
    done \
 && ls -la /opt/steamsdk/linux64/ /opt/steamsdk/linux32/ \
 && test -f /opt/steamsdk/linux64/steamclient.so

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 /opt/steamsdk

COPY entrypoint.sh /entrypoint.sh
# Strip any CR (\r) so a Windows-checkout CRLF entrypoint can't make the kernel
# look for "/bin/bash\r" → "exec /entrypoint.sh: No such file or directory"
# (exit 127 crash-loop). Belt-and-suspenders: the source is LF, but a future
# re-checkout on a CRLF-normalizing host shouldn't be able to break the image.
RUN sed -i 's/\r$//' /entrypoint.sh && chmod +x /entrypoint.sh

EXPOSE 7777/udp 27015/udp 7878/udp

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