#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

my_cnf=/etc/my.cnf
mysql_dir=/www/server/mysql
#version=$mysql_dir/version.pl
version=$mysql_dir/version_check.pl
mysqld_safe=/www/server/mysql/bin/mysqld_safe
#/www/server/mysql/bin/mysqld

if [ $(whoami) != "root" ]; then
    # echo "Please use the [root] user to execute the aapanel installation script!"
    echo -e "Non-root execute, please try the following solutions: \n   Please switch to [root] user execute"
    exit 1
fi

Get_Pack_Manager() {
    if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
        PM="yum"
    elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
        PM="apt-get"
    fi
}

# yum/apt 安装的
if [ -f "/usr/sbin/mysqld" ] || [ -f "/usr/sbin/mariadbd" ]; then
    Get_Pack_Manager
    if [ "${PM}" = "yum" ]; then
        yum_apt_i=`rpm -qa|grep -iE "mysql-server|mariadb-server" |grep -v "WARNING"`
    elif [ "${PM}" = "apt-get" ]; then
        yum_apt_i=`apt list --installed|grep -iE "mysql-server|mariadb-server" |grep -v "WARNING"`
    fi

    if [ -n "${yum_apt_i}" ]; then
        echo "${yum_apt_i}"
        echo -e "\033[31m There exists mysql installed using "${PM}". \033[0m"
        yum_apt_install=1
    fi


    if [ "${yum_apt_install}" = "1" ]; then
        ## 检查 mysql 是否启动
        if [ "${PM}" = "yum" ]; then
            mysql_run=`ps aux|grep -Ei "mariadbd|mysqld" |grep -v grep|grep "/usr/libexec"`
        elif [ "${PM}" = "apt-get" ]; then
            mysql_run=`ps aux|grep -Ei "mariadbd|mysqld" |grep -v grep|grep "/usr/sbin"`
        fi

        if [ "${mysql_run}" ]; then
            echo "${mysql_run}"
        fi
        if [ -f "/usr/sbin/mariadbd" ]; then
            delete_command="${PM} remove mariadb-server*"
        else
            delete_command="${PM} remove mysql-server*"
        fi
      
        echo -e "\033[31m panel cannot be managed ${PM} installed mysql/mariadb \033[0m"
        echo -e "\033[31m If you do not use it, please delete it. Before this, please do a server snapshot backup.\n Delete command: ${delete_command} \033[0m"
        echo -e "\033[31m rm -f /etc/systemd/system/mysqld.service /etc/systemd/system/mysql.service \033[0m"
        
        exit 1
    fi

fi

# 检查是否是面板的 mysql mariadbd
if [ -d "${mysql_dir}" ] && [ -f "${mysqld_safe}" ]; then

    if [ -f "${my_cnf}" ]; then
        mysql_data_dir=`grep "innodb_data_home_dir" ${my_cnf} | grep -v '#' | awk '{print $3}'`
        echo "mysql data dir: ${mysql_data_dir}"
        mysql_port=`grep "port" ${my_cnf} | grep -v '#' | awk '{print $3}'|tail -1`
        echo "mysql port: ${mysql_port}"
    fi

    if [ -f "${version}" ]; then
        mysql_version=`cat ${version}`
        echo "mysql version: ${mysql_version}"
    fi
    ## 检查 mysql 是否启动
    mysql_run=`ps aux|grep -Ei "mariadbd|mysqld" |grep -v grep|grep "${mysql_dir}"`
    if [ "${mysql_run}" ]; then
        mysqld_safe_run=`ps aux|grep -Ei "mariadbd|mysqld" |grep -v grep|grep "${mysqld_safe}"`
        if [ "${mysqld_safe_run}" ]; then
            echo -e "\033[32m panel mysql is running\033[0m"
        else
            echo -e "\033[31m panel mysqld_safe is not running.\033[0m"
            echo "${mysql_run}"
        fi
    else
        echo -e "\033[31m panel mysql is not running. \033[0m"

        # 检查错误日志
        echo "mysql Error log:"
        if [ "${mysql_data_dir}" ]; then
            logs=`cat ${mysql_data_dir}/*.err|grep -i -C 10 error`
            echo "${logs}"
        else
            echo "MySQL data directory was not obtained"
        fi
        
        
    fi

else
    echo "MySQL is not found, please check if it is installed? Is the installation completed? Is the installation successful?"
fi


# # 检查其他方式安装的 mysql
# othen_install=`ps aux|grep -Ei "mariadbd|mysqld" |grep -v grep|grep -v "${mysql_dir}"|grep -v "/usr/sbin|grep -v "/usr/libexec""
# if [ "${othen_install}" ]; then
#     echo "${othen_install}"
# fi



Command_Exists() {
    command -v "$@" >/dev/null 2>&1
}

GetSysInfo() {
    if [ -s "/etc/redhat-release" ];then
        SYS_VERSION=$(cat /etc/redhat-release)
    elif Command_Exists hostnamectl; then
        SYS_VERSION=$(hostnamectl | grep "Operating System" | awk -F":" '{print $2}')
    elif [ -s "/etc/issue" ]; then
        SYS_VERSION=$(cat /etc/issue | tr '\n' ' ')
    fi
    SYS_INFO=$(uname -a)
    SYS_BIT=$(getconf LONG_BIT)
    MEM_TOTAL=$(free -m | grep Mem | awk '{print $2}')
    CPU_INFO=$(getconf _NPROCESSORS_ONLN)

    echo -e ${SYS_VERSION}
    echo -e Bit:${SYS_BIT} Mem:${MEM_TOTAL}M Core:${CPU_INFO}
    echo -e ${SYS_INFO}
    # echo -e "Please screenshot above error message and post forum forum.aapanel.com or send email: kern@aapanel.com for help"
}

# GetSysInfo