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

STORAGE_DIR="/www/wwwroot/storage"
LOG_DIR="${STORAGE_DIR}/agent_browser_logs"
DISPLAY_ID="${AGENT_BROWSER_DISPLAY:-:98}"
VNC_PORT="${AGENT_BROWSER_VNC_PORT:-5908}"
NOVNC_PORT="${AGENT_BROWSER_NOVNC_PORT:-6088}"
SCREEN_SIZE="${AGENT_BROWSER_SCREEN:-1440x1000x24}"

mkdir -p "$LOG_DIR"

chrome_bin() {
  command -v chromium \
    || command -v chromium-browser \
    || command -v google-chrome \
    || command -v google-chrome-stable
}

cdp_alive() {
  local port="$1"
  curl -fsS --max-time 3 "http://127.0.0.1:${port}/json/version" >/dev/null 2>&1
}

ensure_xvfb() {
  if pgrep -f "Xvfb ${DISPLAY_ID}" >/dev/null 2>&1; then
    return 0
  fi
  Xvfb "$DISPLAY_ID" -screen 0 "$SCREEN_SIZE" -nolisten tcp >"${LOG_DIR}/xvfb.log" 2>&1 &
  echo $! > "${LOG_DIR}/xvfb.pid"
  sleep 1
}

ensure_vnc() {
  if ! pgrep -f "x11vnc .*rfbport ${VNC_PORT}" >/dev/null 2>&1; then
    x11vnc -display "$DISPLAY_ID" -localhost -nopw -forever -shared -rfbport "$VNC_PORT" >"${LOG_DIR}/x11vnc.log" 2>&1 &
    echo $! > "${LOG_DIR}/x11vnc.pid"
  fi
  if ! pgrep -f "websockify .*127.0.0.1:${NOVNC_PORT} .*127.0.0.1:${VNC_PORT}" >/dev/null 2>&1; then
    websockify --web=/usr/share/novnc/ "127.0.0.1:${NOVNC_PORT}" "127.0.0.1:${VNC_PORT}" >"${LOG_DIR}/novnc.log" 2>&1 &
    echo $! > "${LOG_DIR}/novnc.pid"
  fi
}

start_provider() {
  local key="$1"
  local port="$2"
  local profile="$3"
  local url="$4"
  local xpos="$5"
  local width="$6"
  local browser
  browser="$(chrome_bin)"

  if cdp_alive "$port"; then
    echo "${key}: running on ${port}"
    return 0
  fi

  mkdir -p "$profile"
  pkill -f "user-data-dir=${profile}" 2>/dev/null || true
  pkill -f "$profile" 2>/dev/null || true
  sleep 1

  DISPLAY="$DISPLAY_ID" "$browser" \
    --no-sandbox \
    --disable-dev-shm-usage \
    --disable-gpu \
    --disable-background-timer-throttling \
    --disable-renderer-backgrounding \
    --disable-backgrounding-occluded-windows \
    --disable-features=CalculateNativeWinOcclusion,IntensiveWakeUpThrottling \
    --password-store=basic \
    --user-data-dir="$profile" \
    --remote-debugging-address=127.0.0.1 \
    --remote-debugging-port="$port" \
    --remote-allow-origins="*" \
    --window-position="${xpos},0" \
    --window-size="${width},960" \
    "$url" >"${LOG_DIR}/${key}_chromium.log" 2>&1 &
  echo $! > "${LOG_DIR}/${key}_chromium.pid"
  sleep 2
  echo "${key}: started on ${port}"
}

ensure_xvfb
ensure_vnc

start_provider "huawei" 9222 "${STORAGE_DIR}/huawei_browser_profile" "https://partner.huaweicloud.com/partnercenter/?region=cn-east-3#/obmgr/growCloud" 0 480
start_provider "ctyun" 9223 "${STORAGE_DIR}/ctyun_browser_profile" "https://www.ctyun.cn/console/agent/index" 480 480
start_provider "volc" 9224 "${STORAGE_DIR}/volc_browser_profile" "https://console.partner.volcengine.com/home/partnerInfo" 960 480

cat <<EOF
三家云代理后台浏览器已启动。

本机执行 SSH 隧道:
ssh -L ${NOVNC_PORT}:127.0.0.1:${NOVNC_PORT} root@115.190.11.134

然后打开:
http://127.0.0.1:${NOVNC_PORT}/vnc.html?autoconnect=1&resize=scale

左/中/右分别处理华为云、天翼云、火山引擎登录。登录完成后直接关闭本机网页即可，不要退出云厂商账号。
EOF
