#!/bin/bash
# =========================================================
# 🚀 一键安装 / 配置 原生 XrayR（非 Docker）
# 与 docker 版功能相同，支持 Reality + XBoard
# =========================================================

set -e

# -------------------------------
# 1️⃣ 卸载旧 Soga / XrayR
# -------------------------------
if [ -f /usr/local/bin/soga ]; then
    /usr/local/bin/soga uninstall || true
    echo "✅ Soga 已卸载"
fi

if systemctl list-unit-files | grep -q XrayR.service; then
    systemctl stop XrayR
    systemctl disable XrayR
    echo "✅ 停止旧 XrayR 服务"
fi

rm -rf /etc/soga /usr/local/etc/soga /opt/soga
rm -rf /etc/XrayR /usr/local/XrayR /usr/local/bin/XrayR
echo "✅ 已清理旧配置"

# -------------------------------
# 2️⃣ 设置时区 + 同步时间
# -------------------------------
echo "👉 设置时区为 Asia/Shanghai"
if command -v timedatectl >/dev/null 2>&1; then
    timedatectl set-timezone Asia/Shanghai
else
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
fi

echo "👉 同步时间..."
if command -v ntpdate >/dev/null 2>&1; then
    ntpdate -u ntp.aliyun.com >/dev/null 2>&1 && echo "✅ 时间已同步（ntpdate）" || echo "⚠️ ntpdate 同步失败"
else
    if [ -f /etc/alpine-release ]; then
        apk add --no-cache openntpd
        ntpd -d -n -q -p ntp.aliyun.com
    else
        if command -v apt >/dev/null 2>&1; then
            apt install -y ntpdate
        elif command -v yum >/dev/null 2>&1; then
            yum install -y ntpdate
        fi
        ntpdate -u ntp.aliyun.com >/dev/null 2>&1
    fi
fi

echo "🕒 当前系统时间：$(date)"
echo "----------------------------------------"

# -------------------------------
# 3️⃣ 安装 XrayR
# -------------------------------
echo "📦 正在安装 XrayR..."
bash <(curl -Ls https://raw.githubusercontent.com/XrayR-project/XrayR-release/master/install.sh)

if ! command -v XrayR >/dev/null 2>&1; then
    echo "❌ 安装失败，请检查网络或 GitHub 访问"
    exit 1
fi

echo "✅ XrayR 已安装"

# -------------------------------
# 4️⃣ 交互输入信息
# -------------------------------
read -p "请输入节点ID: " nodeid
read -p "请输入 XBoard API 地址 (例如: https://xapi.reality.pp.ua): " api_host
read -p "请输入 XBoard API 密钥: " api_key

# -------------------------------
# 5️⃣ 生成配置文件
# -------------------------------
mkdir -p /etc/XrayR
CONFIG_PATH="/etc/XrayR/config.yml"

cat > $CONFIG_PATH <<EOF
Log:
  Level: none
DnsConfigPath: ""
Nodes:
  - PanelType: "XBoard"
    ApiConfig:
      ApiHost: "$api_host"
      ApiKey: "$api_key"
      NodeID: $nodeid
      NodeType: V2ray
      Timeout: 30
      EnableVless: true
      EnableREALITY: true
      DisableLocalREALITYConfig: true
    ControllerConfig:
      ListenIP: 0.0.0.0
      UpdatePeriodic: 60
      EnableDNS: false
      CertConfig:
        CertMode: none
      REALITYConfig:
        Show: false
EOF

echo "✅ 已生成配置文件：$CONFIG_PATH"

# -------------------------------
# 6️⃣ 启动并查看状态
# -------------------------------
systemctl daemon-reload
systemctl enable XrayR
systemctl restart XrayR
sleep 2

echo "----------------------------------------"
systemctl status XrayR --no-pager | head -n 10
echo "----------------------------------------"
echo "✅ XrayR 已启动"
echo "🌐 节点ID: $nodeid"
echo "🔑 API地址: $api_host"
echo "🧩 API密钥: $api_key"
echo "🎭 Fingerprint: $FINGERPRINT"
echo "📁 配置路径: $CONFIG_PATH"
echo "----------------------------------------"
