#!/bin/bash

# 定义颜色代码
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # 无颜色

# 日志记录函数
log() {
    local level=$1
    local message=$2
    local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
    case $level in
        "INFO")
            echo -e "${GREEN}[INFO] $timestamp - $message${NC}"
            ;;
        "ERROR")
            echo -e "${RED}[ERROR] $timestamp - $message${NC}"
            ;;
        *)
            echo -e "[UNKNOWN] $timestamp - $message"
            ;;
    esac
}

# 安装包函数
install_package() {
    local package=$1
    log "INFO" "安装 $package..."
    if yum install $package -y; then
        log "INFO" "$package 安装成功！"
    else
        log "ERROR" "$package 安装失败！错误信息：$?"
        exit 1
    fi
}

# 下载文件函数
download_file() {
    local url=$1
    local filename=$(basename $url)
    log "INFO" "下载 $filename 源码..."
    if wget --inet4-only $url; then
        log "INFO" "$filename 源码下载成功！"
    else
        log "ERROR" "$filename 源码下载失败！错误信息：$?"
        exit 1
    fi
}

# 解压文件函数
extract_file() {
    local filename=$1
    log "INFO" "解压 $filename 源码..."
    if tar -xvf $filename; then
        log "INFO" "$filename 源码解压成功！"
    else
        log "ERROR" "$filename 源码解压失败！错误信息：$?"
        exit 1
    fi
}

# 编译安装 Python 函数
compile_and_install_python() {
    local dir=$1
    log "INFO" "进入 $dir 目录并开始编译..."
    if cd $dir; then
        if ./configure; then
            if make all; then
                if make install; then
                    log "INFO" "Python 3.9.5 编译安装成功！"
                else
                    log "ERROR" "Python 3.9.5 安装步骤失败！错误信息：$?"
                    exit 1
                fi
            else
                log "ERROR" "Python 3.9.5 编译 all 步骤失败！错误信息：$?"
                exit 1
            fi
        else
            log "ERROR" "Python 3.9.5 配置步骤失败！错误信息：$?"
            exit 1
        fi
        cd ..
    else
        log "ERROR" "进入 $dir 目录失败！错误信息：$?"
        exit 1
    fi
}

# 测试 Python 安装函数
test_python_install() {
    log "INFO" "测试 Python 安装..."
    if python3 --version; then
        log "INFO" "Python 安装测试成功！"
    else
        log "ERROR" "Python 安装测试失败！错误信息：$?"
        exit 1
    fi
}

# 配置 pip 镜像源函数
configure_pip_mirror() {
    log "INFO" "配置 pip 使用阿里云镜像源..."
    if pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/; then
        log "INFO" "pip 镜像源配置成功！"
    else
        log "ERROR" "pip 镜像源配置失败！错误信息：$?"
        exit 1
    fi
}

# 删除文件和目录函数
delete_files_and_dirs() {
    local files_and_dirs=("$@")
    log "INFO" "删除无用的文件和目录..."
    if rm -rf "${files_and_dirs[@]}"; then
        log "INFO" "文件和目录删除成功！"
    else
        log "ERROR" "文件和目录删除失败！错误信息：$?"
        exit 1
    fi
}

# 记录当前目录
pushd .

# 1. 安装 gcc
install_package "gcc"

# 2. 安装 Python 依赖
dependencies=(
    "zlib zlib-devel"
    "bzip2 bzip2-devel"
    "ncurses ncurses-devel"
    "readline readline-devel"
    "openssl openssl-devel"
    "xz lzma xz-devel"
    "sqlite sqlite-devel"
    "gdbm gdbm-devel"
    "tk tk-devel"
    "mysql-devel"
    "python-devel"
    "libffi-devel"
    "mariadb-devel"
)
for dep in "${dependencies[@]}"; do
    install_package "$dep"
done

# 3. 前置批量安装
pre_installs=(
    "wget"
    "tar"
    "make"
    "git"
    "epel-release"
    "zbar-devel"
)
for pre in "${pre_installs[@]}"; do
    install_package "$pre"
done

# 4. 下载 Python 源码
download_file "http://dw.chan.ink/Python/Python-3.9.5.tgz"

# 5. 解压源码包
extract_file "Python-3.9.5.tgz"

# 6. 编译安装 Python
compile_and_install_python "Python-3.9.5"

# 7. 测试 Python 安装
test_python_install

# 8. 配置 pip 使用阿里云镜像源
configure_pip_mirror

# 删除无用的压缩包和解压目录
delete_files_and_dirs "Python-3.9.5.tgz" "Python-3.9.5"

# 回到初始目录
popd

# 完成
log "INFO" "Python 3.9.5 安装流程结束！"    