#!/bin/bash

###################################################
# Utils for hooks installation or removing        #
###################################################
. /opt/alt/python27/usr/share/python-cllib/scripts/cl-common


####################################################
# CPanel & EasyApache hooks                        #
####################################################

before_apache_make="/scripts/before_apache_make"
posteasyapache="/scripts/posteasyapache"
postupcp="/scripts/postupcp"
postwwwacct="/scripts/postwwwacct"
postmodifyacct="/scripts/postmodifyacct"
postrestoreacct="/scripts/postrestoreacct"

target="cagefs"
common_path="/usr/share/cagefs/cpanel"
####################################################
# Hooks list                                       #
####################################################

cagefs_HOOK="$common_path/cagefs_hook.sh"
suphp_patch_HOOK="$common_path/cpanel-compile-hook2.sh"
posteasyapache_hook="$common_path/cagefs_posteasyapache_hook.sh"
postupcp_hook="$common_path/cagefs_postupcp_hook.sh"
postwwwacct_hook="$common_path/cagefs_postwwwacct_hook.pl"' "$@"'
postmodifyacct_hook="$common_path/cagefs_postmodifyacct_hook.pl"' "$@"'
postrestoreacct_hook="$common_path/cagefs_postwwwacct_hook.pl user"' "$1"'

writeToLog "Install cPanel hooks" "$common_path"

#Install hook to build suexec
createHookHeader "$before_apache_make" "$target" "$common_path"
showBar 1

writeToLog "before_apache_make: Headers checked" "$common_path"

checkHookString "$before_apache_make" "$cagefs_HOOK" "CageFS Version $VERSION. Required for apache's suexec recompilation"
removeEmptyStringsFromFile "$before_apache_make"
showBar 2

writeToLog "before_apache_make checked" "$common_path"


#Install hook to patch suphp before make
createHookHeader "$before_apache_make" "$target" "$common_path"
showBar 3

writeToLog "before_apache_make: Headers checked" "$common_path"

checkHookString "$before_apache_make" "$suphp_patch_HOOK" "CageFS Version $VERSION. Required for apache's suphp recompilation"
removeEmptyStringsFromFile "$before_apache_make"
showBar 4

writeToLog "before_apache_make checked" "$common_path"


#Install hook to build suphp
createHookHeader "$posteasyapache" "$target" "$common_path"
showBar 5

writeToLog "posteasyapache: Headers checked" "$common_path"

checkHookString "$posteasyapache" "$posteasyapache_hook" "CageFS Version $VERSION. Required for suphp recompilation"
removeEmptyStringsFromFile "$posteasyapache"
showBar 6

writeToLog "posteasyapache checked" "$common_path"

#Install hook to update CageFS after update of CPanel
createHookHeader "$postupcp" "$target" "$common_path"
showBar 7

writeToLog "postupcp: Headers checked" "$common_path"

checkHookString "$postupcp" "$postupcp_hook" "CageFS Version $VERSION. Update CageFS after update of CPanel"
removeEmptyStringsFromFile "$postupcp"
showBar 8

writeToLog "postupcp checked" "$common_path"

createHookHeader "$postwwwacct" "$target" "$common_path"
showBar 9
writeToLog "postwwwacct header checked" "$common_path"

checkHookString "$postwwwacct" "$postwwwacct_hook" "CageFS Version $VERSION"
removeEmptyStringsFromFile "$postwwwacct"
showBar 10

writeToLog "postwwwacct hook installed" "$common_path"


createHookHeader "$postmodifyacct" "$target" "$common_path"
showBar 11
writeToLog "postmodifyacct header checked" "$common_path"

checkHookString "$postmodifyacct" "$postmodifyacct_hook" "CageFS Version $VERSION"
removeEmptyStringsFromFile "$postmodifyacct"
showBar 12

writeToLog "postmodifyacct hook installed" "$common_path"


createHookHeader "$postrestoreacct" "$target" "$common_path"
showBar 13
writeToLog "postrestoreacct header checked" "$common_path"

checkHookString "$postrestoreacct" "$postrestoreacct_hook" "CageFS Version $VERSION"
removeEmptyStringsFromFile "$postrestoreacct"
showBar 14

writeToLog "postrestoreacct hook installed" "$common_path"


writeToLog "Done installing hooks" "$common_path"

echo
