#!/usr/bin/env bash
# Filename:MySQL_Install.sh
# Description: MySQL Installation Script
# Author: shuhui.qiu @2015
# Version: 1.0

######################## Define Variables Start ########################
# EXPORT PATH
export PATH=/bin:/sbin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

# Tarball Package PATH
LCD="/usr/local/src"

# Package Install PATH
PREFIX="/usr/local"

# The User For MySQL
OWNER="mysql"

# MySQL Root Manager Password
mysqlrootpwd="123456"

# MySQL Data Directory
DATADIR="${PREFIX}/mysql/data"

# MySQL Version
VER_55='5.5.46'
VER_56='5.6.27'

# Make Thread 
CPU_THREDS=$(expr $(grep processor /proc/cpuinfo|wc -l) + 1)
 
# Color Variables 
export black='\033[0m'
export boldblack='\033[1;0m'
export red='\033[31m'
export boldred='\033[1;31m'
export green='\033[32m'
export boldgreen='\033[1;32m'
export yellow='\033[33m'
export boldyellow='\033[1;33m'
export blue='\033[34m'
export boldblue='\033[1;34m'
export magenta='\033[35m'
export boldmagenta='\033[1;35m'
export cyan='\033[36m'
export boldcyan='\033[1;36m'
export white='\033[37m'
export boldwhite='\033[1;37m'
 
######################## Define Variables End ######################## 

function is_ROOT()
{
	# Check Run User is root,If Not Exit
	if [ $(id -u) -ne "0" ]; then
	echo -en ${boldyellow} 'Error: Please use root role to install me!' ${black};exit 1
	fi
	
}

function Create_User()
{
	# Create User for MySQL
	id ${OWNER} >/dev/null 2>&1
	if [ $? -eq "1" ];
	then
		/usr/sbin/groupadd ${OWNER}
		/usr/sbin/useradd -s /sbin/nologin -M -g ${OWNER} ${OWNER} ;
	fi
}

function Install_Depend()
{
	for pkg in wget gcc gcc-c++ make cmake ncurses-devel libxml2-devel libtool-ltdl-devel autoconf bison zlib-devel;
	do
		yum -y install ${pkg};
	done		
}

function mysql55()
{
	# Create Directory for MySQL DATA
	if [[ ! -d ${DATADIR} ]]
	then
		mkdir -p ${DATADIR};
	fi
	chown mysql.mysql ${PREFIX}/mysql/data;
	
	cd ${LCD}
	

	wget -c http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-${VER_55}.tar.gz
	tar axvf mysql-${VER_55}.tar.gz;cd mysql-${VER_55};
	cmake -DCMAKE_INSTALL_PREFIX=${PREFIX}/mysql \
	-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
	-DDEFAULT_CHARSET=utf8 \
	-DEXTRA_CHARSETS=all \
	-DDEFAULT_COLLATION=utf8_general_ci \
	-DWITH_EXTRA_CHARSETS:STRING=utf8 \
	-DWITH_MYISAM_STORAGE_ENGINE=1 \
	-DWITH_INNOBASE_STORAGE_ENGINE=1 \
	-DWITH_MEMORY_STORAGE_ENGINE=1 \
	-DWITH_READLINE=1 \
	-DENABLED_LOCAL_INFILE=1 \
	-DMYSQL_DATADIR=${PREFIX}/mysql/data \
	-DMYSQL_USER=mysql \
	-DMYSQL_TCP_PORT=3306 \
	-DSYSCONFDIR=/etc \
	-DINSTALL_SHAREDIR=share
	make -j ${CPU_THREDS};make install
	
	# Create sys_service
	cp support-files/mysql.server /etc/init.d/mysqld
	chmod 755 /etc/init.d/mysqld
	chown -R mysql.mysql ${PREFIX}/mysql

	# Initialize MySQL
	${PREFIX}/mysql/scripts/mysql_install_db \
	--user=mysql \
	--datadir=${DATADIR} \
	--basedir=${PREFIX}/mysql/ \
	--defaults-file=/etc/my.cnf
	
	
	echo "${PREFIX}/mysql/lib/">/etc/ld.so.conf.d/mysql.conf;/sbin/ldconfig
	
	if [[ -e /etc/my.cnf ]]
	then 
		mv /etc/my.cnf /etc/mysql.cnf.$(date +%Y%m%d.%H%m)
	fi
	
	wget http://down.vqiu.cn/conf/Linux_conf/mysql/my.cnf -O /etc/my.cnf
	
	# Set ENV. Variables
	echo "export PATH=\$PATH:${PREFIX}/mysql/bin">/etc/profile.d/mysql.sh
	chmod +x /etc/profile.d/mysql.sh;source /etc/profile.d/mysql.sh

	
	# Start MySQL Service
	/etc/init.d/mysqld start
	chkconfig mysqld on
	
	# Set root User Password
	${PREFIX}/mysql/bin/mysqladmin -u root password ${mysqlrootpwd};
	${PREFIX}/mysql/bin/mysql -uroot password${mysqlrootpwd} -e "drop database test;DROP USER ''@'%';flush privileges;"
	
}

function mysql56()
{
	# Create Directory for MySQL DATA
	if [[ ! -d ${DATADIR} ]]
	then
		mkdir -p ${DATADIR};
	fi
	chown mysql.mysql ${PREFIX}/mysql/data;
	
	cd ${LCD}	

	wget -c http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-${VER_56}.tar.gz
	tar axvf mysql-${VER_56}.tar.gz;cd mysql-${VER_56};
	cmake -DCMAKE_INSTALL_PREFIX=${PREFIX}/mysql \
	-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
	-DDEFAULT_CHARSET=utf8 \
	-DEXTRA_CHARSETS=all \
	-DDEFAULT_COLLATION=utf8_general_ci \
	-DWITH_EXTRA_CHARSETS:STRING=utf8 \
	-DWITH_MYISAM_STORAGE_ENGINE=1 \
	-DWITH_INNOBASE_STORAGE_ENGINE=1 \
	-DWITH_MEMORY_STORAGE_ENGINE=1 \
	-DWITH_READLINE=1 \
	-DENABLED_LOCAL_INFILE=1 \
	-DMYSQL_DATADIR=${PREFIX}/mysql/data \
	-DMYSQL_USER=mysql \
	-DMYSQL_TCP_PORT=3306 \
	-DSYSCONFDIR=/etc \
	-DINSTALL_SHAREDIR=share
	make -j ${CPU_THREDS};make install
	
	# Create sys_service
	cp support-files/mysql.server /etc/init.d/mysqld
	chmod 755 /etc/init.d/mysqld
	chown -R mysql.mysql ${PREFIX}/mysql

	# Initialize MySQL
	${PREFIX}/mysql/scripts/mysql_install_db \
	--user=mysql \
	--datadir=${DATADIR} \
	--basedir=${PREFIX}/mysql/ \
	--defaults-file=/etc/my.cnf
	
	
	echo "${PREFIX}/mysql/lib/">/etc/ld.so.conf.d/mysql.conf;/sbin/ldconfig
	
	if [[ -e /etc/my.cnf ]]
	then 
		mv /etc/my.cnf /etc/mysql.cnf.$(date +%Y%m%d.%H%m)
	fi
	
	wget http://down.vqiu.cn/conf/Linux_conf/mysql/my.cnf -O /etc/my.cnf
	
	# Set ENV. Variables
	echo "export PATH=\$PATH:${PREFIX}/mysql/bin">/etc/profile.d/mysql.sh
	chmod +x /etc/profile.d/mysql.sh;source /etc/profile.d/mysql.sh

	
	# Start MySQL Service
	/etc/init.d/mysqld start
	chkconfig mysqld on
	
	# Set root User Password
	${PREFIX}/mysql/bin/mysqladmin -u root password ${mysqlrootpwd};
	${PREFIX}/mysql/bin/mysql -uroot password${mysqlrootpwd} -e "drop database test;DROP USER ''@'%';flush privileges;"
	
}

	echo -en ${boldgreen} "1) Install MySQL-${VER_55}\n"
	echo -en ${boldgreen} "2) Install MySQL-${VER_56} [Defalut]\n" ${black}
	read -p "Please Enter Number:" DB_VER

	# Getting users to install version
	case "${DB_VER}" in
		1)
			echo "You will install MySQL-${VER_55}"
			DB_VER='55'
			;;
		2)
			echo "You will install MySQL-${VER_56}"
			DB_VER="56"
		;;
		"")
			echo "You will install MySQL-${VER_56}"
			DB_VER="56"
		;;
		*)
			echo "INPUT error,You will install MySQL-${VER_56}"
			DB_VER="56"
	esac


	if [[ ${DB_VER} -eq '55' ]];
	then
		is_ROOT
		Create_User
		Install_Depend
		mysql55
		
	elif [[ ${DB_VER} -eq '56' ]]
	then
		is_ROOT
		Create_User
		Install_Depend
		mysql56
	fi