#!/bin/bash

# ==========================================
# OpenClaw Dashboard 自动化部署脚本
# ==========================================

# 确保脚本以 root 权限运行
if [ "$EUID" -ne 0 ]; then
  echo "错误: 请使用 sudo 或 root 权限运行此脚本"
  exit 1
fi

# 变量配置
BINARY_URL="https://cdn.x-aio.com/X-AIO/tools/openclaw-dashboard" 

DASHBOARD_DIR="/opt/openclaw-dashboard"
BACKUP_DIR="/opt/openclaw-conf-backup"
SERVICE_FILE="/etc/systemd/system/openclaw-dashboard.service"

echo "====================================="
echo "1. 准备目录并下载二进制文件..."
echo "====================================="
# 创建目录（如果不存在）
mkdir -p "$DASHBOARD_DIR"

systemctl stop openclaw-dashboard | true

# 下载文件并覆盖原有文件 (-O 参数直接写入指定路径并覆盖)
echo "正在从 $BINARY_URL 下载..."
# curl -L -o "$DASHBOARD_DIR/openclaw-dashboard" "$BINARY_URL"
wget -O "$DASHBOARD_DIR/openclaw-dashboard" "$BINARY_URL"

# 赋予可执行权限
chmod +x "$DASHBOARD_DIR/openclaw-dashboard"

# 授予 ubuntu:ubuntu 权限
chown -R ubuntu:ubuntu "$DASHBOARD_DIR"
echo "目录 $DASHBOARD_DIR 权限已更新为 ubuntu:ubuntu"


echo -e "\n====================================="
echo "2. 创建配置文件备份目录..."
echo "====================================="
mkdir -p "$BACKUP_DIR"
chown -R ubuntu:ubuntu "$BACKUP_DIR"
echo "目录 $BACKUP_DIR 权限已更新为 ubuntu:ubuntu"


echo -e "\n====================================="
echo "3. 写入 systemd 服务文件..."
echo "====================================="
# 使用 cat 写入服务文件配置，EOF 保证内容原样输出
cat > "$SERVICE_FILE" << 'EOF'
[Unit]
Description=OpenClaw Dashboard
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/opt/openclaw-dashboard
Environment="APP_PORT=18780"
Environment="X_AIO_CODE_BASE_URL=http://172.20.0.3:8084"
Environment="PATH=/home/ubuntu/.nvm/versions/node/v24.14.0/bin:/home/linuxbrew/.linuxbrew/opt/node@24/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
ExecStart=/bin/bash -c /opt/openclaw-dashboard/openclaw-dashboard
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

echo "服务文件已写入到 $SERVICE_FILE"


echo -e "\n====================================="
echo "4. 重新加载 systemd、启动并设置自启动..."
echo "====================================="
systemctl daemon-reload
systemctl enable openclaw-dashboard
systemctl restart openclaw-dashboard
echo "服务已配置为开机自启并已启动。"


echo -e "\n====================================="
echo "5. 检查服务状态..."
echo "====================================="
# 等待几秒钟，确保服务有足够的时间启动
sleep 3 

# 打印 systemctl 状态 (不分页)
systemctl status openclaw-dashboard --no-pager

# 更新模型列表
curl -X POST http://127.0.0.1:18780/api/model_provider/refresh-models -H "Content-Type: application/json" -d '{}'

echo -e "\n正在通过 curl 测试本地端口状态..."
# 测试 HTTP 接口
CURL_RESULT=$(curl -s http://127.0.0.1:18780/status)

if [ -n "$CURL_RESULT" ]; then
    echo "接口响应: $CURL_RESULT"
    echo -e "\n✅ 部署完成！OpenClaw Dashboard 正在运行。"
else
    echo -e "\n⚠️ 警告: 无法获取 curl 响应。请检查上方 systemctl 日志看服务是否报错崩溃。"
fi

echo -e "\n====================================="
echo "6. 写入 systemd 服务文件..."
echo "====================================="
# 使用 cat 写入服务文件配置，EOF 保证内容原样输出
cat > "$SERVICE_FILE" << 'EOF'
[Unit]
Description=OpenClaw Dashboard
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/opt/openclaw-dashboard
Environment="APP_PORT=18780"
Environment="X_AIO_CODE_BASE_URL=http://172.20.0.3:8084"
Environment="IP_WHITELIST=172.20.0.3"
Environment="PATH=/home/ubuntu/.nvm/versions/node/v24.14.0/bin:/home/linuxbrew/.linuxbrew/opt/node@24/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
ExecStart=/bin/bash -c /opt/openclaw-dashboard/openclaw-dashboard
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

echo "服务文件已写入到 $SERVICE_FILE"

echo -e "\n====================================="
echo "4. 重新加载 systemd、启动并设置自启动..."
echo "====================================="
systemctl daemon-reload
systemctl enable openclaw-dashboard
systemctl restart openclaw-dashboard
echo "服务已配置为开机自启并已启动。"