#!/bin/bash
# Jibri Entrypoint mit Xvfb Support
# Startet zuerst Xvfb, dann den normalen Jibri-Prozess

set -e

echo "=== BreakPilot Jibri Entrypoint ==="
echo "DISPLAY: ${DISPLAY}"
echo "RESOLUTION: ${RESOLUTION}"

# Start Xvfb
echo "[Entrypoint] Starting Xvfb..."
/usr/local/bin/start-xvfb.sh

# Export DISPLAY for child processes
export DISPLAY=${DISPLAY:-:0}

# Optional: Start x11vnc for debugging (only if VNC_PASSWORD is set)
if [ -n "${VNC_PASSWORD}" ]; then
    echo "[Entrypoint] Starting x11vnc on port 5900..."
    x11vnc -display ${DISPLAY} -forever -passwd "${VNC_PASSWORD}" -bg -rfbport 5900
fi

echo "[Entrypoint] Starting Jibri..."

# Execute the original Jibri entrypoint (s6-based init)
exec /init "$@"
