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

public_file=/www/server/panel/install/public.sh
. $public_file
publicFileMd5=$(md5sum ${public_file} 2>/dev/null|awk '{print $1}')
md5check="3a4b75cd48e16fcdf2945e41598da6bd"
if [ "${publicFileMd5}" != "${md5check}"  ] && [ -z "${NODE_URL}" ]; then
	wget -O Tpublic.sh https://download.bt.cn/install/public.sh -T 20;
	publicFileMd5=$(md5sum Tpublic.sh 2>/dev/null|awk '{print $1}')
	if [ "${publicFileMd5}" == "${md5check}"  ]; then
		\cp -rpa Tpublic.sh $public_file
	fi
	rm -f Tpublic.sh
	. $public_file
fi

download_Url=$NODE_URL

run_path="/root"
Is_64bit=$(getconf LONG_BIT)
Setup_Path="/www/server/mysql"
Data_Path="/www/server/data"
sysType=$(uname -a|grep x86_64)


actionType=$1
version=$2

Get_Sys_Version(){
	if [ -f "/etc/os-release" ];then
		. /etc/os-release
		OS_V=${VERSION_ID%%.*}
		if [ "${ID}" == "opencloudos" ] && [[ "${OS_V}" =~ ^(9)$ ]];then
			OS_NAME=${ID}
		elif { [ "${ID}" == "almalinux" ] || [ "${ID}" == "centos" ] || [ "${ID}" == "rocky" ]; } && [[ "${OS_V}" =~ ^(9)$ ]]; then
            OS_NAME="9"
# 		elif [ "${ID}" == "alinux" ] && [[ "${OS_V}" =~ ^(3)$ ]];then
# 		    OS_NAME=${ID}
# 		elif [ "${ID}" == "hce" ] && [[ "${OS_V}" =~ ^(2)$ ]];then
# 		    OS_NAME=${ID}
		fi
	fi
    
    X86_CHECK=$(uname -m|grep x86_64)

    if [ "${OS_NAME}" ] && [ "${X86_CHECK}" ];then
        wget -O mysql.sh ${download_Url}/install/4/mysql_new.sh && sh mysql.sh $actionType $version
        exit
    fi
}
Get_Sys_Version

if [ -f "/etc/redhat-release" ] && [ $(cat /etc/os-release|grep PLATFORM_ID|grep -oE "el8|an8") ];then
	el="8"
fi

CENTOS_OS=$(cat /etc/redhat-release|grep  -oEi centos)
if [ "${CENTOS_OS}" ] && [ -z "${el}" ];then
	el=$(cat /etc/redhat-release|grep -oE "([6-8]\.)+[0-9]+"|cut -f1 -d ".")
fi

ALIYUN_OS=$(cat /etc/redhat-release|grep  -oEi aliyun)
if [ "${ALIYUN_OS}" ];then
	el=$(uname -r|grep -oE "al7|al8"|grep -oE "[7-8]")
fi
ALIBABAA_OS=$(cat /etc/redhat-release |grep Alibaba|grep " 3")
if [ "${ALIBABAA_OS}" ];then
    el=8
fi

if [ "${el}" == "6" ]; then
	wget -O mysql.sh $download_Url/install/1/old/mysql.sh -T 20
	bash mysql.sh $1 $2
	exit;
fi

# HUAWEI_CLOUD_EULER=$(cat /etc/os-release |grep '"Huawei Cloud EulerOS 1')
# EULER_OS=$(cat /etc/os-release |grep "EulerOS 2.0 ")
# [ "${HUAWEI_CLOUD_EULER}" ] && el="8"
# [ "${EULER_OS}" ] && el="7"

if [ "${el}" == "8" ]; then
	if [ ! -f "/usr/bin/python" ];then
		yum install python3 -y
		ln -sf /usr/bin/python3 /usr/bin/python
	fi
fi

if [ -z "${el}" ] || [ "${Is_64bit}" == "32" ] || [ -z "${sysType}" ] || [ "$2" == "mariadb_10.6" ] || [ "$2" == "mariadb_10.7" ] || [ "$2" == "mariadb_11.3" ] || [ "$2" == "mariadb_10.11" ] ;then
	wget -O mysql.sh $download_Url/install/0/mysql.sh -T 20
	bash mysql.sh $1 $2
	exit;
fi

Install_mysqldb()
{
	wget -O MySQL-python-1.2.5.zip ${download_Url}/install/src/MySQL-python-1.2.5.zip -T 10
	unzip MySQL-python-1.2.5.zip
	rm -f MySQL-python-1.2.5.zip
	cd MySQL-python-1.2.5
	python setup.py install
	cd ..
	rm -rf MySQL-python-1.2.5
}

Install_mysqldb3()
{
	wget -O mysqlclient-1.3.12.zip ${download_Url}/install/src/mysqlclient-1.3.12.zip -T 10
	unzip mysqlclient-1.3.12.zip
	rm -f mysqlclient-1.3.12.zip
	cd mysqlclient-1.3.12
	python setup.py install
	cd ..
	rm -rf mysqlclient-1.3.12
}

Install_Mysql(){
    if [ -z "${sqlVersion}" ];then
        wget -O mysql.sh $download_Url/install/0/mysql.sh -T 20
    	bash mysql.sh $actionType $version
    	exit;
    fi
	Uninstall_Mysql

	if ! id mysql &> /dev/null;then
		groupadd mysql
		useradd -s /sbin/nologin -M -g mysql mysql
	fi

	echo "正在下载mysql极速安装包......"
	wget -O bt-${sqlVersion}.rpm ${download_Url}/rpm/centos${el}/${Is_64bit}/bt-${sqlVersion}.rpm
	echo "正在安装mysql......."
	rpm -ivh bt-${sqlVersion}.rpm --force --nodeps
	rm -f bt-${sqlVersion}.rpm
	if [ ! -f "/www/server/mysql/bin/mysql" ];then
		echo '========================================================'
		GetSysInfo
		echo -e "ERROR: mysql-rpm-$version installation failed.";
		echo -e "安装失败，请截图以上报错信息发帖至论坛www.bt.cn/bbs求助"
		exit 1
	fi
}

Install_Mysql_PyDb(){
	PYMYSQL_C=$(btpip list 2> /dev/null |grep PyMySQL)
	if [ "$PYMYSQL_C" ]; then
		return
	fi
	pyVersion=$(python -V 2>&1|awk '{printf ("%d",$2)}')
	if [ "${pyVersion}" == "2" ];then
		if [ -f "${Setup_Path}/mysqlDb3.pl" ]; then
			pip uninstall mysqlclient -y
			Install_mysqldb3
			/etc/init.d/bt reload
		else
			pip uninstall mysql-python -y
			pipUrl=`cat /root/.pip/pip.conf|awk 'NR==2 {print $3}'`
			if [ "${pipUrl}" != "" ]; then
				checkPip=`curl --connect-timeout 5 --head -s -o /dev/null -w %{http_code} ${pipUrl}`
			fi
			if [ "${checkPip}" = "200" ]; then
				pip install mysql-python
			else
				Install_mysqldb
				/etc/init.d/bt reload
			fi
		fi
		pip uninstall pymysql -y
		pip install pymysql
	else
		pip uninstall pymysql -y
		pip install pymysql
	fi	
}
Uninstall_Mysql(){
	yum remove mysql-devel -y > /dev/null 2>&1
	[ -f "/etc/init.d/mysqld" ] && /etc/init.d/mysqld stop
	mysqlVersion=`rpm -qa |grep bt-mysql-`
	mariadbVersion=`rpm -qa |grep bt-mariadb-`
	[ "${mysqlVersion}" ] && rpm -e $mysqlVersion --nodeps
	[ "${mariadbVersion}" ] && rpm -e $mariadbVersion --nodeps
	[ -f "${Setup_Path}/rpm.pl" ] && yum remove $(cat ${Setup_Path}/rpm.pl) -y
	if [ -f "${Setup_Path}/bin/mysql" ] || [ -f "/etc/init.d/mysqld" ]; then
		/etc/init.d/mysqld stop > /dev/null 2>&1
		chkconfig --del mysqld
		rm -rf /etc/init.d/mysqld
		rm -rf ${Setup_Path}
		mkdir -p /www/backup
		mv -f $Data_Path  /www/backup/oldData
		rm -rf $Data_Path
		rm -f /usr/bin/mysql*
		rm -f /usr/lib/libmysql*
		rm -f /usr/lib64/libmysql*
	fi
	
}

actionType=$1
version=$2

if [ "${actionType}" == 'install' ] || [ "${actionType}" == "update" ];then
    MYSQL_RUN=$(ps -ef|grep mysql|grep -v grep) 
    if [ "${MYSQL_RUN}" ] && [ -f "/www/server/mysql/bin/mysql" ];then
        echo "当前已有数据库正在运行 停止安装!"
        exit 1 
    fi
	if [ -z "${version}" ]; then
		exit
	fi
	mysqlpwd=`cat /dev/urandom | head -n 16 | md5sum | head -c 16`
	case "$version" in
		'5.1')
			sqlVersion="mysql51"
			;;
		'5.5')
			sqlVersion="mysql55"
			;;
		'5.6')
			sqlVersion="mysql56"
			;;
		'5.7')
			sqlVersion="mysql57"
			;;
		'8.0')
			sqlVersion="mysql80"
			;;
		'alisql')
			sqlVersion="AliSQL"
			;;
		'mariadb_10.0')
			sqlVersion="mariadb100"
			;;		
		'mariadb_10.1')
			sqlVersion="mariadb101"
			;;
		'mariadb_10.2')
			sqlVersion="mariadb102"
			;;
		'mariadb_10.3')
			sqlVersion="mariadb103"
			;;
		'mariadb_10.4')
			sqlVersion="mariadb104"
			;;
		'mariadb_10.5')
			sqlVersion="mariadb105"
			;;
	esac
	Install_Mysql
	Install_Mysql_PyDb
	mysqlpwd=`cat /dev/urandom | head -n 16 | md5sum | head -c 16`
    if [ -f '/www/server/panel/tools.py' ] && [ "btpython" ];then
		echo "正在初始化Mysql root密码...."
        set_r=$(btpython /www/server/panel/tools.py root $mysqlpwd > /dev/null 2>&1)
		echo "初始化完成!"
		echo "root密码：${mysqlpwd}"
		echo "============================="
		echo "mysql-$version已经完成安装！"
    fi
elif [ "$actionType" == 'uninstall' ];then
	Uninstall_Mysql del
fi


