#!/bin/bash
#INSTALL DBGOV

VERSION="0.1"
disable_update_variable="/etc/mysqlupdisable"
common_path_of_governor="/usr/share/lve/db_governor"
postupcp="/scripts/postupcp"

function getCurrentMySQLVersion(){
    VER=$(rpm -qa MySQL-server)
    if [ -z "$VER" ];then
	VER=$(rpm -qa cpanel-MySQL-server)
    fi
    mysqlver=$(echo "$VER" | sed "s/[A-Za-z\-]//g")
    mysqlver=${mysqlver:0:3} 
    echo $mysqlver
}

function disableExclude(){
    yum clean all
}

function enableExclude(){
    yum clean all
}

function checkMySQL(){
    res=$(rpm -qa cpanel-MySQL-server | grep cpanel-MySQL-server-$1)
    echo "$res"
}

function installMySQL(){
    mkdir -p "$common_path_of_governor/oldinfo"
    mkdir -p "$common_path_of_governor/tmp"
    echo "Install cpanel-MySQL-$1"
    yum erase -y MySQL-client MySQL-devel MySQL-bench MySQL-shared MySQL-server cpanel-MySQL-client-$1 cpanel-MySQL-devel-$1 cpanel-MySQL-bench-$1 cpanel-MySQL-shared-$1 cpanel-MySQL-server-$1
    yum -y install cpanel-MySQL-client-$1 cpanel-MySQL-devel-$1 cpanel-MySQL-bench-$1 cpanel-MySQL-shared-$1 cpanel-MySQL-server-$1 --enablerepo=cloudlinux-updates-testing --enablerepo=cloudlinux-updates --disableexcludes=all
    /scripts/restartsrv_mysql
}

function checkForOldHook(){
    #1 - hook
    #2 - tmp name
    if [ -e "$1" ];then
        chunkvariable=0
        touch "$common_path_of_governor/tmp/$2.tmp.$$"
        condition=0
        isfindbash="";
        cat "$1" | while read line
        do
            tmp=`echo $line | grep 'INSTALL DBGOV'`
            tmp2=`echo $line | grep 'dbgovernorInstall 5.1.48$'`
            tmp3=`echo $line | grep "#!/bin/bash"`
            tmp4=`echo $line | grep "fi$"`
            if [ -n "$tmp" ];then
                condition=1
            fi
            if [ $condition == 0 ];then
                if [ -z "$isfindbash" -o -z "$tmp3" ];then
                    echo $line >> "$common_path_of_governor/tmp/$2.tmp.$$"
                fi
            fi
            if [ -n "$tmp2" ];then
                if [ $chunkvariable == 0 ];then                                                                                                                 
                    chunkvariable=1                                                                                                                                  
                fi
            fi 
            if [ -z "$isfindbash" -a -n "$tmp3" ];then
                isfindbash="Y"
            fi
            if [ $chunkvariable == 1 -a -n "$tmp4" ];then
                chunkvariable=0
                condition=0
            fi
        done                                              
        cat "$common_path_of_governor/tmp/$2.tmp.$$" > "$1"
        rm -f "$common_path_of_governor/tmp/$2.tmp.$$"                                                    
    fi
    touch "$common_path_of_governor/tmp/postupcp.tmp.$$"
    cat "$postupcp" | sed /install-db-governor/d > "$common_path_of_governor/tmp/postupcp.tmp.$$"     
    cat "$common_path_of_governor/tmp/postupcp.tmp.$$" > "$postupcp"
    rm -f "$common_path_of_governor/tmp/postupcp.tmp.$$"
    
}

function installUpCpHook(){
    
    if [ -e "/scripts/postupcp" ];then
	checkForOldHook /scripts/postupcp "future"
    fi
    if [ ! -e "$disable_update_variable" ];then
	echo "Create disable file"
	touch "$disable_update_variable"
    else
	if [ ! -e "$common_path_of_governor/oldinfo/mysqlupdisable" ];then
	    cp "$disable_update_variable" "$common_path_of_governor/oldinfo/mysqlupdisable"
	fi
    fi
}

function dbgovernorInstall(){
    res=$(checkMySQL $1)
    if [ -n "$res" ];then
	yum -y install db_governor --enablerepo=cloudlinux-updates-testing --disableexcludes=all
	chkconfig --level 35 db_governor on
	/sbin/service db_governor restart
	 installUpCpHook
    fi
}

ver=$(getCurrentMySQLVersion)
if [ "$ver" == "5.0" ];then
 echo "MySQL 5.0 detected"
 disableExclude
 installMySQL 5.0.91 
 dbgovernorInstall 5.0.91
 enableExclude
fi
if [ "$ver" == "5.1" ];then
 echo "MySQL 5.1 detected"
 installUpCpHook
 disableExclude
 installMySQL 5.1.48
 dbgovernorInstall 5.1.48
fi
