#!/bin/bash
# Reproduce the IntersectionObserver V2 / PEPC occlusion bypass.
#
# Usage: CHROME=/path/to/chrome ./run.sh
set -e

DIR="$(cd "$(dirname "$0")" && pwd)"
CHROME="${CHROME:-/chromium/chromium/src/out/asan/chrome}"
PORT=8080

pkill -f "server.py" 2>/dev/null || true
python3 "$DIR/server.py" >/dev/null 2>&1 &
SERVER_PID=$!
trap 'kill $SERVER_PID 2>/dev/null || true' EXIT
sleep 1

run_chrome() {
  local url="$1" profile="$2"
  rm -rf "$profile"
  timeout 15 "$CHROME" \
    --no-first-run --no-default-browser-check --no-sandbox \
    --disable-gpu --headless=new --window-size=1200,900 \
    --enable-logging=stderr --v=0 \
    --user-data-dir="$profile" \
    "$url" 2>&1 | grep -E '\[POC' || true
}

echo "=== poc.html : IntersectionObserver V2 isVisible bypass ==="
run_chrome "http://127.0.0.1:${PORT}/poc.html" /tmp/chrome-poc-profile-1
echo
echo "=== poc-geolocation.html : <geolocation> PEPC occlusion-guard bypass ==="
run_chrome "http://127.0.0.1:${PORT}/poc-geolocation.html" /tmp/chrome-poc-profile-2
