#!/usr/bin/bash

# Copyright © Cloud Linux GmbH \& Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

###################################################
# Utils for hooks installation or removing        #
###################################################
. /usr/share/lve/modproctitle/include/cpanel-common-lve

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
	touch "$1"
    fi
    if [ -e "$1" ];then
	chmod 755 "$1"
	tmp=`cat "$1" | grep "#!/bin/bash"`
	if [ -z "$tmp" ];then
	    echo "#!/bin/bash" > "$common_path_of_cpanel/tmp/future_hook.tmp.$$"
	    cat "$1" >> "$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
    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
}



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

before_apache_make="/scripts/before_apache_make"
before_httpd_restart_tests="/scripts/before_httpd_restart_tests"
postupcp="/scripts/postupcp"
after_apache_make_install="/scripts/after_apache_make_install"
posteasyapache="/scripts/posteasyapache"

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

proctitle_HOOK="$common_path_of_cpanel/hooks/proctitle.hook"
cpanelpostupcp_HOOK="$common_path_of_cpanel/hooks/cpanelpostupcp.hook"
#clearconf_HOOK="$common_path_of_cpanel/hooks/clearconf.hook"
#posteasyapache_HOOK="$common_path_of_cpanel/hooks/buildlsphp.hook"

VER=`getEasyApacheVer`
writeToLog "Install cPanel hooks. Apache ${VER}"

showBar 1

if [ "$VER" == "2_4" ]
then
    #Install new hooks
    createHookHeader "$before_apache_make"
    createHookHeader "$before_httpd_restart_tests"
    createHookHeader "$postupcp"
    createHookHeader "$after_apache_make_install"
    createHookHeader "$posteasyapache"
    writeToLog "Headers checked"
else
    writeToLog "No hooks for Apache ${VER}"
fi
showBar 2

if [ "$VER" == "2_4" ]
then
    checkHookString "$before_httpd_restart_tests" "$proctitle_HOOK" "mod_proctitle Version $VERSION mod_proctitle module installation"
    checkHookString "$before_httpd_restart_tests" "$cpanelpostupcp_HOOK" "mod_proctitle Version $VERSION Rebuild Apache config hook"
    removeEmptyStringsFromFile "$before_httpd_restart_tests"
    writeToLog "before_httpd_restart_tests checked"
fi

showBar 3
if [ "$VER" == "2_4" ]
then
    checkHookString "$postupcp" "$cpanelpostupcp_HOOK" "mod_proctitle Version $VERSION Rebuild Apache config hook"
    removeEmptyStringsFromFile "$postupcp"
    writeToLog "postupcp checked"
fi

showBar 4
if [ "$VER" == "2_4" ]
then
    checkHookString "$after_apache_make_install" "$proctitle_HOOK" "mod_proctitle Version $VERSION mod_proctitle module installation"
    checkHookString "$after_apache_make_install" "$cpanelpostupcp_HOOK" "mod_proctitle Version $VERSION Rebuild Apache config hook"
    removeEmptyStringsFromFile "$after_apache_make_install"
    writeToLog "after_apache_make_install checked"
fi

showBar 5
if [ "$VER" == "2_4" ]
then
    #checkHookString "$posteasyapache" "$posteasyapache_HOOK" "mod_proctitle Version $VERSION mod_proctitle module installation"
    checkHookString "$posteasyapache" "$cpanelpostupcp_HOOK" "mod_proctitle Version $VERSION Rebuild Apache config hook"
    removeEmptyStringsFromFile "$posteasyapache"
    writeToLog "posteasyapache checked"
fi

showBar 6
writeToLog "Done installing hooks"

echo
