#!/bin/bash
# Runner script for the SpinButton/moveBefore atomic-move bypass POC.
set -e

DIR="$(cd "$(dirname "$0")" && pwd)"
CHROME="${CHROME:-/chromium/chromium/src/out/asan/chrome}"
PORT="${PORT:-8080}"
PROFILE=$(mktemp -d /tmp/poc-profile.XXXXXX)

cleanup() {
  [ -n "$SRV_PID" ] && kill "$SRV_PID" 2>/dev/null || true
  rm -rf "$PROFILE"
}
trap cleanup EXIT

python3 "$DIR/server.py" "$PORT" &
SRV_PID=$!
sleep 1

# --enable-gpu-benchmarking exposes chrome.gpuBenchmarking.pointerActionSequence
# so the page can synthesize a real mousedown on the spin button (a step that
# would otherwise require a human to click-and-hold). It does not alter the
# code path under test.
"$CHROME" \
    --headless=new --no-sandbox \
    --no-first-run --no-default-browser-check \
    --disable-features=Crashpad --noerrdialogs \
    --user-data-dir="$PROFILE" \
    --enable-gpu-benchmarking \
    --enable-logging=stderr --v=0 \
    "http://127.0.0.1:$PORT/poc.html" 2>&1
