#!/bin/bash
# 將 awoooi-startup 部署到 192.168.0.188
# 執行方式: bash deploy-to-188.sh

set -euo pipefail
HOST="ollama@192.168.0.188"
PASS="0936223270"

echo "=== 部署 awoooi-startup 到 192.168.0.188 ==="

# 1. 上傳腳本
echo "[1/4] 上傳啟動腳本..."
scp awoooi-startup.sh "$HOST:/tmp/awoooi-startup.sh"

# 2. 上傳 systemd unit
echo "[2/4] 上傳 systemd unit..."
scp awoooi-startup.service "$HOST:/tmp/awoooi-startup.service"

# 3. 安裝
echo "[3/4] 安裝..."
ssh "$HOST" "
echo '$PASS' | sudo -S bash -c '
    cp /tmp/awoooi-startup.sh /usr/local/bin/awoooi-startup.sh
    chmod +x /usr/local/bin/awoooi-startup.sh
    cp /tmp/awoooi-startup.service /etc/systemd/system/awoooi-startup.service
    systemctl daemon-reload
    systemctl enable awoooi-startup.service
    echo done
'
"

# 4. 驗證
echo "[4/4] 驗證安裝..."
ssh "$HOST" "systemctl is-enabled awoooi-startup.service && echo '✅ 已啟用' || echo '❌ 啟用失敗'"

echo ""
echo "✅ 部署完成！"
echo "下次重開機後，188 會自動執行修復並啟動所有服務。"
echo ""
echo "手動測試執行："
echo "  ssh $HOST 'sudo /usr/local/bin/awoooi-startup.sh'"
