#!/bin/bash

# 获取系统信息
get_system_info() {
    if [ -f /etc/os-release ]; then
        # 使用 /etc/os-release 获取系统信息
        source /etc/os-release
        system=$NAME
        version=$VERSION_ID
    elif [ -f /etc/centos-release ]; then
        # 使用 /etc/centos-release 获取 CentOS 7 版本
        system="CentOS"
        version=$(cat /etc/centos-release | grep -oP '(?<=release )\d+' | cut -d '.' -f 1)
    else
        echo "无法识别操作系统。"
        exit 1
    fi
}

# 备份原始软件源配置文件
backup_sources() {
    if [ -f "$1" ]; then
        sudo cp "$1" "$1.bak"
        echo "已备份原始软件源配置文件：$1 -> $1.bak"
    fi
}

# 创建新的软件源配置文件
create_sources() {
    if [ ! -f "$1" ]; then
        sudo touch "$1"
        echo "已创建新的软件源配置文件：$1"
    fi
}

# 替换软件源配置文件
replace_sources() {
    sudo cp "$1" "$2"
    echo "已将 $1 替换为 $2"
}

# 清华源
tuna_source="https://mirrors.tuna.tsinghua.edu.cn"
tuna_sources=(
    "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
    "https://mirrors.tuna.tsinghua.edu.cn/debian/"
    "https://mirrors.tuna.tsinghua.edu.cn/kali/"
    "https://mirrors.tuna.tsinghua.edu.cn/centos/"
    "https://mirrors.tuna.tsinghua.edu.cn/fedora/"
    "https://mirrors.tuna.tsinghua.edu.cn/rocky/"
)

# 阿里源
aliyun_source="https://mirrors.aliyun.com"
aliyun_sources=(
    "https://mirrors.aliyun.com/ubuntu/"
    "https://mirrors.aliyun.com/debian/"
    "https://mirrors.aliyun.com/kali/"
    "https://mirrors.aliyun.com/centos/"
    "https://mirrors.aliyun.com/fedora/"
    "https://mirrors.aliyun.com/rocky/"
)

# 网易源
source_163="http://mirrors.163.com"
sources_163=(
    "http://mirrors.163.com/ubuntu/"
    "http://mirrors.163.com/debian/"
    "http://mirrors.163.com/kali/"
    "http://mirrors.163.com/centos/"
    "http://mirrors.163.com/fedora/"
    "http://mirrors.163.com/rocky/"
)

# 检测源是否可连接
check_source() {
    if ping -c 1 $(echo "$1" | awk -F/ '{print $3}') &> /dev/null; then
        echo "源 $1 可连接。"
    else
        echo "源 $1 无法连接，请检查网络连接。"
        exit 1
    fi
}

# 更新软件包索引
update_package_index() {
    if [[ "$system" == "Debian" || "$system" == "Ubuntu" || "$system" == "Kali" ]]; then
        sudo apt update
    elif [[ "$system" == "CentOS" || "$system" == "Rocky" ]]; then
        sudo yum makecache
    elif [[ "$system" == "Fedora" ]]; then
        sudo dnf makecache
    fi
}

# 选择软件源
select_source() {
    PS3="请选择要使用的软件源编号："
    select source_index in "清华源" "阿里源" "网易源"; do
        case $source_index in
            "清华源")
                selected_source=$tuna_source
                selected_sources=("${tuna_sources[@]}")
                break
                ;;
            "阿里源")
                selected_source=$aliyun_source
                selected_sources=("${aliyun_sources[@]}")
                break
                ;;
            "网易源")
                selected_source=$source_163
                selected_sources=("${sources_163[@]}")
                break
                ;;
            *)
                echo "无效的选择。"
                ;;
        esac
    done
}

# 更换软件源
change_source() {
    # 备份原始软件源配置文件
    backup_sources "$source_file"

    # 创建新的软件源配置文件
    create_sources "$source_file"

    # 选择软件源
    select_source

    # 检测源是否可连接
    check_source "$selected_source"

    # 替换软件源配置文件
    replace_sources "${selected_sources[$(($version-1))]}" "$source_file"

    # 清除缓存
    if [[ "$system" == "Debian" || "$system" == "Ubuntu" || "$system" == "Kali" ]]; then
        sudo apt clean
    elif [[ "$system" == "CentOS" || "$system" == "Rocky" || "$system" == "Fedora" ]]; then
        sudo yum clean all
    fi

    # 更新软件包索引
    update_package_index

    echo "软件源已切换为 $selected_source"
}

# 更新系统
update_system() {
    if [[ "$system" == "Debian" || "$system" == "Ubuntu" || "$system" == "Kali" ]]; then
        sudo apt upgrade -y
    elif [[ "$system" == "CentOS" || "$system" == "Rocky" || "$system" == "Fedora" ]]; then
        sudo yum update -y
    fi

    echo "系统已更新完成。"
}

# 查看当前源和系统信息
view_current_source() {
    get_system_info
    echo "当前系统信息："
    echo "系统名称：$system"
    echo "版本号：$version"
    echo ""
    echo "当前软件源信息："
    if grep -q "$tuna_source" "$source_file"; then
        echo "当前软件源：清华源"
    elif grep -q "$aliyun_source" "$source_file"; then
        echo "当前软件源：阿里源"
    elif grep -q "$source_163" "$source_file"; then
        echo "当前软件源：网易源"
    else
        echo "无法识别当前使用的软件源。"
    fi
}

# 自动检测并显示当前系统和版本号
get_system_info

# 显示当前系统和版本号
echo "当前系统信息："
echo "系统名称：$system"
echo "版本号：$version"
echo ""

# 菜单
while true; do
    echo "--------------------------------------"
    echo "        软件源配置脚本"
    echo "--------------------------------------"
    echo "1. 更换软件源"
    echo "2. 更新系统"
    echo "3. 查看当前源和系统信息"
    echo "4. 退出"
    echo "--------------------------------------"
    read -p "请输入选项编号：" option

    case $option in
        1)
            change_source
            ;;
        2)
            update_system
            ;;
        3)
            view_current_source
            ;;
        4)
            break
            ;;
        *)
            echo "无效的选项。"
            ;;
    esac
done
