#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
CHROMIUM="${CHROMIUM:-$(command -v chromium || command -v chromium-browser || command -v google-chrome || command -v google-chrome-stable || true)}"

if [[ -z "$CHROMIUM" ]]; then
  echo "missing Chromium-compatible browser; set CHROMIUM=/path/to/browser" >&2
  exit 1
fi

if [[ "${SKIP_INI_BUILD:-0}" != "1" ]]; then
  "$ROOT_DIR/tools/build_ini_panel.sh"
fi
if [[ "${SKIP_INTERP_BUILD:-0}" != "1" ]]; then
  "$ROOT_DIR/tools/build_wasm_core.sh"
fi

TMP_DIR="$(mktemp -d)"
PORT_FILE="$TMP_DIR/port"
SERVER_LOG="$TMP_DIR/server.log"
CHROME_PROFILE="$TMP_DIR/chrome-profile"
mkdir -p "$CHROME_PROFILE"

cleanup() {
  if [[ -n "${SERVER_PID:-}" ]]; then
    kill "$SERVER_PID" 2>/dev/null || true
    wait "$SERVER_PID" 2>/dev/null || true
  fi
  rm -rf "$TMP_DIR"
}
trap cleanup EXIT

python3 - <<'PY' "$ROOT_DIR" "$PORT_FILE" >"$SERVER_LOG" 2>&1 &
import functools
import http.server
import pathlib
import socketserver
import sys

root = pathlib.Path(sys.argv[1])
port_file = pathlib.Path(sys.argv[2])

handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=str(root))
with socketserver.TCPServer(("127.0.0.1", 0), handler) as httpd:
    port_file.write_text(str(httpd.server_address[1]), encoding="ascii")
    httpd.serve_forever()
PY
SERVER_PID=$!

for _ in $(seq 1 100); do
  [[ -s "$PORT_FILE" ]] && break
  sleep 0.05
done

if [[ ! -s "$PORT_FILE" ]]; then
  echo "browser smoke HTTP server did not start" >&2
  cat "$SERVER_LOG" >&2 || true
  exit 1
fi

PORT="$(cat "$PORT_FILE")"
URL="http://127.0.0.1:$PORT/tests/browser/ini_panel_smoke.html"
CONTROL_URL="http://127.0.0.1:$PORT/tests/browser/ini_panel_control_page_smoke.html"
LAUNCH_URL="http://127.0.0.1:$PORT/tests/browser/ini_panel_launch_smoke.html"
WORKFLOW_OVERVIEW_URL="http://127.0.0.1:$PORT/tests/browser/ini_panel_workflow_overview_smoke.html"
SHELL_INTEGRATION_URL="http://127.0.0.1:$PORT/tests/browser/ini_panel_shell_integration_workflow_smoke.html"
OUT="$TMP_DIR/chromium.out"
CONTROL_OUT="$TMP_DIR/chromium-control.out"
LAUNCH_OUT="$TMP_DIR/chromium-launch.out"
WORKFLOW_OVERVIEW_OUT="$TMP_DIR/chromium-workflow-overview.out"
SHELL_INTEGRATION_OUT="$TMP_DIR/chromium-shell-integration.out"

"$CHROMIUM" \
  --headless=new \
  --disable-gpu \
  --no-sandbox \
  --user-data-dir="$CHROME_PROFILE" \
  --virtual-time-budget=10000 \
  --dump-dom \
  "$URL" >"$OUT" 2>&1

if ! grep -Fq "browser_ini_opfs_smoke=ok" "$OUT"; then
  echo "browser INI/OPFS smoke failed" >&2
  sed -n '1,220p' "$OUT" >&2
  exit 1
fi

echo "browser_ini_opfs_smoke=ok"

"$CHROMIUM" \
  --headless=new \
  --disable-gpu \
  --no-sandbox \
  --user-data-dir="$CHROME_PROFILE" \
  --virtual-time-budget=10000 \
  --dump-dom \
  "$CONTROL_URL" >"$CONTROL_OUT" 2>&1

if ! grep -Fq "browser_ini_control_page_smoke=ok" "$CONTROL_OUT"; then
  echo "browser INI control page smoke failed" >&2
  sed -n '1,220p' "$CONTROL_OUT" >&2
  exit 1
fi

echo "browser_ini_control_page_smoke=ok"

"$CHROMIUM" \
  --headless=new \
  --disable-gpu \
  --no-sandbox \
  --user-data-dir="$CHROME_PROFILE" \
  --virtual-time-budget=10000 \
  --dump-dom \
  "$LAUNCH_URL" >"$LAUNCH_OUT" 2>&1

if ! grep -Fq "browser_ini_launch_smoke=ok" "$LAUNCH_OUT"; then
  echo "browser INI launch smoke failed" >&2
  sed -n '1,220p' "$LAUNCH_OUT" >&2
  exit 1
fi

echo "browser_ini_launch_smoke=ok"

"$CHROMIUM" \
  --headless=new \
  --disable-gpu \
  --no-sandbox \
  --user-data-dir="$CHROME_PROFILE" \
  --virtual-time-budget=10000 \
  --dump-dom \
  "$WORKFLOW_OVERVIEW_URL" >"$WORKFLOW_OVERVIEW_OUT" 2>&1

if ! grep -Fq "browser_ini_workflow_overview_smoke=ok" "$WORKFLOW_OVERVIEW_OUT"; then
  echo "browser INI workflow overview smoke failed" >&2
  sed -n '1,220p' "$WORKFLOW_OVERVIEW_OUT" >&2
  exit 1
fi

echo "browser_ini_workflow_overview_smoke=ok"

"$CHROMIUM" \
  --headless=new \
  --disable-gpu \
  --no-sandbox \
  --user-data-dir="$CHROME_PROFILE" \
  --virtual-time-budget=10000 \
  --dump-dom \
  "$SHELL_INTEGRATION_URL" >"$SHELL_INTEGRATION_OUT" 2>&1

if ! grep -Fq "browser_ini_shell_integration_workflow_smoke=ok" "$SHELL_INTEGRATION_OUT"; then
  echo "browser INI shell integration workflow smoke failed" >&2
  sed -n '1,220p' "$SHELL_INTEGRATION_OUT" >&2
  exit 1
fi

echo "browser_ini_shell_integration_workflow_smoke=ok"
