#!/bin/bash

function setup_mirror_sources() {
    echo "配置镜像源加速..."
    sudo dnf install -y epel-release
    sudo dnf config-manager --set-enabled powertools
    sudo dnf update -y
    echo "镜像源加速配置完成。"
}

function setup_github_accelerator() {
    echo -n "请输入github加速域名（可以使用国内的镜像服务，如https://gitclone.com）: "
    read github_mirror
    echo "配置github加速..."
    git config --global url."$github_mirror".insteadOf https://github.com
    echo "GitHub加速配置完成。"
}

function enable_security_mode() {
    echo "配置安全模式..."
    sudo systemctl enable firewalld
    sudo systemctl start firewalld
    echo "安全模式配置完成。"
}

function configure_ports() {
    echo -n "请输入要开放的端口号（多个端口用空格分隔）: "
    read ports
    for port in $ports; do
        sudo firewall-cmd --zone=public --add-port=${port}/tcp --permanent
    done
    sudo firewall-cmd --reload
    echo "端口配置完成。"
}

function install_dotnet_sdk() {
    echo "安装dotnet SDK 8.0..."
    sudo dnf install -y https://packages.microsoft.com/config/rhel/8.0/packages-microsoft-prod.rpm
    sudo dnf install -y dotnet-sdk-8.0
    echo "dotnet SDK 8.0 安装完成。"
}

function install_redis() {
    echo "本地安装Redis..."
    sudo dnf install -y redis
    sudo systemctl enable redis
    sudo systemctl start redis
    echo "Redis安装完成。"
}

function install_mysql() {
    echo "本地安装MySQL 8.0..."
    sudo dnf install -y https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
    sudo dnf install -y mysql-community-server
    sudo systemctl enable mysqld
    sudo systemctl start mysqld
    echo "MySQL 8.0 安装完成。"
}

function install_caddy() {
    echo "本地安装Caddy 2..."
    sudo dnf install -y yum-utils
    sudo rpm --import https://caddyserver.com/static/caddy-fingerprint/RPM-GPG-KEY.gpg
    sudo yum-config-manager --add-repo https://caddyserver.com/download/linux/rpm/caddy.repo
    sudo dnf install -y caddy
    sudo systemctl enable caddy
    sudo systemctl start caddy
    echo "Caddy 2 安装完成。"
}

function check_status() {
    echo "系统状态检查..."
    echo "Firewall状态: $(sudo systemctl is-active firewalld)"
    echo "Redis状态: $(sudo systemctl is-active redis)"
    echo "MySQL状态: $(sudo systemctl is-active mysqld)"
    echo "Caddy状态: $(sudo systemctl is-active caddy)"
}

PS3='请选择功能: '
options=("配置镜像源加速" "配置GitHub加速" "配置安全模式" "配置端口" "安装dotnet SDK 8.0" "安装Redis" "安装MySQL 8.0" "安装Caddy 2" "状态检查" "退出")
select opt in "${options[@]}"
do
    case $opt in
        "配置镜像源加速")
            setup_mirror_sources
            ;;
        "配置GitHub加速")
            setup_github_accelerator
            ;;
        "配置安全模式")
            enable_security_mode
            ;;
        "配置端口")
            configure_ports
            ;;
        "安装dotnet SDK 8.0")
            install_dotnet_sdk
            ;;
        "安装Redis")
            install_redis
            ;;
        "安装MySQL 8.0")
            install_mysql
            ;;
        "安装Caddy 2")
            install_caddy
            ;;
        "状态检查")
            check_status
            ;;
        "退出")
            break
            ;;
        *) echo "无效选项 $REPLY";;
    esac
done
