#!/bin/bash
echo "🔄 正在重啟 OpenClaw Gateway..."
# 殺掉現有 process
PID=$(ps aux | grep -i openclaw | grep -v grep | awk '{print $2}' | head -1)
if [ -n "$PID" ]; then
    kill -9 $PID 2>/dev/null
    echo "✅ 已終止舊程序 (PID: $PID)"
fi
sleep 2
# 透過 launchd 重啟
launchctl unload ~/Library/LaunchAgents/ai.openclaw.gateway.plist 2>/dev/null
sleep 1
launchctl load ~/Library/LaunchAgents/ai.openclaw.gateway.plist
sleep 5
# 確認啟動
NEW_PID=$(ps aux | grep -i openclaw | grep -v grep | awk '{print $2}' | head -1)
if [ -n "$NEW_PID" ]; then
    echo "✅ OpenClaw 已成功重啟 (PID: $NEW_PID)"
    echo "🌐 儀表板：http://localhost:18789"
else
    echo "❌ 重啟失敗，請手動檢查"
fi
