#!/bin/bash



###################################################
# Utils for hooks installation or removing        #
###################################################
. /usr/share/alt-mod-passenger-5.3.7-9.el7.cloudlinux/include/cpanel-common-lve

function createHookHeader(){
    #1 - hook name
    if [ ! -e "$common_tmp_path" ]; then
        mkdir -p "$common_tmp_path"
    fi
    if [ ! -e "$1" ];then
        touch "$1"
    fi
    if [ -e "$1" ];then
        chmod 755 "$1"
        TMP=`cat "$1" | grep "#!/bin/bash"`
        if [ -z "$TMP" ];then
            echo "#!/bin/bash" > "$common_tmp_path/future_hook.tmp.$$"
            cat "$1" >> "$common_tmp_path/future_hook.tmp.$$"
            cat "$common_tmp_path/future_hook.tmp.$$" > "$1"
            rm -f "$common_tmp_path/future_hook.tmp.$$"
        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_tmp_path/future_hook.tmp.$$"
        cat "$common_tmp_path/future_hook.tmp.$$" > "$1"
        rm -f "$common_tmp_path/future_hook.tmp.$$"
    fi
}



####################################################
# EasyApache hooks                                 #
####################################################

before_apache_make="/scripts/before_apache_make"
before_httpd_restart_tests="/scripts/before_httpd_restart_tests"
postupcp="/scripts/postupcp"

####################################################
# Hooks list                                       #
####################################################

modinstall_HOOK="$common_path_of_cpanel/hooks/modinstall.hook"
cpanelpostupcp_HOOK="$common_path_of_cpanel/hooks/cpanelpostupcp.hook"
clearconf_HOOK="$common_path_of_cpanel/hooks/clearconf.hook"


writeToLog "Install cPanel hooks"

showBar 1
#Install new hooks
createHookHeader "$before_apache_make"
createHookHeader "$before_httpd_restart_tests"
createHookHeader "$postupcp"
showBar 2
writeToLog "Headers checked"
addHookStringFirst "$before_apache_make" "$clearconf_HOOK" "mod_passenger version $VERSION" "clearconf.hook"
removeEmptyStringsFromFile "$before_apache_make"
showBar 3
writeToLog "before_apache_make checked"
checkHookString "$before_httpd_restart_tests" "$modinstall_HOOK" "mod_passenger Version $VERSION mod_passenger module installation"
checkHookString "$before_httpd_restart_tests" "$cpanelpostupcp_HOOK" "mod_passenger Version $VERSION Rebuild Apache config hook"
removeEmptyStringsFromFile "$before_httpd_restart_tests"
showBar 4
writeToLog "before_httpd_restart_tests checked"
checkHookString "$postupcp" "$cpanelpostupcp_HOOK" "mod_passenger Version $VERSION Rebuild Apache config hook"
removeEmptyStringsFromFile "$postupcp"
showBar 5
writeToLog "postupcp checked"
writeToLog "Hooks installed"

echo
