#!/bin/bash

# 获取操作系统的 ID
distro_id=$(grep -w ID /etc/os-release | cut -d= -f2 | tr -d '"')

# 定义 repo 文件路径
repo_file_path="/etc/yum.repos.d/idste-AppStream_dev.repo"

# 生成 repo 文件
echo "Creating repo file at $repo_file_path..."
cat <<EOF | sudo tee "$repo_file_path" > /dev/null
[IDsteAppStream]
name=IDste CEOC Server - AppStream
baseurl=http://mirrors.idste.org/rpm/$distro_id/\$releasever/\$basearch
enabled=1
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ANOLIS
gpgcheck=0

[IDsteCommonStream]
name=IDste CEOC Server - CommonStream
baseurl=http://mirrors.idste.org/rpm/common/\$basearch
enabled=1
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ANOLIS
gpgcheck=0
EOF

echo "Repo file created and cache refreshed."

# 检查生成的文件内容
#echo "Repo file content:"
#cat "$repo_file_path"

# 删掉可能存在nginx
yum remove nginx-* -y
yum remove mosquitto -y
yum remove mariadb-* -y

echo "remove invalid rpm success"

if [[ ${distro_id} != kylin ]]
then
    wget http://mirrors.idste.org/anos8/rpmfusion-free-release-8.noarch.rpm
    yum install -y epel-release
    rpm -Uvh ./rpmfusion-free-release-8.noarch.rpm
fi


echo "Cleaning and updating cache..."
yum clean all && yum makecache

yum install -y supervisor


rm -f idste-vlc-install.sh
wget http://mirrors.idste.org/rpm/idste-vlc-install.sh
bash idste-vlc-install.sh

systemctl stop firewalld.service
systemctl disable firewalld.service

IDSTE_CNF_BACK_DIR_NAME=/etc/idste_bak_not_rm
DB_FLAG_FILE=${IDSTE_CNF_BACK_DIR_NAME}/db.flag
IDSTE_YUM_REPOS=http://mirrors.idste.org/rpm/

dataBases=("mysql" "kingbase")

echo "请选择要使用的数据库类型："
for i in "${!dataBases[@]}"; do
    echo "$((i + 1)).${dataBases[$i]}"
done

count=${#dataBases[@]}
hasDBDriver=0
echo "count =${count}"



if [[ ! -d ${IDSTE_CNF_BACK_DIR_NAME} ]]; then
    mkdir -p ${IDSTE_CNF_BACK_DIR_NAME}
fi

while [ ${hasDBDriver} == 0 ]; do
    read -p "请输入对应的数字(1-${count}): " inputChoice

    echo "您选择的数字是：${inputChoice}"

    # 检查输入是否有效
    if [[ "${inputChoice}" =~ ^[1-9]+$ ]] && [ ${inputChoice} -ge 1 ] && [ ${inputChoice} -le ${count} ]; then
        selectDBDriver="${dataBases[$((inputChoice - 1))]}"
        echo "您选择了数据库：${selectDBDriver}"
        hasDBDriver=1
        # 根据选择执行后续操作（可以根据需要扩展）
        case "${selectDBDriver}" in
            "mysql")
                echo "正在配置 MySQL 数据库..."
                if [[ `rpm -qa | grep mysql-community-server | wc -l` == 0 ]]; then
                    echo "install mysql now"
                    wget ${IDSTE_YUM_REPOS}/common/x86_64/mysql80-community-release-el8-9.noarch.rpm
                    rpm -ivh mysql80-community-release-el8-9.noarch.rpm --force
                    yum install mysql-server -y
                    systemctl start mysqld
                    systemctl enable mysqld
                fi
                ;;
            "kingbase")
                echo "请手动安装 kingbase 数据库..."
                ;;
            *)
                echo "未知的数据库类型！"
                hasDBDriver=0
                ;;
        esac
        echo ${selectDBDriver} > ${DB_FLAG_FILE}
    else
        echo "无效的选择。"
    fi
done


echo "数据库处理完成"


echo "pre install done"

