#!/usr/bin/env bash
# 无线主控：设备端 Termux Agent + 本机 Frida，无需 Mac ADB
# 用法: bash ensure_ws_agent_wireless.sh [SDK_HOST:PORT] [device_id]
set -euo pipefail
SDK_HOST="${1:-127.0.0.1:8899}"
DEVICE_ID="${2:-xgfe65eimrrofyws}"
WS_URL="ws://${SDK_HOST}/ws/device/${DEVICE_ID}"

echo "================================================================"
echo "  工作手机 · 无线主控（WebSocket + 设备本机 Frida）"
echo "  不需要 Mac USB ADB；execute/probe 全走 WS"
echo "================================================================"
echo ""
echo "【请在红米 Termux 执行】"
echo ""
FRIDA_PORT="$(python3 -c "import json; print(json.load(open('$(dirname "$0")/anti_detect/phantom_frida_config.json'))['listen_port'])" 2>/dev/null || echo 17263)"
cat <<EOF
# 1) 确保 frida-server 在手机本机运行（Root，phantom 端口见 phantom_frida_config.json）
# 2) 启动 Agent（连 SDK）：
export WP_AGENT_ON_DEVICE=1
export WP_FRIDA_MODE=remote
export WP_FRIDA_PORT=${FRIDA_PORT}
export WP_AUTO_DISCOVER=1
cd ~/workphone/agent   # 或你同步 agent 目录的路径
python agent.py -d ${DEVICE_ID} --heartbeat 10
# 可选手填: -s ${WS_URL}
EOF
echo ""
echo "【Mac 端仅保留 SDK】"
echo "  curl http://${SDK_HOST}/api/v3/connection/status"
echo "  curl http://${SDK_HOST}/api/v3/hook/probe/${DEVICE_ID}"
echo ""
echo "【勿再使用】Mac 上 ensure_ws_agent.sh（需 ADB forward，与无线口径冲突）"
echo ""
# 可选：停掉 Mac 侧假 Agent
pkill -f "agent.py -d ${DEVICE_ID} -s ws://" 2>/dev/null || true
curl -s "http://${SDK_HOST}/api/v3/connection/status" | python3 -m json.tool 2>/dev/null | head -20 || true
