#!/bin/bash
set -e

echo "[entrypoint] Xvfb 起動中..."
rm -f /tmp/.X99-lock
Xvfb :99 -screen 0 ${SCREEN_WIDTH}x${SCREEN_HEIGHT}x${SCREEN_DEPTH} -ac +extension GLX +render -noreset &
sleep 2

echo "[entrypoint] x11vnc 起動中..."
x11vnc -display :99 -forever -nopw -shared -rfbport ${VNC_PORT} -bg -o /var/log/x11vnc.log
sleep 1

echo "[entrypoint] noVNC 起動中 (port ${NOVNC_PORT})..."
websockify --web /usr/share/novnc ${NOVNC_PORT} localhost:${VNC_PORT} &
sleep 1

echo "[entrypoint] mercari_server.py 起動中 (port ${MERCARI_PORT})..."
echo "[entrypoint] noVNC: http://localhost:${NOVNC_PORT}/vnc.html"
echo "[entrypoint] 検索API: http://localhost:${MERCARI_PORT}/search?keyword=xxx"

# --gui モードで起動（Xvfb が DISPLAY=:99 を提供するので GUI モードで動く）
exec python /app/mercari_server.py --gui --bind 0.0.0.0 --port ${MERCARI_PORT}
