#!/bin/bash

# 自动检测系统类型和版本
detect_system() {
    if [ -f /etc/centos-release ]; then
        OS="CentOS"
        VERSION_ID=$(rpm -q --queryformat '%{VERSION}' centos-release)
        DEFAULT_SOURCE="official"
    elif [ -f /etc/rocky-release ]; then
        OS="Rocky"
        VERSION_ID=$(rpm -q --queryformat '%{VERSION}' rocky-release)
        DEFAULT_SOURCE="official"
    elif [ -f /etc/almalinux-release ]; then
        OS="AlmaLinux"
        VERSION_ID=$(rpm -q --queryformat '%{VERSION}' almalinux-release)
        DEFAULT_SOURCE="official"
    elif [ -f /etc/lsb-release ]; then
        . /etc/lsb-release
        if [[ "$DISTRIB_ID" == "Ubuntu" ]]; then
            OS="Ubuntu"
            DEFAULT_SOURCE="official"
        elif [[ "$DISTRIB_ID" == "Debian" ]]; then
            OS="Debian"
            DEFAULT_SOURCE="official"
        elif [[ "$DISTRIB_ID" == "Kali" ]]; then
            OS="Kali"
            DEFAULT_SOURCE="official"
        fi
    elif [ -f /etc/arch-release ]; then
        OS="Arch"
        DEFAULT_SOURCE="official"
    elif [ -f /etc/openwrt_release ]; then
        OS="OpenWrt"
        DEFAULT_SOURCE="official"
    else
        echo "该脚本只支持 CentOS、Rocky、AlmaLinux、Ubuntu、Debian、Kali、Arch 和 OpenWrt 系统。"
        exit 1
    fi
}

# 自动备份源文件
auto_backup_sources() {
    echo "正在自动备份当前源文件..."
    BACKUP_SUFFIX=$(date +"%Y%m%d%H%M%S")
    case "$OS" in
        CentOS | Rocky | AlmaLinux)
            cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak_$BACKUP_SUFFIX
            ;;
        Ubuntu | Debian | Kali)
            cp /etc/apt/sources.list /etc/apt/sources.list.bak_$BACKUP_SUFFIX
            ;;
        Arch)
            cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak_$BACKUP_SUFFIX
            ;;
        OpenWrt)
            cp /etc/opkg/customfeeds.conf /etc/opkg/customfeeds.conf.bak_$BACKUP_SUFFIX
            ;;
    esac
    echo "自动备份完成，备份文件已存储为 *.bak_$BACKUP_SUFFIX"
}

# 手动备份源文件
backup_sources() {
    echo "正在手动备份源文件..."
    case "$OS" in
        CentOS | Rocky | AlmaLinux)
            cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
            ;;
        Ubuntu | Debian | Kali)
            cp /etc/apt/sources.list /etc/apt/sources.list.bak
            ;;
        Arch)
            cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak
            ;;
        OpenWrt)
            cp /etc/opkg/customfeeds.conf /etc/opkg/customfeeds.conf.bak
            ;;
    esac
    echo "手动备份完成。"
}

# 还原源文件
restore_sources() {
    echo "正在还原源文件..."
    case "$OS" in
        CentOS | Rocky | AlmaLinux)
            if [ -f /etc/yum.repos.d/CentOS-Base.repo.bak ]; then
                mv /etc/yum.repos.d/CentOS-Base.repo.bak /etc/yum.repos.d/CentOS-Base.repo
                echo "$OS 源已还原。"
            else
                echo "未找到备份文件。"
            fi
            ;;
        Ubuntu | Debian | Kali)
            if [ -f /etc/apt/sources.list.bak ]; then
                mv /etc/apt/sources.list.bak /etc/apt/sources.list
                echo "$OS 源已还原。"
            else
                echo "未找到备份文件。"
            fi
            ;;
        Arch)
            if [ -f /etc/pacman.d/mirrorlist.bak ]; then
                mv /etc/pacman.d/mirrorlist.bak /etc/pacman.d/mirrorlist
                echo "$OS 源已还原。"
            else
                echo "未找到备份文件。"
            fi
            ;;
        OpenWrt)
            if [ -f /etc/opkg/customfeeds.conf.bak ]; then
                mv /etc/opkg/customfeeds.conf.bak /etc/opkg/customfeeds.conf
                echo "$OS 源已还原。"
            else
                echo "未找到备份文件。"
            fi
            ;;
    esac
}

# 切换源
switch_source() {
    SOURCE=$1
    echo "正在切换到 $SOURCE 源..."
    
    # 自动备份当前源文件
    auto_backup_sources

    case "$OS" in
        CentOS | Rocky | AlmaLinux)
            case $SOURCE in
                official)
                    cat > /etc/yum.repos.d/CentOS-Base.repo << EOF
# 官方源
[base]
name=$OS-\$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF
                    ;;
                aliyun)
                    cat > /etc/yum.repos.d/CentOS-Base.repo << EOF
# 阿里云源
[base]
name=$OS-\$releasever - Base - 阿里云
baseurl=http://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
EOF
                    ;;
                tencent)
                    cat > /etc/yum.repos.d/CentOS-Base.repo << EOF
# 腾讯云源
[base]
name=$OS-\$releasever - Base - 腾讯云
baseurl=http://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-7
EOF
                    ;;
                huawei)
                    cat > /etc/yum.repos.d/CentOS-Base.repo << EOF
# 华为云源
[base]
name=$OS-\$releasever - Base - 华为云
baseurl=https://repo.huaweicloud.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7
EOF
                    ;;
            esac
            yum clean all
            yum makecache
            ;;
        Ubuntu | Debian | Kali)
            case $SOURCE in
                official)
                    cat > /etc/apt/sources.list << EOF
# 官方源
deb http://archive.ubuntu.com/ubuntu/ $DISTRIB_CODENAME main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $DISTRIB_CODENAME-updates main restricted universe multiverse
EOF
                    ;;
                aliyun)
                    cat > /etc/apt/sources.list << EOF
# 阿里云源
deb http://mirrors.aliyun.com/ubuntu/ $DISTRIB_CODENAME main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $DISTRIB_CODENAME-updates main restricted universe multiverse
EOF
                    ;;
                tencent)
                    cat > /etc/apt/sources.list << EOF
# 腾讯云源
deb http://mirrors.cloud.tencent.com/ubuntu/ $DISTRIB_CODENAME main restricted universe multiverse
deb http://mirrors.cloud.tencent.com/ubuntu/ $DISTRIB_CODENAME-updates main restricted universe multiverse
EOF
                    ;;
                huawei)
                    cat > /etc/apt/sources.list << EOF
# 华为云源
deb https://repo.huaweicloud.com/ubuntu/ $DISTRIB_CODENAME main restricted universe multiverse
deb https://repo.huaweicloud.com/ubuntu/ $DISTRIB_CODENAME-updates main restricted universe multiverse
EOF
                    ;;
            esac
            apt-get clean
            apt-get update
            ;;
        Arch)
            case $SOURCE in
                official)
                    cat > /etc/pacman.d/mirrorlist << EOF
# Arch 官方源
Server = https://mirrors.kernel.org/archlinux/\$repo/os/\$arch
EOF
                    ;;
                aliyun)
                    cat > /etc/pacman.d/mirrorlist << EOF
# 阿里云源
Server = http://mirrors.aliyun.com/archlinux/\$repo/os/\$arch
EOF
                    ;;
                tencent)
                    cat > /etc/pacman.d/mirrorlist << EOF
# 腾讯云源
Server = http://mirrors.cloud.tencent.com/archlinux/\$repo/os/\$arch
EOF
                    ;;
                huawei)
                    cat > /etc/pacman.d/mirrorlist << EOF
# 华为云源
Server = https://repo.huaweicloud.com/archlinux/\$repo/os/\$arch
EOF
                    ;;
            esac
            pacman -Syy
            ;;
        OpenWrt)
            VERSION=$(cat /etc/openwrt_version)
            TARGET=$(cat /etc/openwrt_release | grep DISTRIB_TARGET | cut -d"=" -f2)
            case $SOURCE in
                official)
                    cat > /etc/opkg/customfeeds.conf << EOF
# OpenWrt 官方源
src/gz openwrt_core http://downloads.openwrt.org/releases/\$VERSION/targets/\$TARGET/packages
EOF
                    ;;
                aliyun)
                    cat > /etc/opkg/customfeeds.conf << EOF
# 阿里云源
src/gz openwrt_core http://mirrors.aliyun.com/openwrt/releases/\$VERSION/targets/\$TARGET/packages
EOF
                    ;;
                tencent)
                    cat > /etc/opkg/customfeeds.conf << EOF
# 腾讯云源
src/gz openwrt_core http://mirrors.cloud.tencent.com/openwrt/releases/\$VERSION/targets/\$TARGET/packages
EOF
                    ;;
                huawei)
                    cat > /etc/opkg/customfeeds.conf << EOF
# 华为云源
src/gz openwrt_core https://repo.huaweicloud.com/openwrt/releases/\$VERSION/targets/\$TARGET/packages
EOF
                    ;;
            esac
            opkg update
            ;;
    esac
    echo "$SOURCE 源已切换。"
}

# 更新系统
update_system() {
    echo "正在更新系统..."
    case "$OS" in
        CentOS | Rocky | AlmaLinux)
            yum update -y
            ;;
        Ubuntu | Debian | Kali)
            apt-get upgrade -y
            ;;
        Arch)
            pacman -Syu --noconfirm
            ;;
        OpenWrt)
            opkg upgrade
            ;;
    esac
    echo "系统更新完成。"
}

# 主菜单
main_menu() {
    echo "自动检测到的系统为: $OS"
    echo "请选择要进行的操作："
    echo "1. 备份源"
    echo "2. 切换到官方源"
    echo "3. 切换到阿里源"
    echo "4. 切换到腾讯源"
    echo "5. 切换到华为源"
    echo "6. 还原源"
    echo "7. 更新系统"
    echo "0. 退出"

    read -p "请输入选项: " choice
    case $choice in
        1)
            backup_sources
            ;;
        2)
            switch_source "official"
            ;;
        3)
            switch_source "aliyun"
            ;;
        4)
            switch_source "tencent"
            ;;
        5)
            switch_source "huawei"
            ;;
        6)
            restore_sources
            ;;
        7)
            update_system
            ;;
        0)
            exit 0
            ;;
        *)
            echo "无效选项，请重新选择。"
            ;;
    esac
}

# 开始脚本
detect_system
while true; do
    main_menu
done
