#!/bin/sh

# 使用方法：
# bash docker_vless_reality.sh <nodeid> [domain]

nodeid=$1
domain=$2

if [ -z "$nodeid" ]; then
    echo "❌ 错误：节点 ID 不能为空！"
    echo "✅ 用法：bash $0 <nodeid> [domain]"
    exit 1
fi

# 安装依赖（注释掉以提高运行速度）
# echo "==> 安装依赖中..."
# if grep -qi "centos" /etc/os-release; then
#     yum install -y tar wget git socat
# else
#     apt update && apt-get install -y tar wget git socat
# fi

echo "==> 下载 XrayR-release..."
rm -rf /opt/XrayR-release
cd /opt || exit 1
git clone https://github.com/XrayR-project/XrayR-release

echo "==> 下载 config.yml..."
wget --no-check-certificate -qO /opt/XrayR-release/config/config.yml https://img.digac.icu/Files/LinuxShell/docker_XrayR_vless_reality_config.yml

echo "==> 替换配置内容..."
sed -i 's/API节点/023021.xyz/' /opt/XrayR-release/config/config.yml
sed -i 's/API秘钥/michaeltan057720/' /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 [ -n "$domain" ]; then
    echo "==> 设置证书相关（未启用实际申请逻辑）"
    # 你可以自行启用如下 Cloudflare DNS 证书申请
    # bash <(curl -Lso- https://your.domain/CloudFlare.sh) $domain
    # sed -i 's/CertMode: none/CertMode: dns/' /opt/XrayR-release/config/config.yml
    # sed -i "s/证书域名/${domain}/" /opt/XrayR-release/config/config.yml
    # sed -i 's/证书邮箱/onepve@outlook.com/' /opt/XrayR-release/config/config.yml
    # sed -i 's/证书秘钥/your_key_here/' /opt/XrayR-release/config/config.yml
fi

echo "==> 最终配置如下："
cat /opt/XrayR-release/config/config.yml

echo "==> 启动容器..."
cd /opt/XrayR-release || exit 1
docker-compose up -d

echo "✅ 启动完成，查看运行日志如下："
docker-compose logs -f -t --tail=20
