#!/bin/sh

# 1️⃣ 卸载 Soga 并删除配置
if [ -f /usr/local/bin/soga ]; then
    /usr/local/bin/soga uninstall
    echo "✅ Soga 已卸载"
fi

# 删除 Soga 配置文件
rm -rf /etc/soga /usr/local/etc/soga /opt/soga
echo "✅ Soga 配置已清理"

# 2️⃣ 设置时区为上海
echo "👉 设置时区为 Asia/Shanghai"
timedatectl set-timezone Asia/Shanghai

# 3️⃣ 同步时间
echo "👉 正在同步时间..."
if ntpdate -u ntp.aliyun.com >/dev/null 2>&1; then
    echo "✅ 时间已同步（使用 ntpdate）"
else
    echo "⚠️ ntpdate 同步失败，尝试使用 chronyd"
    systemctl restart chronyd
    chronyc -a makestep
    echo "✅ 时间已同步（使用 chronyd）"
fi

# 4️⃣ 检查 Docker 和 docker-compose
if ! command -v docker >/dev/null 2>&1; then
    echo "❌ Docker 未安装，请先手动安装 Docker"
    exit 1
fi

if ! command -v docker-compose >/dev/null 2>&1; then
    echo "❌ docker-compose 未安装，请先手动安装 docker-compose"
    exit 1
fi

# 5️⃣ 交互式输入节点ID
read -p "请输入节点ID: " nodeid
if [ -z "$nodeid" ]; then
    echo "❌ 节点ID不能为空！"
    exit 1
fi

# 6️⃣ 删除旧 XrayR-release 并拉取最新版本
rm -rf /opt/XrayR-release
cd /opt
git clone https://github.com/XrayR-project/XrayR-release

# 7️⃣ 下载默认 config.yml 并修改
wget --no-check-certificate -qO- https://sh.digac.icu/shell/docker_XrayR_vless_reality_config.yml > /opt/XrayR-release/config/config.yml
sed -i 's/API节点/xxx.xxxxxx.com/' /opt/XrayR-release/config/config.yml
sed -i 's/API秘钥/你里填写你的API密钥!!!!!!!!!/' /opt/XrayR-release/config/config.yml
sed -i "s/节点序号/${nodeid}/" /opt/XrayR-release/config/config.yml
sed -i 's/NodeType: V2ray/NodeType: Vless/' /opt/XrayR-release/config/config.yml
sed -i 's/EnableVless: false/EnableVless: true/' /opt/XrayR-release/config/config.yml
sed -i 's/EnableREALITY: false/EnableREALITY: true/' /opt/XrayR-release/config/config.yml
sed -i 's/DisableLocalREALITYConfig: false/DisableLocalREALITYConfig: true/' /opt/XrayR-release/config/config.yml
sed -i 's/Show: true/Show: false/' /opt/XrayR-release/config/config.yml

# 跳过域名部分，不做任何操作
if [ 1 -eq 0 ]; then :; fi

# 8️⃣ 输出配置并启动 Docker
cat /opt/XrayR-release/config/config.yml
cd /opt/XrayR-release
docker-compose up -d
docker-compose logs -f -t --tail=10