#!/bin/bash

# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'  # No Color

# 确保脚本以root权限运行
check_root() {
  if [ "$(id -u)" != "0" ]; then
    echo -e "${RED}此脚本需要root权限${NC}" 1>&2
    exit 1
  fi
}

# 处理 yum 源
change_sources() {
  read -p "是否要更换源? (y/n): " choice
  if [[ "$choice" == "y" ]]; then
    echo -e "${YELLOW}正在更换源...${NC}"
    sed -e 's|^mirrorlist=|#mirrorlist=|g' \
        -e 's|^# baseurl=https://repo.almalinux.org|baseurl=https://mirrors.aliyun.com|g' \
        -i.bak \
        /etc/yum.repos.d/almalinux*.repo
    echo -e "${GREEN}源更换完成！${NC}"
  else
    echo -e "${GREEN}源保持不变。${NC}"
  fi
}

# 更新软件源并安装常用工具
install_tools() {
  echo -e "${YELLOW}正在更新软件包并安装常用工具...${NC}"
  dnf update -y && dnf upgrade -y
  dnf install tar vim wget git bind-utils unzip -y
  echo -e "${GREEN}软件包更新并安装完成！${NC}"
}

# 安装 uv
install_uv() {
  read -p "是否安装 uv? (y/n): " choice
  if [[ "$choice" == "y" ]]; then
    echo -e "${YELLOW}正在安装 uv...${NC}"
    curl -LsSf https://astral.sh/uv/install.sh | sh
    echo -e "${GREEN}uv 安装完成！${NC}"
  else
    echo -e "${GREEN}跳过 uv 安装。${NC}"
  fi
}

# 关闭 SELinux
disable_selinux() {
  echo -e "${YELLOW}正在关闭 SELinux...${NC}"
  sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
  echo -e "${GREEN}SELinux 关闭完成！${NC}"
}

# 关闭防火墙
disable_firewall() {
  echo -e "${YELLOW}正在关闭防火墙...${NC}"
  systemctl disable --now firewalld
  echo -e "${GREEN}防火墙关闭完成！${NC}"
}

# 主函数
main() {
  check_root
  change_sources
  install_tools
  install_uv
  disable_selinux
  disable_firewall
  echo -e "${GREEN}初始化完成！请重启系统使更改生效。${NC}"
}

# 执行主函数
main


