FROM ubuntu:22.04

# Отключаем интерактивные вопросы во время установки
ENV DEBIAN_FRONTEND=noninteractive

# Опциональный прокси для apt (например apt-cacher-ng: http://192.168.1.60:3142)
ARG APT_PROXY
RUN if [ -n "$APT_PROXY" ]; then \
      printf 'Acquire::http::Proxy "%s";\nAcquire::https::Proxy "%s";\n' "$APT_PROXY" "$APT_PROXY" > /etc/apt/apt.conf.d/01proxy; \
    fi

# 1. Добавляем 32-битную архитектуру для Steam и базовые графические зависимости
RUN dpkg --add-architecture i386 && \
    apt-get update && apt-get install -y \
    xserver-xorg-core xserver-xorg-video-dummy xserver-xorg-input-libinput \
    fluxbox dbus-x11 xauth xfonts-base x11-xserver-utils xinput xdotool tini \
    software-properties-common wget curl ca-certificates locales pciutils \
    libboost-chrono1.74.0 libboost-filesystem1.74.0 libboost-locale1.74.0 \
    libboost-program-options1.74.0 libboost-thread1.74.0 \
    libcap2-bin libgomp1 mesa-utils \
    libxcb-cursor0 libxcb-xinerama0 libxcb-xfixes0 libxcb-shape0 libxcb-randr0 libxcb-xtest0 \
    libxcb1 libxcb-render0 libxcb-render-util0 libxcb-util1 libxcb-keysyms1 \
    && add-apt-repository multiverse \
    && apt-get update

# 2. Скачиваем проверенный пакет Sunshine
RUN wget "https://github.com/LizardByte/Sunshine/releases/download/v0.20.0/sunshine-ubuntu-22.04-amd64.deb" -O /tmp/sunshine.deb \
    && chmod +x /tmp/sunshine.deb

# 3. Устанавливаем скачанный пакет и Steam со всеми зависимостями
RUN apt-get update && \
    echo "debconf steam/question select I AGREE" | debconf-set-selections \
    && apt-get install -y /tmp/sunshine.deb steam:i386 steam-devices \
    libxtst6 libxtst6:i386 \
    libxss1 libxss1:i386 \
    libnss3 libnss3:i386 \
    libxi6 libxi6:i386 \
    libgtk2.0-0 libgtk2.0-0:i386 \
    libglib2.0-0 libglib2.0-0:i386 \
    libpipewire-0.3-0 libpipewire-0.3-0:i386 \
    libxcb-res0 libxcb-res0:i386 \
    libvpx7 libvpx7:i386 \
    libvdpau1 libvdpau1:i386 \
    libbz2-1.0 libbz2-1.0:i386 \
    libibus-1.0-5 libibus-1.0-5:i386 \
    && ln -sf /lib/x86_64-linux-gnu/libvpx.so.7 /lib/x86_64-linux-gnu/libvpx.so.6 \
    && ln -sf /lib/i386-linux-gnu/libvpx.so.7 /lib/i386-linux-gnu/libvpx.so.6 \
    && locale-gen en_US.UTF-8 \
    && setcap -r /usr/bin/sunshine-v0.20.0 || true \
    && rm -f /tmp/sunshine.deb \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 4. Конфиг Xorg dummy: нужен для корректной работы ввода Moonlight в контейнере
RUN mkdir -p /etc/X11 \
    && echo 'Section "ServerLayout"\n\
Identifier "Layout0"\n\
Screen 0 "Screen0"\n\
EndSection\n\
\n\
Section "Device"\n\
Identifier "Device0"\n\
Driver "dummy"\n\
VideoRam 256000\n\
EndSection\n\
\n\
Section "Monitor"\n\
Identifier "Monitor0"\n\
HorizSync 28.0-80.0\n\
VertRefresh 48.0-75.0\n\
Modeline "1920x1080" 172.80 1920 2040 2248 2576 1080 1081 1084 1118\n\
EndSection\n\
\n\
Section "Screen"\n\
Identifier "Screen0"\n\
Device "Device0"\n\
Monitor "Monitor0"\n\
DefaultDepth 24\n\
SubSection "Display"\n\
Depth 24\n\
Modes "1920x1080"\n\
EndSubSection\n\
EndSection\n\
' > /etc/X11/xorg-dummy.conf

# 5. Entrypoint, fluxbox и tini
COPY scripts/entrypoint.sh /entrypoint.sh
COPY scripts/fluxbox /etc/game-server/fluxbox
RUN chmod +x /entrypoint.sh

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