#!/bin/bash
# Start Xvfb (Virtual Framebuffer) for Jibri recording

set -e

DISPLAY_NUM="${DISPLAY_NUM:-0}"
RESOLUTION="${RESOLUTION:-1920x1080x24}"

echo "[Xvfb] Starting virtual display :${DISPLAY_NUM} with resolution ${RESOLUTION}"

# Remove any existing lock files
rm -f /tmp/.X${DISPLAY_NUM}-lock

# Start Xvfb in background
Xvfb :${DISPLAY_NUM} -screen 0 ${RESOLUTION} -ac +extension GLX +render -noreset &

# Wait for Xvfb to start
sleep 2

# Verify display is working
if xdpyinfo -display :${DISPLAY_NUM} >/dev/null 2>&1; then
    echo "[Xvfb] Display :${DISPLAY_NUM} is ready"
else
    echo "[Xvfb] ERROR: Display :${DISPLAY_NUM} failed to start"
    exit 1
fi
