#!/bin/bash
#INSTALL DBGOV

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

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.$$"
    
}

checkForOldHook $postupcp