# ====================================================================================
# Monster image: Unreal Engine (Linux) + Synapse (Matrix homeserver)
# ====================================================================================
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive \
    TZ=Etc/UTC \
    UE_ROOT=/opt/UnrealEngine \
    SYNAPSE_VENV=/opt/synapse \
    SYNAPSE_CONFIG_PATH=/data/homeserver.yaml

# ------------------------------------------------------------------------------
# Base OS deps
# ------------------------------------------------------------------------------
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates \
      curl \
      wget \
      unzip \
      python3 \
      python3-pip \
      python3-venv \
      libffi-dev \
      libssl-dev \
      libjpeg-dev \
      libpq-dev \
      sqlite3 \
      tini \
      supervisor \
      locales \
      # optional: tools for debugging
      less vim \
    && rm -rf /var/lib/apt/lists/*

# Set UTF-8 locale
RUN locale-gen en_US.UTF-8 && update-locale LANG=en_US.UTF-8
ENV LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8

# ------------------------------------------------------------------------------
# Unreal Engine: install from local ZIP
# ------------------------------------------------------------------------------
# Place Linux_Unreal_Engine_5.7.1.zip next to this Dockerfile before building
COPY Linux_Unreal_Engine_5.7.1.zip /tmp/ue.zip

RUN mkdir -p "${UE_ROOT}" \
    && unzip -q /tmp/ue.zip -d "${UE_ROOT}" \
    && rm /tmp/ue.zip

# If the ZIP unpacks with an extra top-level directory, you may want to normalize:
# RUN mv "${UE_ROOT}"/Linux_Unreal_Engine_*/* "${UE_ROOT}"/ && rmdir "${UE_ROOT}"/Linux_Unreal_Engine_*

# ------------------------------------------------------------------------------
# Synapse (Matrix homeserver) in a virtualenv
# ------------------------------------------------------------------------------
RUN python3 -m venv "${SYNAPSE_VENV}" \
    && "${SYNAPSE_VENV}/bin/pip" install --upgrade pip wheel \
    && "${SYNAPSE_VENV}/bin/pip" install matrix-synapse

# Data directory for Synapse config, keys, DB, etc.
RUN mkdir -p /data \
    && chown -R root:root /data

# ------------------------------------------------------------------------------
# Runtime scripts
# ------------------------------------------------------------------------------

# Synapse start script: expects /data/homeserver.yaml to already exist
# You can mount /data as a volume and pre-generate config per client
RUN mkdir -p /opt/scripts
RUN cat << 'EOF' > /opt/scripts/run-synapse.sh
#!/usr/bin/env bash
set -e

VENV="${SYNAPSE_VENV:-/opt/synapse}"
CONFIG="${SYNAPSE_CONFIG_PATH:-/data/homeserver.yaml}"

if [ ! -f "$CONFIG" ]; then
  echo "ERROR: Synapse config not found at $CONFIG"
  echo "You must generate homeserver.yaml and mount /data or set SYNAPSE_CONFIG_PATH."
  exit 1
fi

exec "$VENV/bin/python" -m synapse.app.homeserver \
    --config-path "$CONFIG"
EOF
RUN chmod +x /opt/scripts/run-synapse.sh

# Unreal start script: adjust for your packaged app
# TODO: Replace the command with your actual cooked Linux server/client binary
RUN cat << 'EOF' > /opt/scripts/run-unreal.sh
#!/usr/bin/env bash
set -e

UE_ROOT="${UE_ROOT:-/opt/UnrealEngine}"

# Example placeholder; replace with your actual binary & project path
# For example, if you have a packaged server:
#   "$UE_ROOT/YourGame/Binaries/Linux/YourGameServer-Linux-Shipping"
if [ ! -x "$UE_ROOT/Engine/Binaries/Linux/UnrealEditor" ]; then
  echo "WARNING: UnrealEditor not found or not executable. Fix /opt/scripts/run-unreal.sh."
fi

# Placeholder: just sleep to avoid crash-loop if you haven't wired in your app yet
# Replace this with the actual Unreal command.
exec "$UE_ROOT/Engine/Binaries/Linux/UnrealEditor" || exec sleep 3600
EOF
RUN chmod +x /opt/scripts/run-unreal.sh

# ------------------------------------------------------------------------------
# Supervisor config: run Synapse + Unreal together
# ------------------------------------------------------------------------------
RUN mkdir -p /etc/supervisor/conf.d

RUN cat << 'EOF' > /etc/supervisor/conf.d/supervisord.conf
[supervisord]
nodaemon=true
logfile=/var/log/supervisord.log
pidfile=/var/run/supervisord.pid

[program:synapse]
command=/opt/scripts/run-synapse.sh
autostart=true
autorestart=true
priority=10
stdout_logfile=/var/log/synapse.log
stderr_logfile=/var/log/synapse.err

[program:unreal]
command=/opt/scripts/run-unreal.sh
autostart=true
autorestart=true
priority=20
stdout_logfile=/var/log/ueapp.log
stderr_logfile=/var/log/ueapp.err
EOF

# ------------------------------------------------------------------------------
# Ports
# ------------------------------------------------------------------------------
# Synapse: 8008 (http), 8448 (federation / tls, if enabled)
# Unreal: you’ll add ports here depending on your game/server (e.g. 7777, 5901 for VNC, etc.)
EXPOSE 8008 8448

WORKDIR /opt

# Use tini as PID 1 for better signal handling
ENTRYPOINT ["/usr/bin/tini", "--"]

CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

