FROM debian:bookworm-slim

ARG DEBIAN_FRONTEND=noninteractive \
    TINI_VERSION=0.19.0 \
    WINE_PGP_URL=https://dl.winehq.org/wine-builds/winehq.key \
    WINE_SRC_URL=https://dl.winehq.org/wine-builds/debian/dists/bookworm/winehq-bookworm.sources \
    WINE_FLAVOUR=stable \
    WINE_VERSION=8.0.2~bookworm-1 \
    USER_ID=1000 \
    GROUP_ID=1000

# Install tini
ADD https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini /tini

RUN chmod +x /tini \
    && dpkg --add-architecture i386 \
    && apt-get update -y \
    && apt-get install -y --no-install-recommends \
        gnupg \
        ca-certificates \
        curl \
        xauth \
        xvfb \
        iproute2 \
    && mkdir -pm755 /etc/apt/keyrings \
    && curl -fsSL -o /etc/apt/keyrings/winehq-archive.key ${WINE_PGP_URL} \
    && curl -fsSL -o /etc/apt/sources.list.d/winehq.sources ${WINE_SRC_URL} \
    && apt-get update -y \
    && apt-get install -y --no-install-recommends \
        winehq-${WINE_FLAVOUR}=${WINE_VERSION} \
        wine-${WINE_FLAVOUR}=${WINE_VERSION} \
        wine-${WINE_FLAVOUR}-amd64=${WINE_VERSION} \
        wine-${WINE_FLAVOUR}-i386=${WINE_VERSION} \
        unzip \
        git \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Create container user (Pelican compatible UID 1000)
RUN groupadd --gid $GROUP_ID blockland \
    && useradd --uid $USER_ID --gid $GROUP_ID --create-home --shell /bin/bash blockland

USER blockland

# Set Pelican working directory
WORKDIR /home/container

# Copy scripts
COPY --chmod=0755 --chown=blockland:blockland bin/ /usr/local/bin/

# Environment defaults
ENV BL_DEDI_LAN=0 \
    BL_DTOKEN="" \
    BL_SERVER_NAME="" \
    BL_MAX_PLAYERS="" \
    BL_GAMEMODE="Custom" \
    BL_PORT=28000 \
    BL_EXTRA_ARGS="" \
    BL_INST_NUM_PROC=10 \
    BL_INST_FORCE=0 \
    BL_INST_OVERWRITE=0 \
    BL_UNZIP_ADDONS=0 \
    BL_ADDON_REPO="" \
    BL_ADDON_REPO_BRANCH="" \
    BL_PATCH_ENABLE=1 \
    BL_PATCH_EXE_URL=https://gitlab.com/-/project/19166101/uploads/7e9bc075056c3301fd1247975ecaa218/Blockland.exe \
    BL_PATCH_LOADER_URL=https://gitlab.com/-/project/19166101/uploads/2970aa2d8c23259f8becb0f49630efcc/RedBlocklandLoader.dll \
    BL_PATCH_WINEFIX_URL=https://gitlab.com/-/project/46964875/uploads/91958483cf78ffc270a09824336bd91a/WineFix.dll \
    WINEARCH=win32 \
    WINEDEBUG=-all

EXPOSE 28000/udp
EXPOSE 28050/udp

ENTRYPOINT ["/tini", "-g", "--", "/bin/bash", "-c", "run-bl"]
