#!/bin/bash

##################################################
# Common fucntions                               #
##################################################
VERSION="2.0"

common_path_of_cpanel="/usr/share/lve/dbgovernor/cpanel"
common_current_date=`date +%Y-%m-%d`
common_tmp_path="$common_path_of_cpanel/tmp"

function getLogFile(){
    if [ ! -e "$common_path_of_cpanel/logs" ];then
	mkdir -p "$common_path_of_cpanel/logs"
    fi
    current_date_time=`date +"%Y-%m-%d %k:%M:%S"`
    echo "$common_path_of_cpanel/logs/$common_current_date.log"
}

function writeToLog(){
    if [ ! -e "$common_path_of_cpanel/logs" ];then
	mkdir -p "$common_path_of_cpanel/logs"
    fi
    current_date_time=`date +"%Y-%m-%d %k:%M:%S"`
    prg=`basename "$0"`
    echo "[$current_date_time from $prg] $1" >> "$common_path_of_cpanel/logs/$common_current_date.log"
}

function writeFileToLog(){
    if [ ! -e "$common_path_of_cpanel/logs" ];then
	mkdir -p $common_path_of_cpanel/logs
    fi
    current_date_time=`date +"%Y-%m-%d %k:%M:%S"`
    prg=`basename "$0"`
    echo "[$current_date_time from $prg] ----------------File Content $1 BEG---------------" >> "$common_path_of_cpanel/logs/$common_current_date.log"
    if [ -e "$1" ];then
	cat $1 >> "$common_path_of_cpanel/logs/$common_current_date.log"
    fi
    echo "[$current_date_time from $prg] ----------------File Content $1 End---------------" >> "$common_path_of_cpanel/logs/$common_current_date.log"
}

function removeEmptyStringsFromFile(){
    filename="$1"
    res=`sed -e '/^$/d' $filename`
    echo "$res" > $filename
}

function deleteAllExcept(){
    #1 - hook
    #2 - tmp name
    #3 - pattern
    if [ ! -e "$common_tmp_path" ]; then
        mkdir -p "$common_tmp_path"
    fi
    if [ -e "$1" ];then
        cat "$1" | sed -n "$3" > "$common_tmp_path/$2.tmp.$$"
        echo "#!/bin/bash" > "$1"
        cat "$common_tmp_path/$2.tmp.$$" >> "$1"
        rm -f "$common_tmp_path/$2.tmp.$$"
    fi
}

function deleteAllInclude(){
    #1 - hook
    #2 - tmp name
    #3 - pattern
    if [ ! -e "$common_tmp_path" ]; then
        mkdir -p "$common_tmp_path"
    fi
    if [ -e "$1" ];then
        cat "$1" | sed "$3" > "$common_tmp_path/$2.tmp.$$"
        cat "$common_tmp_path/$2.tmp.$$" > "$1"
        rm -f "$common_tmp_path/$2.tmp.$$"
    fi
}

function showBar {
 nmb=$(cat $0 | grep showBar | wc -l)
 let "nmb = $nmb"
 let "prct = $1 * 30 / $nmb"
 let "prct_n = $1 * 100 / $nmb"
 prg=`basename "$0"`
 echo -n "$prg: [" >&2
 for bar in {1..30}
 do
  if [ $bar -le $prct ];then
    echo -n "#" >&2
  else
    echo -n " " >&2
  fi
 done
 echo -ne "] ($prct_n%)\r" >&2
}

function get_command(){                                                                                                                                       
    command=$(which $1)                                                                                                                                      
    if [ $? != 0 ]; then                                                                                                                                     
        writeToLog "Can't execute command $1..."                                                                                                                   
        exit 1;                                                                                                                                              
    fi                                                                                                                                                       
    echo $command                                                                                                                                            
} 

function createHookHeader(){
	#1 - hook name
	if [ ! -e "$common_path_of_cpanel/tmp" ]; then
		mkdir -p "$common_path_of_cpanel/tmp"
	fi
	if [ ! -e "$1" ]; then
		echo "#!/bin/bash" > "$1"
		chmod 755 "$1"
	else
		chmod 755 "$1"
		tmp=`cat "$1" | egrep "#!/bin/bash|#!/bin/sh"`
		if [ -z "$tmp" ];then
			rm -f "$1".governorh.bak
			mv "$1" "$1".governorh.bak
			echo "#!/bin/bash" > "$1"
			echo "$1.governorh.bak"' "$@"' >> "$1"
			chmod 755 "$1"
		fi
	fi
}

function checkHookString(){
    #1 - hook name
    #2 - pattern string
    #3 - comment srting
    if [ -e "$1" ];then
	tmp=`cat "$1" | grep "$2"`
	if [ -z "$tmp" ];then
	    echo "$2   #$3" >> $1
	fi
    fi
}

function addHookStringFirst(){
    #1 - hook name
    #2 - pattern string
    #3 - comment srting
    #4 - short hook name
    if [ -e "$1" ];then
	cat $1 | sed /$4/d | sed 1a"$2   #$3" > "$common_path_of_cpanel/tmp/future_hook.tmp.$$"
	cat "$common_path_of_cpanel/tmp/future_hook.tmp.$$" > "$1"
	rm -f "$common_path_of_cpanel/tmp/future_hook.tmp.$$"
    fi
}
