#!/bin/bash

# 颜色定义
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
RESET='\033[0m'

# 日志函数
log() {
    local level=$1
    local msg=$2
    case $level in
        INFO)    echo -e "${BLUE}[INFO]${RESET} $msg" ;;
        SUCCESS) echo -e "${GREEN}[SUCCESS]${RESET} $msg" ;;
        ERROR)   echo -e "${RED}[ERROR]${RESET} $msg" ;;
        WARNING) echo -e "${YELLOW}[WARNING]${RESET} $msg" ;;
    esac
}

# 检查系统版本，确定使用 yum 还是 dnf
if [ -f /etc/redhat-release ]; then
    release=$(cat /etc/redhat-release)
    if [[ $release == *"CentOS Linux release 8"* ]] || [[ $release == *"CentOS Stream release 8"* ]] || [[ $release == *"CentOS Linux release 9"* ]] || [[ $release == *"CentOS Stream release 9"* ]]; then
        PACKAGE_MANAGER="dnf"
    else
        PACKAGE_MANAGER="yum"
    fi
else
    log ERROR "无法确定系统版本，请手动检查。"
    exit 1
fi

# 更新系统
log INFO "开始更新系统..."
$PACKAGE_MANAGER update -y
if [ $? -eq 0 ]; then
    log SUCCESS "系统更新成功。"
else
    log ERROR "系统更新失败。"
    exit 1
fi

# 定义常用命令对应的软件包列表
packages=(
    wget
    curl
    vim
    net-tools
    telnet
    traceroute
    zip
    unzip
    bzip2
    lsof
    gcc
    gcc-c++
    make
    cmake
    git
    htop
    atop
    iotop
    nmon
    dos2unix
)

# 安装常用命令对应的软件包
log INFO "开始安装常用命令对应的软件包..."
$PACKAGE_MANAGER install -y ${packages[@]}
if [ $? -eq 0 ]; then
    log SUCCESS "常用命令对应的软件包安装成功。"
else
    log ERROR "常用命令对应的软件包安装失败。"
    exit 1
fi

log SUCCESS "所有操作完成。"