#!/bin/bash

IUSER=$1

mysql -v
if [ $? -eq 0 ];then
	echo 'mysql 已安装'
	echo 'mysql 已安装' >>/home/stop.log
	exit
fi

echo "<<=========== mysql部署[开始] ==========>>"
date '+时间 : %Y-%m-%d %H:%M:%S' >> /home/start.log
echo "<<=========== mysql部署[开始] ==========>>" >> /home/start.log
echo "##########" >> /home/start.log

function schdpkg
{
        rs=0
        [ ! -n "`dpkg -l | grep "$1"`" ] || [ ! -n "`dpkg -l | grep "ii  $1"`" ] || rs=1
        return $rs 
}
MYSQL_ROOT_PASS="Simba.123456"
mysqlVer="mysql-server-5.7"
schdpkg ${mysqlVer}
s=$?
if [ $s -eq 0 ];then
        echo "${mysqlVer} was not installed" 
        echo "mysql-server mysql-server/root_password password $MYSQL_ROOT_PASS" > /tmp/mysql.preseed
        echo "mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASS" >> /tmp/mysql.preseed
        cat /tmp/mysql.preseed | sudo debconf-set-selections
        rm /tmp/mysql.preseed
        apt-get install ${mysqlVer} -y
fi
depFile=/etc/mysql/debian.cnf.bak
cp /etc/mysql/debian.cnf ${depFile}
sed -i "s/[[:space:]]//g" ${depFile}
debusr=`sed -n '4p' ${depFile}`
debpasswd=`sed -n '5p' ${depFile}`
#echo ${debusr:5}
#echo ${debpasswd:9}
rm -rf ${depFile}

myPath="/etc/mysql/mysql.conf.d/mysqld.cnf"
HOSTNAME="127.0.0.1"
PORT="3306"
USERNAME="root"
PASSWORD="${MYSQL_ROOT_PASS}"
DBNAME="mysql"
#14.04
#db_sql_0="update user set password=password('${MYSQL_ROOT_PASS}');"
#db_sql_1="update user set host = '%' where user = 'root' and host = '::1';"
#db_sql_x="SET SQL_SAFE_UPDATES=0;" 
#16.04
db_sql_0="use mysql;"
db_sql_1="update user set host = '%'  where user ='root';"
db_sql_2="FLUSH PRIVILEGES;"
if [ -f ${myPath} ];then
	echo mysql install
	#echo ${db_sql_0}
	#echo ${db_sql_1}
	#echo ${db_sql_2}
	sed -i "s/127\.0\.0\.1/0\.0\.0\.0/g"  ${myPath}
	sed -i "61a max_connections        = 2000" ${myPath}
	echo -e "# \n# addSimbaConfig\n# init time :`date +%Y%m%d`." >> ${myPath}
	echo -e "# author : whhsn.cn  " >> ${myPath}
	echo -e "skip-name-resolve\ndefault-storage-engine=innodb\ncharacter-set-server=utf8" >> ${myPath}
	echo "sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" >> ${myPath}
	sshpass -p${PASSWORD} mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p ${DBNAME} -e "${db_sql_0}"
	sshpass -p${PASSWORD} mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p ${DBNAME} -e "${db_sql_1}"
	sshpass -p${PASSWORD} mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p ${DBNAME} -e "${db_sql_2}"
	service mysql stop
	service mysql start
	echo mysql install OK!
	echo "<<==================================>>" >> /home/start.log
	echo "<<==     mysql install OK!!!      ==>>" >> /home/start.log
	echo "<<==================================>>" >> /home/start.log

else
	rm -rf /usr/lib/jvm/java
	rm -rf /tmp/winit.lock
#	rm -rf /home/${IUSER}/simba/server
	mv ~/.bashrc.bak ~/.bashrc
	mv /etc/rc.local.bak /etc/rc.local
	apt-get remove ${mysqlVer} -y
	echo mysql config not found
	echo init err , please try again.
	exit 1;
fi

