#!/bin/bash
#***************************************************************#
# ScriptName: myshellinitv7.0.sh
# Author: xhy
# Create Date: 2025-02-16 15:24
# Modify Date: 2025-02-16 15:24
# Version: v2
# Description: CentOS 7/8 Post-Installation Script (Optimized)
#***************************************************************#
set -euo pipefail
trap 'echo -e "\n\033[35m[ERROR] $BASH_COMMAND failed at line $LINENO\033[0m"; exit 1' ERR

# ------------------------------
# 全局配置区（需根据实际情况修改）
# ------------------------------
declare -A CONFIG=(
    # 基础配置
    [MIRROR_SOURCE]="China"                  # 镜像源 China/Abroad
    [TIMEZONE]="Asia/Shanghai"               # 时区设置
    
    # 网络配置
    [HOSTNAME]="localhost.localdomain"       # 主机名
    [IP_ADDR]="172.24.8.10"                  # IP地址
    [NETMASK]="24"                           # 子网掩码
    [GATEWAY]="172.24.8.2"                   # 默认网关
    [MTU]="1500"                             # MTU值
    [NET_IFACE]="eth0"                       # 物理网卡名
    [DNS]="223.5.5.5 223.6.6.6"              # DNS
    [LANG]="en_US.UTF-8"                     # 系统语言
   
    # 功能开关
    [ENABLE_KERNEL_UPDATE]="yes"             # 是否升级内核
    [ENABLE_OPENSSL_UPDATE]="yes"            # 是否升级OpenSSL
    [ENABLE_PYTHON_UPDATE]="yes"             # 是否升级Python
    [ENABLE_SOFTWARE_INSTALL]="yes"          # 是否安装附加软件
    [ENABLE_REBOOT]="yes"                    # 是否最后重启
    [OPENSSL_VERSION]="3.4.1"                # 可选项: 1.1.1w / 3.4.1
    [PYTHON_VERSION]="3.13.2"                # Python目标版本
)

# ------------------------------
# 环境系统版本检测
# ------------------------------
check_os_release() {
    echo -e "\n\n\033[33m[INFO] 检查当前系统版本...\033[0m"
    sleep 2

    # 系统平台检测
    if [[ -f /etc/redhat-release ]]; then
        PLATFORM=$(rpm -E %{rhel} 2>/dev/null || rpm -q --qf '%{VERSION}' -f /etc/redhat-release)
    else
        echo -e "\n\n\033[35m[ERROR] 仅支持 RedHat-based 系统...\033[0m"
        exit 1
    fi

    # 创建必要目录
    mkdir -p ~/.pip
    mkdir -p /etc/yum.repos.d/backup
    mkdir -p /mnt/cdrom /media/cdrom /tmp/packages
    
    echo -e "\n\033[32m[SUCC] 当前系统版本为：CentOS ${PLATFORM}...\033[0m"
    sleep 2
}

# ------------------------------
# 修改网卡名称
# ------------------------------
configure_netname() {
    echo -e "\n\n\033[33m[INFO] 修改网卡名称为ethX...\033[0m"
    sleep 2

    # 清理旧规则
    rm -f /etc/udev/rules.d/*-persistent-*.rules
    
    # 修改GRUB配置
    cp -brpf /etc/default/grub{,.bak}
    sed -i 's/GRUB_CMDLINE_LINUX=\"/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0 /g' /etc/default/grub
    # 应用新配置
    grub2-mkconfig -o /boot/grub2/grub.cfg >/dev/null
    
    echo -e "\n\033[32m[SUCC] 网卡名称已修改为eth0...\033[0m"
    sleep 2
}

# ------------------------------
# 基础配置
# ------------------------------
configure_basic() {
    echo -e "\n\n\033[33m[INFO] 配置主机名称和时区...\033[0m"
    sleep 2
    
    # 设置主机名
    hostnamectl set-hostname "${CONFIG[HOSTNAME]}"

    # 时区与语言
    timedatectl set-timezone "${CONFIG[TIMEZONE]}"
    localectl set-locale LANG="${CONFIG[LANG]}"
    echo "LANG=${CONFIG[LANG]}" > /etc/locale.conf

    # 禁用终端滴滴
    echo 'set bell-style none' >> /etc/inputrc

    echo -e "\n\033[32m[SUCC] 主机名称和时区已配置完成...\033[0m"
    sleep 2
}

# ------------------------------
# 网络配置
# ------------------------------
configure_network() {
    echo -e "\n\n\033[33m[INFO] 配置静态网络信息，如IP地址...\033[0m"
    sleep 2

    # 备份默认网卡配置
    DEFAULT_IFACE=$(ls /sys/class/net | grep -E '^enp|^ens|^eth' | head -1)
    mv -f "/etc/sysconfig/network-scripts/ifcfg-${DEFAULT_IFACE}"{,.bak} || true

    # 生成新配置
    nmcli connection add \
        connection.autoconnect yes \
        autoconnect yes \
        ipv4.method manual \
        con-name "${CONFIG[NET_IFACE]}" \
        ifname "${CONFIG[NET_IFACE]}" \
        ipv4.addresses "${CONFIG[IP_ADDR]}/${CONFIG[NETMASK]}" \
        ipv4.dns "${CONFIG[DNS]}" \
        type ethernet \
        mtu "${CONFIG[MTU]}" \
        ipv4.gateway "${CONFIG[GATEWAY]}"

    echo -e "\n\033[32m[SUCC] 静态网络信息，如IP地址等已配置完成...\033[0m"
    sleep 2
}

# ------------------------------
# 安全配置
# ------------------------------
configure_security() {
    echo -e "\n\n\033[33m[INFO] 配置安全信息，如关闭SELinux和防火墙...\033[0m"
    sleep 2
    
    # 关闭SELinux
    cp -brpf /etc/selinux/config{,.bak}
    sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
    setenforce 0 || true

    # 禁用防火墙
    systemctl disable --now firewalld || true

    # SSH优化
    sed -i '/^#*ClientAliveInterval/cClientAliveInterval 300' /etc/ssh/sshd_config
    sed -i 's/^UseDNS.*/UseDNS no/' /etc/ssh/sshd_config
    sed -i 's/^#UseDNS.*/UseDNS no/' /etc/ssh/sshd_config
    systemctl restart sshd

    echo -e "\n\033[32m[SUCC] 安全信息，如关闭SELinux和防火墙已配置完成...\033[0m"
    sleep 2
}

# ------------------------------
# 软件源配置
# ------------------------------
configure_repos() {
    echo -e "\n\n\033[33m[INFO] 配置软件仓库源...\033[0m"
    sleep 2

    # 备份原仓库
    mkdir -p /etc/yum.repos.d/backup
    mv -f /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/ 2>/dev/null || true

    # 安装wget工具
    case "${CONFIG[MIRROR_SOURCE]}" in
        China)
            curl -o /etc/yum.repos.d/CentOS-${PLATFORM}-Tmp-Base-Tencent.repo http://down.linuxsb.com/myoptions/myshellinit/repo/centos/CentOS-${PLATFORM}-Tmp-Base-Tencent.repo
             ;;
        Abroad)
            curl -o /etc/yum.repos.d/CentOS-${PLATFORM}-Tmp-Base-Org.repo http://down.linuxsb.com/myoptions/myshellinit/repo/centos/CentOS-${PLATFORM}-Tmp-Base-Org.repo
            ;;
        *) 
            echo -e "\n\n\033[34m[WARN] 使用默认国内源...\033[0m"
            curl -o /etc/yum.repos.d/CentOS-${PLATFORM}-Tmp-Base-Tencent.repo http://down.linuxsb.com/myoptions/myshellinit/repo/centos/CentOS-${PLATFORM}-Tmp-Base-Tencent.repo
            sleep 1
            return ;;
    esac

    yum -y install wget
    yum -y update
    rm -rf /etc/yum.repos.d/CentOS-${PLATFORM}-Tmp-*.repo

    # 仓库配置
    case "${CONFIG[MIRROR_SOURCE]}" in
        China)
            repos=(
                "http://down.linuxsb.com/myoptions/myshellinit/repo/centos/CentOS-${PLATFORM}-Base-Aliyun-Ustc-In.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-aliyun-ustc.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-aliyun-ustc-testing.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-aliyun-ustc-modular.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-aliyun-ustc-playground.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-aliyun-ustc-testing-modular.repo"
            ) ;;
        Abroad)
            repos=(
                "http://down.linuxsb.com/myoptions/myshellinit/repo/centos/CentOS-${PLATFORM}-Base-Rackspace-Org-Out.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-rit-rackspace.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-rit-rackspace-testing.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-rit-rackspace-modular.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-rit-rackspace-playground.repo"
                "http://down.linuxsb.com/myoptions/myshellinit/repo/epel/epel${PLATFORM}-rit-rackspace-testing-modular.repo"
            ) ;;
        *) 
            echo -e "\n\n\033[34m[WARN] 使用默认仓库源...\033[0m"
            sleep 1
            return ;;
    esac

    # 下载仓库文件
    for url in "${repos[@]}"; do
        wget -P /etc/yum.repos.d/ "$url" || true
    done

    find /etc/yum.repos.d/ -maxdepth 1 -type f \
        ! -name "*${PLATFORM}*" \
        ! -name "backup" \
        -exec mv -t /etc/yum.repos.d/backup/ {} +

    # 导入GPG密钥
    rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    yum clean all
    yum makecache

    echo -e "\n\033[32m[SUCC] 软件仓库源已配置完成...\033[0m"
    sleep 2
}

# ------------------------------
# 内核升级
# ------------------------------
upgrade_kernel() {
    [[ "${CONFIG[ENABLE_KERNEL_UPDATE]}" != "yes" ]] && return
    
    echo -e "\n\n\033[33m[INFO] 升级内核版本...\033[0m"
    sleep 2

    find /etc/yum.repos.d/ -maxdepth 1 -type f \
        ! -name "*${PLATFORM}*" \
        ! -name "backup" \
        -exec mv -t /etc/yum.repos.d/backup/ {} +
   
    # 配置镜像源
    local KERNELVERSION=6.9.7-1
    local BASE_URL="https://mirrors.coreix.net/elrepo-archive-archive/kernel/el7/x86_64/RPMS"
    local PKG_LIST=(
        kernel-ml-${KERNELVERSION}.el${PLATFORM}.elrepo.x86_64.rpm
        kernel-ml-devel-${KERNELVERSION}.el${PLATFORM}.elrepo.x86_64.rpm
        kernel-ml-doc-${KERNELVERSION}.el${PLATFORM}.elrepo.x86_64.rpm
        kernel-ml-headers-${KERNELVERSION}.el${PLATFORM}.elrepo.x86_64.rpm
        kernel-ml-tools-${KERNELVERSION}.el${PLATFORM}.elrepo.x86_64.rpm
        kernel-ml-tools-libs-${KERNELVERSION}.el${PLATFORM}.elrepo.x86_64.rpm
    )

    rpm --import https://www.elrepo.org/RPM-GPG-KEY-v2-elrepo.org 2>/dev/null || :

    case "${CONFIG[MIRROR_SOURCE]}" in
        China|Abroad)
            if (( PLATFORM == 8 )); then
    # 安装ELRepo
                echo -e "\n\n\033[33m[INFO] 安装ELRepo...\033[0m"
                yum -y install "https://www.elrepo.org/elrepo-release-${PLATFORM}.el${PLATFORM}.elrepo.noarch.rpm" || true
                mv -b /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/backup || true
                local REPO_SUFFIX=$([[ "${CONFIG[MIRROR_SOURCE]}" == "China" ]] && echo "aliyun-ustc" || echo "rit-rackspace")
                wget -q -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/myshellinit/repo/epel/elrepo${PLATFORM}-${REPO_SUFFIX}.repo
                rpm --import https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official 2>/dev/null || :

                echo -e "\n\n\033[33m[INFO] 开始内核升级操作...\033[0m"
                sleep 1
                yum --disablerepo="*" --enablerepo="elrepo-kernel" install -y kernel-ml kernel-ml-headers kernel-ml-devel kernel-ml-tools kernel-ml-tools-libs --allowerasing
            else
                for pkg in "${PKG_LIST[@]}"; do
                    rpm -ivh --nodeps --force "${BASE_URL}/${pkg}" 2>/dev/null || :
                done
            fi  
            ;;
        *)  
            echo -e "\n\n\033[34m[WARN] 不支持内核升级...\033[0m"
            sleep 1
            ;;
    esac

    # 配置新内核
    grub2-set-default 0

    echo -e "\n\033[32m[SUCC] 内核升级已完成...\033[0m"
    sleep 2
}

# ------------------------------
# 基础软件安装
# ------------------------------
install_base_tools() {
    echo -e "\n\n\033[33m[INFO] 安装基础常用软件...\033[0m"
    sleep 2
    
    rpm --import https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-${PLATFORM}

    local BASE_PKGS=(
        bash-completion bind-utils bzip2 curl epel-release gcc gcc-c++ git htop
        jq libffi-devel lrzsz lsof lvm2 make net-tools openssh-clients
        openssl openssl-devel perl-IPC-Cmd perl-CPAN perl-interpreter perl-Pod-Html python3-pip screen tar
        tk-devel traceroute tree unzip vim wget xz yum-utils zlib zlib-devel
    )

    if (( PLATFORM == 7 )); then
        yum -y install "${BASE_PKGS[@]}" ntp
        cp -brpf /etc/ntp.conf{,.bak}
#        yum -y update
        curl -o /etc/ntp.conf http://down.linuxsb.com/myoptions/myshellinit/Aliyunntp.conf
        systemctl enable --now ntpd
    else
        dnf -y install "${BASE_PKGS[@]}" chrony
#        dnf -y update
        cp -brpf /etc/chrony.conf{,.bak}
        curl -o /etc/chrony.conf http://down.linuxsb.com/myoptions/myshellinit/Aliyunchrony.conf
        systemctl enable --now chronyd
    fi
    echo -e "\n\033[32m[SUCC] 基础常用软件已安装完成...\033[0m"
    sleep 2
}

# ------------------------------
# 附加工具安装,fping tcping
# ------------------------------
install_extra_tools() {
    echo -e "\n\n\033[33m[INFO] 安装额外扩展软件...\033[0m"
    sleep 2
    
    # tcping安装
    local TCPING_DIR="/usr/local/tcping"
    mkdir -p "${TCPING_DIR}/sbin"
    curl -sL http://down.linuxsb.com/software/tcping-1.3.5-4-x86_64.tar.gz | tar -zx -C /tmp
    mv -f /tmp/usr/bin/tcping "${TCPING_DIR}/sbin/" || true

    # fping编译安装
    curl -sL http://down.linuxsb.com/software/fping-5.0.tar.gz | tar -zx -C /tmp
    (cd /tmp/fping-5.0 && ./configure --prefix=/usr/local/fping && make -j$(nproc) install)

    # 环境变量配置
    echo "export PATH=\$PATH:/usr/local/tcping/sbin:/usr/local/fping/sbin" > /etc/profile.d/custom_path.sh
	
    echo -e "\n\033[32m[SUCC] 额外扩展软件已安装完成...\033[0m"
    sleep 2
}

# ------------------------------
# 开发环境配置模块
# ------------------------------
configure_dev_env() {
    echo -e "\n\n\033[33m[INFO] 配置系统Bash和Vim环境...\033[0m"
    sleep 2
    
    # Bash配置
    curl -s http://down.linuxsb.com/myoptions/myshellinit/my_Bashrc_Diy01_v1 > /etc/profile.d/custom_bash.sh

    # Vim配置
    curl -s http://down.linuxsb.com/myoptions/myshellinit/my_Vimrc_Diy01_v1 > /etc/skel/.vimrc
    curl -s http://down.linuxsb.com/myoptions/myshellinit/my_Vimrc_Diy01_v1 > ${HOME}/.vimrc

    echo -e "\n\033[32m[SUCC] 系统Bash和Vim环境配置完成...\033[0m"
    sleep 2
}

# ------------------------------
# 核心组件升级模块（含OpenSSL）
# ------------------------------
upgrade_openssl_components() {
    [[ "${CONFIG[ENABLE_OPENSSL_UPDATE]}" == "yes" ]] && upgrade_openssl
}

upgrade_openssl() {
    echo -e "\n\n\033[33m[INFO] 开始升级OpenSSL至版本: ${CONFIG[OPENSSL_VERSION]}\033[0m"
    sleep 2

    WORKDIR="/tmp/openssl"
    INSTALL_PREFIX="/usr/local/openssl"
    mkdir -p "${WORKDIR}" || true
    cd "${WORKDIR}"
    
    echo -e "\n\n\033[33m[INFO] 备份源文件...\033[0m"
    mv -f /usr/bin/openssl /usr/bin/openssl.bak || true
    mv -f /usr/include/openssl/ /usr/include/openssl.bak || true

    echo -e "\n\n\033[33m[INFO] 下载OpenSSL源码...\033[0m"

    case "${CONFIG[MIRROR_SOURCE]}" in
        China)
            curl -sL http://down.linuxsb.com/software/openssl-${CONFIG[OPENSSL_VERSION]}.tar.gz | tar -zx
             ;;
        Abroad)
            curl -sL https://github.com/openssl/openssl/releases/download/openssl-${CONFIG[OPENSSL_VERSION]}/openssl-${CONFIG[OPENSSL_VERSION]}.tar.gz | tar -zx
            ;;
        *) 
            echo -e "\n\n\033[34m[WARN] 使用默认国内源...\033[0m"
            curl -sL http://down.linuxsb.com/software/openssl-${CONFIG[OPENSSL_VERSION]}.tar.gz | tar -zx
            sleep 1
            return ;;
    esac

    echo -e "\n\n\033[33m[INFO] 开始编译OpenSSL ${CONFIG[OPENSSL_VERSION]}...\033[0m"
    cd openssl-${CONFIG[OPENSSL_VERSION]} || exit 1
    ./config --prefix="${INSTALL_PREFIX}" --shared
    make -j$(nproc) && make install

    MAJOR_VERSION=$(echo "${CONFIG[OPENSSL_VERSION]}" | cut -d. -f1)
    if [[ "${MAJOR_VERSION}" == "1" ]]; then
        LIB_SUFFIX="1.1"
    else
        LIB_SUFFIX="3"
    fi

    sleep 1
    ln -sf "${INSTALL_PREFIX}/bin/openssl" "/usr/bin/openssl"
    ln -sf "${INSTALL_PREFIX}/include/openssl" "/usr/include/openssl"
    sleep 1
    ln -sf "${INSTALL_PREFIX}/lib64/libssl.so.${LIB_SUFFIX}" "/usr/lib64/libssl.so"
    ln -sf "${INSTALL_PREFIX}/lib64/libssl.so.${LIB_SUFFIX}" "/usr/lib64/libssl.so.${LIB_SUFFIX}"
    sleep 1
    ln -sf "${INSTALL_PREFIX}/lib64/libcrypto.so.${LIB_SUFFIX}" "/usr/lib64/libcrypto.so"
    ln -sf "${INSTALL_PREFIX}/lib64/libcrypto.so.${LIB_SUFFIX}" "/usr/lib64/libcrypto.so.${LIB_SUFFIX}"

    echo "${INSTALL_PREFIX}/lib64" > /etc/ld.so.conf.d/openssl.conf
    ldconfig

    echo -e "\033[32m[SUCC] OpenSSL升级完成，验证信息：\033[0m"
    openssl version -a
    ldd  /usr/bin/openssl
    sleep 2
}

upgrade_python_components() {
    [[ "${CONFIG[ENABLE_PYTHON_UPDATE]}" == "yes" ]] && upgrade_python
}

upgrade_python() {
    PYTHON_DIR=/usr/local/python3
 
    echo -e "\n\n\033[33m[INFO] 开始下载 Python ${CONFIG[PYTHON_VERSION]}...\033[0m"
    sleep 2

    find /etc/yum.repos.d/ -maxdepth 1 -type f \
        ! -name "*${PLATFORM}*" \
        ! -name "backup" \
        -exec mv -t /etc/yum.repos.d/backup/ {} +

    cd /tmp

    case "${CONFIG[MIRROR_SOURCE]}" in
        China)
            curl -o /tmp/Python-${CONFIG[PYTHON_VERSION]}.tar.xz https://mirrors.huaweicloud.com/python/${CONFIG[PYTHON_VERSION]}/Python-${CONFIG[PYTHON_VERSION]}.tar.xz
             ;;
        Abroad)
            curl -o /tmp/Python-${CONFIG[PYTHON_VERSION]}.tar.xz https://www.python.org/ftp/python/${CONFIG[PYTHON_VERSION]}/Python-${CONFIG[PYTHON_VERSION]}.tar.xz
            ;;
        *) 
            echo -e "\n\n\033[34m[WARN] 使用默认国内站点...\033[0m"
            curl -o /tmp/Python-${CONFIG[PYTHON_VERSION]}.tar.xz https://mirrors.huaweicloud.com/python/${CONFIG[PYTHON_VERSION]}/Python-${CONFIG[PYTHON_VERSION]}.tar.xz
            sleep 1
            return ;;
    esac

    tar -Jxvf Python-${CONFIG[PYTHON_VERSION]}.tar.xz
    cd Python-${CONFIG[PYTHON_VERSION]}/

    # CentOS7 特殊处理
    if (( PLATFORM == 7 )); then
        wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/myshellinit/repo/sclo/CentOS-7-SCLo-scl.repo
        wget -P /etc/yum.repos.d/ http://down.linuxsb.com/myoptions/myshellinit/repo/sclo/CentOS-7-SCLo-scl-rh.repo
        rpm --import http://down.linuxsb.com/myoptions/myshellinit/repo/sclo/RPM-GPG-KEY-CentOS-SIG-SCLo
        yum -y install devtoolset-11-gcc*

        mv -f /usr/bin/gcc /usr/bin/gcc-4.bak || true
        ln -sf /opt/rh/devtoolset-11/root/bin/gcc /usr/bin/gcc 
        echo -e "\n\n\033[32m[SUCCESS] 当前Gcc版本已完成升级...\033[0m"
        sleep 1
    else
        echo -e "\n\n\033[33m[INFO] 当前Gcc版本无需升级...\033[0m"
        sleep 1
    fi

    echo -e "\n\n\033[33m[INFO] 开始编译安装Python ${CONFIG[PYTHON_VERSION]}...\033[0m"
    sleep 2

    ./configure --prefix=${PYTHON_DIR} --with-openssl=/usr/local/openssl --enable-optimizations --enable-shared --with-ssl
    make -j$(nproc) && make install
    echo "${PYTHON_DIR}/lib/" > /etc/ld.so.conf.d/python.conf
    echo "export PATH=\$PATH:${PYTHON_DIR}/bin/" >> /etc/profile.d/custom_path.sh
    
    ldconfig

    echo -e "\n\n\033[33m[INFO] 配置 Python ${CONFIG[PYTHON_VERSION]}库文件路径...\033[0m"
    sleep 1

    PYTHON_VERSION_NEW=`echo ${CONFIG[PYTHON_VERSION]} | awk -F '.' '{ print $1"."$2 }'`
    ln -sf /usr/local/python3/bin/pip${PYTHON_VERSION_NEW} /usr/bin/pip-${PYTHON_VERSION_NEW} 

    cp -brpf /usr/local/python3/bin/{idle,pip,pydoc,python}${PYTHON_VERSION_NEW} /usr/bin/
    cp -brpf /usr/local/python3/bin/python${PYTHON_VERSION_NEW}-config /usr/bin/

    alternatives --install /usr/bin/python3 python3 /usr/bin/python${PYTHON_VERSION_NEW} 4000000 \
      --slave /usr/bin/pip3 pip3 /usr/bin/pip${PYTHON_VERSION_NEW}  \
      --slave /usr/bin/pip-3 pip-3 /usr/bin/pip-${PYTHON_VERSION_NEW} \
      --slave /usr/bin/easy_install-3 easy_install-3 /usr/bin/easy_install-3.6 \
      --slave /usr/bin/pydoc3 pydoc3 /usr/bin/pydoc${PYTHON_VERSION_NEW}  \
      --slave /usr/bin/pydoc-3 pydoc-3 /usr/bin/pydoc${PYTHON_VERSION_NEW}  \
      --slave /usr/bin/python3-config python3-config /usr/bin/python${PYTHON_VERSION_NEW}-config \
      --slave /usr/bin/pyvenv-3 pyvenv-3 /usr/bin/pyvenv-3.6

    alternatives --auto python3
    ldconfig
 
    echo -e "\n\n\033[33m[INFO] 配置 PIP ${CONFIG[PYTHON_VERSION]} 国内仓库源...\033[0m"
    sleep 1
    case "${CONFIG[MIRROR_SOURCE]}" in
        Abroad)
            cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://pypi.org/simple/
EOF
            ;;
        *) 
            cat > ~/.pip/pip.conf <<'EOF'
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
EOF
           ;;
    esac

    pip3 install --upgrade pip setuptools

    echo -e "\n\033[32m[SUCC] 验证 Python ${CONFIG[PYTHON_VERSION]}当前版本...\033[0m"
    alternatives --display python3
    python3 --version
    sleep 2

    echo -e "\n\n\033[32m[SUCC] 当前 PIP 仓库源为：\033[0m"
    cat ~/.pip/pip.conf
    sleep 2
}

# ------------------------------
# 重启
# ------------------------------
rebootos() {
    if [[ "${CONFIG[ENABLE_REBOOT]}" == "yes" ]]; then
        curl -o /root/clean.sh http://down.linuxsb.com/myoptions/clean.sh
        echo -e "\n\n\033[34m[WARN] 操作系统将在1分钟后重启...\033[0m"
        shutdown -r +1
        find /etc/yum.repos.d/ -maxdepth 1 -type f \
            ! -name "*${PLATFORM}*" \
            ! -name "backup" \
            -exec mv -t /etc/yum.repos.d/backup/ {} +
        nmcli connection reload
        chmod u+x /root/clean.sh || true
    else
        echo -e "\n\n\033[34m[WARN] 操作已完成，建议重启系统...\033[0m"
        curl -o /root/clean.sh http://down.linuxsb.com/myoptions/clean.sh
		chmod u+x /root/clean.sh || true
    fi
}

# ------------------------------
# 主执行流程
# ------------------------------
main() {
    check_os_release   
    configure_netname
    configure_basic
    configure_network
    configure_security
    configure_repos
    install_base_tools
    install_extra_tools
    upgrade_kernel
    configure_dev_env
    upgrade_openssl_components
    upgrade_python_components
    rebootos
}

main "$@"
