#!/usr/bin/env bash
# FlexUI CLI — delegates to scripts/<command>.sh
set -euo pipefail

# Guard: these scripts require Git Bash (MSYS), NOT WSL bash. Windows 的系统
# bash (C:\Windows\System32\bash.exe) 可能被 WSL 劫持,WSL 未装发行版时会
# 直接失败/超时且无有效输出。早期拦截避免误用。
if [[ "$(uname -s 2>/dev/null)" == "Linux" ]]; then
  echo "ERROR: running under WSL bash — FlexUI build scripts need Git Bash (MSYS)." >&2
  echo "Use: \"D:\\Program Files\\Git\\bin\\bash.exe\" -lc './build.sh <cmd>'" >&2
  exit 1
fi

REPO_ROOT="$(cd "$(dirname "$0")" && pwd)"

usage() {
  echo "Usage: $0 <command> [args]"
  echo ""
  echo "  Options: --platform android  (default: ohos)"
  echo ""
  echo "  Android commands (scripts/android/):"
  echo "    dev            Build jsfwk + demo → gradlew assembleDebug → adb install → start"
  echo "    build-apk      gradlew assembleDebug / assembleRelease"
  echo "    install        adb install -r"
  echo "    start          adb am start"
  echo "    logs           adb logcat filtered"
  echo "    clean          gradlew clean"
  echo "    pkg            Build AARs (flexui_engine + agent_runtime + as_apis) → build/pkg-<date>/android/"
  echo "                     Options: --release (default: debug)"
  echo ""
  echo "  OHOS commands (scripts/):"
  echo "    dev              ASCF compile → sync rawfile → build HAP → install → start (full cycle)"
  echo "    debug-server     Start/stop/status/forward debug server for CDP inspector"
  echo "    get-inspector    Fetch DOM+CSS via CDP from a connected debug target"
  echo "    build            ASCF compile → sync OHOS+Android → hvigorw assembleHap"
  echo "    build-hap        hvigorw assembleHap only (no ASCF compile) [--no-engine]"
  echo "                     --no-engine: skip flexui_engine HAR rebuild (faster for demo/card changes)"
  echo "    build-test       Build engine-test-demo (Test Lab) HAP → auto hdc install"
  echo "    test-engine [level]  Run engine-test-demo regression (auto PC layout server + fport,"
  echo "                     install → launch(ascfTest, level) → poll [TEST])"
  echo "                     level: basic/feature/bug/all (default all)"
  echo "    build-demo       ASCF compile + sync to OHOS + Android assets"
  echo "    install          hdc install -r HAP to device"
  echo "    start            hdc aa start EntryAbility"
  echo ""
  echo "    build-agent      hvigorw assembleHap for ai-agent"
  echo "    install-agent    hdc install agent HAP"
  echo "    dev-agent        compile ASCF → sync → build agent → install → start"
  echo "    sync-ascf-rawfile Sync ASCF rawfile to agent"
  echo ""
  echo "    build-jsfwk      Build flexui_jsfwk.js
    build-engine     Build flexui_jsfwk.js → flexui_engine HAR [--release]
    build-engine-to-phone  build-engine → hdc push HAR to /data/local/tmp/ [--release]"
  echo "    check-env        Print toolchain status (DevEco SDK/hvigorw/ohpm/hdc/node/bash)"
  echo "    preview          Start ASCF preview dev server"
  echo "    clean            Remove build caches + ohpm install"
  echo "    clean-build      clean + build-agent"
  echo "    logs             Show filtered app logs"
  echo "    status           Check app process + crash logs + events"
  echo "    backup-config    一键备份所有本地敏感配置(sign.sh + llm-config.sh + android-sign.sh)"
  echo "    restore-config   一键恢复所有本地敏感配置(同上 restore;恢复后请勿提交相关改动)"
  echo "    pkg              Build SDK artifacts → build/pkg-<date>/<platform>/"
  echo "                     OHOS (default): flexui_engine.har + agent_runtime.har + as_apis.har"
  echo "                     Android: flexui_engine.aar + agent_runtime.aar + as_apis.aar"
  echo "                     Options: --release (default: debug)"
  exit 1
}

# Parse --platform <value> from anywhere in args
PLATFORM="ohos"
FILTERED_ARGS=()
while [[ $# -gt 0 ]]; do
  case "$1" in
    --platform)
      PLATFORM="$2"
      shift 2
      ;;
    *)
      FILTERED_ARGS+=("$1")
      shift
      ;;
  esac
done
set -- "${FILTERED_ARGS[@]}"

cmd="${1:-}"
shift 2>/dev/null || true

if [ -z "$cmd" ]; then
  usage
fi

# Route to platform-specific script dir
SCRIPT_DIR="$REPO_ROOT/scripts"
if [ "$PLATFORM" = "android" ]; then
  # build-demo is platform-agnostic — always use scripts/build-demo.sh
  if [ "$cmd" != "build-demo" ]; then
    SCRIPT_DIR="$REPO_ROOT/scripts/android"
  fi
fi

script="$SCRIPT_DIR/${cmd}.sh"
if [ ! -f "$script" ]; then
  echo "Unknown command: $cmd (platform=$PLATFORM)"
  usage
fi

export REPO_ROOT
export PLATFORM
exec bash "$script" "$@"