#!/bin/bash

set -e

# 颜色定义
GREEN="\033[32m"
RED="\033[31m"
RESET="\033[0m"

# 选择 Linux 发行版
echo -e "${GREEN}请选择您的 Linux 发行版:${RESET}"
echo "1) Ubuntu"
echo "2) Debian"
echo "3) CentOS"
echo "4) Alpine"
read -p "请输入选项 (1-4): " os_choice

case $os_choice in
    1) OS="ubuntu";;
    2) OS="debian";;
    3) OS="centos";;
    4) OS="alpine";;
    *) echo -e "${RED}无效输入，退出！${RESET}"; exit 1;;
esac

# -------------------------- 修改开始 --------------------------
# 选择 Web 服务器
echo -e "${GREEN}请选择 Web 服务器:${RESET}"
echo "1) darkhttpd (轻量级)"
echo "2) caddy (自动 HTTPS)"
echo "3) Python3 内置 HTTP 服务器"
echo "4) 跳过 (仅安装 Aria2)" # 新增的跳过选项
read -p "请输入选项 (1-4): " web_choice

# 安装必要依赖
# 注：无论是否安装Web服务器，aria2, unzip, curl, wget 都是必须的。
if [[ "$OS" == "ubuntu" || "$OS" == "debian" ]]; then
    apt update && apt install -y aria2 unzip curl wget
    [[ "$web_choice" == "1" ]] && apt install -y darkhttpd
    [[ "$web_choice" == "2" ]] && apt install -y caddy
elif [[ "$OS" == "centos" ]]; then
    yum install -y epel-release
    yum install -y aria2 unzip curl wget
    [[ "$web_choice" == "1" ]] && yum install -y darkhttpd
    [[ "$web_choice" == "2" ]] && yum install -y caddy
elif [[ "$OS" == "alpine" ]]; then
    apk update && apk add aria2 unzip curl wget
    [[ "$web_choice" == "1" ]] && apk add darkhttpd
    [[ "$web_choice" == "2" ]] && apk add caddy
fi

# 让用户自定义 Aria2 配置 (这部分保持不变)
read -p "请输入 Aria2 RPC 端口 (默认 6800): " aria2_port
aria2_port=${aria2_port:-6800}
read -p "请输入 Aria2 RPC 密钥 (默认随机生成): " aria2_secret
aria2_secret=${aria2_secret:-$(openssl rand -hex 8)}

# 配置 Aria2 (这部分保持不变)
mkdir -p /etc/aria2 /var/log/aria2 /var/lib/aria2
touch /etc/aria2/aria2.session
chmod 777 /etc/aria2/aria2.session

cat > /etc/aria2/aria2.conf <<EOF
dir=/var/lib/aria2
file-allocation=none
continue=true
input-file=/etc/aria2/aria2.session
save-session=/etc/aria2/aria2.session
save-session-interval=60
max-concurrent-downloads=5
split=10
min-split-size=10M
max-connection-per-server=10
enable-rpc=true
rpc-allow-origin-all=true
rpc-listen-all=true
rpc-listen-port=$aria2_port
rpc-secret=$aria2_secret
EOF

# 配置 systemd 服务 (这部分保持不变)
cat > /etc/systemd/system/aria2.service <<EOF
[Unit]
Description=Aria2 Service
After=network.target

[Service]
ExecStart=/usr/bin/aria2c --conf-path=/etc/aria2/aria2.conf
Restart=on-failure
User=root

[Install]
WantedBy=multi-user.target
EOF

# 启动 Aria2 (这部分保持不变)
systemctl daemon-reload
systemctl enable aria2
systemctl restart aria2

# ---------------------------------------------------------------
# 只有在用户选择了Web服务器时，才下载和配置 AriaNg 及 Web 服务器
if [[ "$web_choice" != "4" ]]; then
    
    # 安装 AriaNg
    ARIA_NG_PATH="/usr/share/ariang"
    mkdir -p "$ARIA_NG_PATH"
    wget -qO- https://github.com/mayswind/AriaNg/releases/download/1.3.10/AriaNg-1.3.10.zip | unzip - -d "$ARIA_NG_PATH"

    # 启动 Web 服务器
    if [[ "$web_choice" == "1" ]]; then
        cat > /etc/systemd/system/darkhttpd.service <<EOF
[Unit]
Description=Darkhttpd for AriaNg
After=network.target

[Service]
ExecStart=/usr/bin/darkhttpd /usr/share/ariang --port 8080
Restart=on-failure
User=root

[Install]
WantedBy=multi-user.target
EOF
        systemctl enable darkhttpd
        systemctl restart darkhttpd
    elif [[ "$web_choice" == "2" ]]; then
        # 假设 caddy 已经安装并支持 systemd
        # 此处可能需要根据实际 Caddy 安装方式进行微调
        cat > /etc/caddy/Caddyfile <<EOF
:8080 {
    root * /usr/share/ariang
    file_server
}
EOF
        systemctl enable caddy
        systemctl restart caddy
    elif [[ "$web_choice" == "3" ]]; then
        # 注意：使用 rc.local 启动不优雅，但在没有 systemd/init 的简陋环境可能有用
        echo "python3 -m http.server 8080 --directory /usr/share/ariang &" >> /etc/rc.local
        chmod +x /etc/rc.local
        # 立即运行一次（如果rc.local存在且有效）
        if [ -f /etc/rc.local ]; then
             /etc/rc.local
        else
             echo -e "${RED}警告: 未能找到或运行 /etc/rc.local 来启动 Python Web 服务器。${RESET}"
        fi
    fi

fi
# -------------------------- 修改结束 --------------------------

echo -e "${GREEN}安装完成！${RESET}"
echo -e "Aria2 RPC 地址: ${GREEN}http://your_server_ip:$aria2_port${RESET}"
echo -e "Aria2 RPC 密钥: ${GREEN}$aria2_secret${RESET}"

# 如果跳过，给用户提示
if [[ "$web_choice" == "4" ]]; then
    echo -e "${GREEN}Web 服务器已跳过。您需要自行配置一个 Web 前端或使用外部 AriaNg 客户端连接。${RESET}"
else
    echo -e "AriaNg 访问地址: ${GREEN}http://your_server_ip:8080${RESET}"
fi