#!/bin/bash

#Define variables
VERSION_OF_SCRIPT="1.7"

#mod_hostinglimits
need_install_hostinglimits=""
need_hosting_hook=""
need_rebuild_conf=""
need_repo_data=""
fix_cpanel=""
force="force"
quite=""
debug=""
is_set_type_apache=""

#mod_fcgid
need_install_fcgid=""                                                                                                                                        
need_hosting_fhook=""

#mod_sucgid
need_install_sucgid=""                                                                                                                                        
need_sucgid_hook=""

#apache and mod_cgid.patch
need_modcgid_patch=""


OK="Ok"
FAILED="Failed"
TEST="/test"

#Initializing varaibles
#Global net-path variables
CMAKESRC="cmake-2.6.4.tar.gz"
REPOSRC="cloudlinux-release-5-5.el5.cloudlinux.noarch.rpm"
FTPSERVERCONF="http://repo.cloudlinux.com/cloudlinux/sources$TEST"
FTPSERVERRPM="http://repo.cloudlinux.com/cloudlinux/5.5/os/x86_64/CloudLinux"
FTPSERVERSRC="http://repo.cloudlinux.com/cloudlinux/sources$TEST"

#Local cPanel utilities
pathtoconfig1="/var/cpanel/templates/apache2"
pathtoconfig2="/usr/local/cpanel/etc/httptemplates/apache"
pathtoconfig3="/var/cpanel/templates/apache1"
needpath1="/usr/local/apache/conf"
needpath2="/etc/httpd/conf"
rebuildscript="/scripts/rebuildhttpdconf"
hook="/scripts/before_httpd_restart_tests"
pathtomodules1="/etc/httpd/modules"
pathtomodules2="/usr/local/apache/modules"
pathtomodules3="/usr/local/apache/libexec"
pathtophprebuild="/usr/local/cpanel/bin/rebuild_phpconf"
hook_fcgid="/scripts/before_apache_make"
postupcp="/scripts/postupcp"

#mod_fcgid
ARCHNAME="mod_fcgid"
MODULESRC_fcgid="$ARCHNAME.tar.gz"
CONFFILE_fcgid="$ARCHNAME.conf"
HOOKFILE_fcgid="$ARCH_NAME.hook"                                                                                                                                                             
 
#mod_hostinglimits
MODLIMSRC="mod_hostinglimits.tar.gz"
CONFFILE2="modhostinglimits.conf"
HOOKCONF2="hook2.conf"
archname2="mod_hostinglimits"

#mod_sucgid
ARCHNAME_sucgid="sucgid"
MODULESRC_sucgid="$ARCHNAME_sucgid.tar.gz"
CONFFILE_sucgid="modsucgid.conf"
HOOKFILE_sucgid="hook.conf"

#mod_cgid
PATCH_NAME="httpd-lvecgid.patch"
HOOKFILE_cgid="mcgid.hook"

#upcp
HOOKUPCP="postupcp.hook"

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

function check_package_update_permission {                                                                                                                    
    if [ -e /etc/yum.conf ] && [ -n "$(grep exclude /etc/yum.conf | grep $1)" ]; then                                                                    
        echo $1 update is prohibited on your system.                                                                                                     
        echo Remove "exclude $1" option from /etc/yum.conf                                                                                               
        echo and re-run this script again.                                                                                                                      
        exit 1;                                                                                                                                              
    fi;                                                                                                                                                      
} 

function rpmInstall {
    if [ -n "$force" ]; then
	frc="rpm -Uhv $1 --force"
    else
	frc="rpm -Uhv $1"
    fi
    if [ -n "$quite" ]; then
	frc=$(echo "$frc --quiet")
    fi
    $frc
    return $?
}
function yumInstall {
    check_package_update_permission $1
    frc="yum -y update $1 --nogpgcheck"
    if [ -n "$quite" ]; then
	frc=$(echo "$frc --quiet")
    fi
    $frc
    return $?
}
function yumInstallUpd {
    check_package_update_permission $1
    frc="yum -y install $1 --nogpgcheck"
    if [ -n "$quite" ]; then
	frc=$(echo "$frc --quiet")
    fi
    $frc
    return $?
}
function wgetInstall {
    iswget=$(get_command "wget")
    frc="$iswget $1"
    if [ -n "$quite" ]; then
	frc=$(echo "$frc -q")
    fi
    $frc
    return $?
}

function showMessage {
    if [ -n "$debug" ]; then
	if [ -n "$2" ]; then
	    echo "$1"
	else
	    echo -n "$1"
	fi
    fi
}
function isRPMInstall {
    if [ -n "$2" ];then
	showMessage "$2"
    fi
    which $1 &>/dev/null
    result=$?
    RET_VAL=$(rpm -qa "$1")
    if [ -z "$RET_VAL" -a $result != 0 ]; then 
     showMessage $FAILED "NEWLINE"
     return 0
    else
     showMessage $OK "NEWLINE"
     return 1
    fi
}
function isRPMInstallForce {
    if [ -n "$2" ];then
	showMessage "$2 $force"
    fi
    which $1 &>/dev/null
    result=$?
    RET_VAL=$(rpm -qa "$1")
    if [ -n "$force" ]; then
	RET_VAL=""
	result=1
    fi
    if [ -z "$RET_VAL" -a $result != 0 ]; then 
     showMessage $FAILED "NEWLINE"
     return 0
    else
     showMessage $OK "NEWLINE"
     return 1
    fi
}
function isFileInCurrDir {
    if [ -n "$2" ];then
	showMessage "$2"
    fi
    RET_VAL=$(ls | grep "$1")
    if [ -n "$force" ]; then
	if [ -n "$RET_VAL" ]; then
	    rm "$RET_VAL"
	fi
	RET_VAL=""
    fi
    if [ -z "$RET_VAL" ]; then 
     showMessage $FAILED "NEWLINE"
     return 0
    else
     showMessage $OK "NEWLINE"
     return 1
    fi
}
function getFilesFromFtp {
    isFileInCurrDir $1 "Downloading $3..."
    if [[ $? == 0 ]]; then
	wgetInstall "$2/$1"
	if [[ $? != 0 ]]; then
	{
    	    showMessage "Cannot Download $3... Script Failed..." "NEWLINE"
    	    return 0
	} fi
    fi    
    return 1
}
function getPathToFile {
    file_name=$(ls $1 | head -n1)
    if [ -z "$file_name" ]; then
	echo ""
	return 0
    else
	echo "$file_name"
	return 1
    fi
}
function makeSource {
	CMAKE_SRC=tmpcmakesrc
	mkdir -p $CMAKE_SRC
	tar -zxvf $1 -C $CMAKE_SRC
	(cd $CMAKE_SRC/$2
	    ./configure
	    make
	    make install)
	makeproc=$?
	rm -rf $CMAKE_SRC
	return $makeproc
}
function getPathToApacheModules {
    if [ -n "$1" -a -e "$1" ]; then
	echo "$1"
	return 1
    fi
    
    if [ -n "$2" -a -e "$2" ]; then
	echo "$2"
	return 1
    fi
    
    if [ -n "$3" -a -e "$3" ]; then
	echo "$3"
	return 1
    fi
    
    if [ -n "$4" -a -e "$4" ]; then
	echo "$4"
	return 1
    fi
    
    return 0
}
function cmakeSorce {
 iscmake=$(get_command "cmake")
 CMAKE_SRC=tmpcmakesrc
 mkdir -p $CMAKE_SRC 
 if [ -z "$quite" ]; then
 tar -zxvf $1 -C $CMAKE_SRC
 (cd $CMAKE_SRC/$2
	    $iscmake . && make && make install)
 else
 tar -zxf $1 -C $CMAKE_SRC
 (cd $CMAKE_SRC/$2 
	    $iscmake . 1>/dev/null && make 1>/dev/null && make install 1>/dev/null) 
 fi
 makeproc=$?
 rm -rf $CMAKE_SRC
 return $makeproc
}
function cmakeSorce_fcgid {
 CMAKE_SRC=tmpcmakesrc
 mkdir -p $CMAKE_SRC 
 if [ -z "$quite" ]; then
 tar -zxvf $1 -C $CMAKE_SRC
 curr_path=$(pwd)
 cd $CMAKE_SRC/$2
 ./configure.apxs && 
 make &&
 cp modules/fcgid/.libs/mod_fcgid.so $3
 cd $curr_path
 else 
 tar -zxf $1 -C $CMAKE_SRC
 curr_path=$(pwd)
 cd $CMAKE_SRC/$2
 
  if [ -e "$3/mod_fcgid.so" ];then
    if [ -e "$3/mod_fcgid.so.bak" ];then
        buf="OK"
    else
        mv $3/mod_fcgid.so $3/mod_fcgid.so.bak
    fi
 fi


 ./configure.apxs 1>/dev/null && 
 make 1>/dev/null &&
 cp -f modules/fcgid/.libs/mod_fcgid.so $3 1>/dev/null
 cd $curr_path
 fi
 makeproc=$?
 cp -f $CMAKE_SRC/$2/mod_fcgid.conf $curr_path
 cp -f $CMAKE_SRC/$2/mod_fcgid.hook $curr_path
 rm -rf $CMAKE_SRC
 return $makeproc
}
function cmakeSorce_sucgid {
 iscmake=$(get_command "cmake")
 CMAKE_SRC=tmpcmakesrc
 mkdir -p $CMAKE_SRC 
 if [ -z "$quite" ]; then
 tar -zxvf $1 -C $CMAKE_SRC
 (cd $CMAKE_SRC/$2
	    $iscmake -DAPACHE_USER:string=nobody && make && make install)
 else
 tar -zxf $1 -C $CMAKE_SRC
 (cd $CMAKE_SRC/$2 
	    $iscmake -DAPACHE_USER:string=nobody 1>/dev/null && make 1>/dev/null && make install 1>/dev/null) 
 fi
 makeproc=$?
 rm -rf $CMAKE_SRC
 return $makeproc
}
function installModule {
	pathtosrc=$(getPathToFile "$1.tar.gz")
	if [[ $? == 0 ]]; then
	    showMessage "$FAILED Cannot find $1" "NEWLINE"
	else
 
	    cmakeSorce $pathtosrc $1    
	    if [[ $? != 0 ]]; then
    		showMessage "Linking error $1" "NEWLINE"
	    else
    		showMessage "Module compiled... Ok" "NEWLINE"

		if [ -e "$2" ]; then
		    needpath=$(echo  $2)
		else
		    needpath=$(echo  "/etc/httpd")
		fi
		
		if [ -e "$needpath/conf.d" ]; then
		    showMessage "Check for conf.d... Ok" "NEWLINE"
		else
		    mkdir "$needpath/conf.d"
		    showMessage "Check for conf.d...Created Ok" "NEWLINE"
		fi
		
		(echo "LoadModule $6 $4/$3.so" && cat "$5") > "$needpath/conf.d/$5"
		showMessage "Config file for module $6 was successfully created... Ok" "NEWLINE"
		return 1
		    		
	    fi
 
	fi
        return 0

}

function installModule_hosting {
	pathtosrc=$(getPathToFile "$1.tar.gz")
	if [[ $? == 0 ]]; then
	    showMessage "$FAILED Cannot find $1" "NEWLINE"
	else
 
	    cmakeSorce $pathtosrc $1    
	    if [[ $? != 0 ]]; then
    		showMessage "Linking error $1" "NEWLINE"
	    else
    		showMessage "Module compiled... Ok" "NEWLINE"

		if [ -e "$2" ]; then
		    needpath=$(echo  $2)
		else
		    needpath=$(echo  "/etc/httpd")
		fi
		
		if [ -e "$needpath/conf.d" ]; then
		    showMessage "Check for conf.d... Ok" "NEWLINE"
		else
		    mkdir "$needpath/conf.d"
		    showMessage "Check for conf.d...Created Ok" "NEWLINE"
		fi
		
		sed "s#module_name#$6#g" "$5" | sed "s#module_nm#$3#g" | sed  "s#module_path#$4#g" > "$needpath/conf.d/modhostinglimits.conf"
		showMessage "Config file for module $1 was successfully created... Ok" "NEWLINE"
		return 1
		    		
	    fi
 
	fi
        return 0

}
function installModule_fcgid {
        pathtosrc=$(getPathToFile "$1")
        if [[ $? == 0 ]]; then
            showMessage "$FAILED Cannot find $1" "NEWLINE"
        else
            
            
            cmakeSorce_fcgid $1 $ARCHNAME $4
               
            if [[ $? != 0 ]]; then
                showMessage "Linking error $1" "NEWLINE"
            else
                showMessage "Module compiled... Ok" "NEWLINE"

                if [ -e "$2" ]; then
                    needpath=$(echo  $2)
                else
                    needpath=$(echo  "/etc/httpd")
                fi
                
                if [ -e "$needpath/conf.d" ]; then
                    showMessage "Check for conf.d... Ok" "NEWLINE"
                else
                    mkdir "$needpath/conf.d"
                    showMessage "Check for conf.d...Created Ok" "NEWLINE"
                fi
                
                (cat "$5") > "$needpath/conf.d/$5"
                showMessage "Config file for module $1 was successfully created... Ok" "NEWLINE"
                return 1
                                
            fi
 
        fi
        return 0

}
function installModule_sucgid {
	pathtosrc=$(getPathToFile "$1.tar.gz")
	if [[ $? == 0 ]]; then
	    showMessage "$FAILED Cannot find $1" "NEWLINE"
	else
 
	    cmakeSorce_sucgid $pathtosrc $1    
	    if [[ $? != 0 ]]; then
    		showMessage "Linking error $1" "NEWLINE"
	    else
    		showMessage "Module compiled... Ok" "NEWLINE"

		if [ -e "$2" ]; then
		    needpath=$(echo  $2)
		else
		    needpath=$(echo  "/etc/httpd")
		fi
		
		if [ -e "$needpath/conf.d" ]; then
		    showMessage "Check for conf.d... Ok" "NEWLINE"
		else
		    mkdir "$needpath/conf.d"
		    showMessage "Check for conf.d...Created Ok" "NEWLINE"
		fi
		
		(echo -e "<IfModule !mod_fcgid.c>\n LoadModule $6 $4/$3.so\n</IfModule>" && cat "$5") > "$needpath/conf.d/$5"
		showMessage "Config file for module $1 was successfully created... Ok" "NEWLINE"
		return 1
		    		
	    fi
 
	fi
        return 0

}

function escapeString {
    internal_var=$(echo $1 | sed s/"\/"/"%"/g )
    internal_var=${internal_var//"%"/"\/"}
    internal_var=${internal_var//"."/"\."}
    echo "$internal_var"
}

function clearAll {
    echo -ne '\E[44;31m'"\033[1mScripts before_apache_make, before_httpd_restart_tests will be deteletd. Are you sure? [y/n] \033[0m"
    tput sgr0
    read answ
    if [ "$answ" == "y" ];then
     rm -f "$hook" "$hook_fcgid"
     echo "1) $hook & $hook_fcgid deleted"
     rm -f /usr/local/apache/conf/conf.d/$CONFFILE_fcgid /usr/local/apache/conf/conf.d/$CONFFILE2 /usr/local/apache/conf/conf.d/$CONFFILE_sucgid /usr/local/apache/conf/conf.d/sumodcgid.conf
     echo "2) $CONFFILE_fcgid & $CONFFILE2 & $CONFFILE_sucgid & sumodcgid.conf deleted"
     if [ -e "$pathtoconfig1/main.default" ];then
       cfg=$(echo -n "$pathtoconfig1")
     else
       if [ -e "$pathtoconfig3/main.default" ];then
        cfg=$(echo -n "$pathtoconfig3")
       else
        cfg=""
       fi
     fi
     if [ -n "$cfg" ];then
        sed 's#SuexecEnableLVE on# #g' "$cfg/main.default" > main.default
        mv main.default $cfg
        echo "3) SuexecEnableLVE deleted"
     fi
    else
     echo ""
     echo "Aborted by user"
     exit 1
    fi
    echo ""
}

function createTemplateBckp {
    if [ -e "$pathtoconfig1/main.default" ];then
	if [ ! -e "$pathtoconfig1/main.def.bak" ];then
	    cp $pathtoconfig1/main.default $pathtoconfig1/main.def.bak
	    echo "Backup copy of $pathtoconfig1/main.default successfully created"
	fi
    fi
    
    if [ -e "$pathtoconfig3/main.default" ];then
	if [ ! -e "$pathtoconfig3/main.def.bak" ];then
	    cp $pathtoconfig3/main.default $pathtoconfig3/main.def.bak
	    echo "Backup copy of $pathtoconfig3/main.default successfully created"
	fi
    fi
    
}
function lynxInstall {
    islynx=$(get_command "lynx")
    if [ $? != 0 ] ;then
     yum reinstall lynx -y
     islynx=$(get_command "lynx")
     if [ $? != 0 ] ;then
       echo "Can't find lynx"
       exit 1
     fi 
    fi
    res=$($islynx -dump $FTPSERVERCONF/install-lve)
    echo "$res" | grep VERSION_OF_SCRIPT
    return $?
}
                                                            
function checkVersion {
 isRPMInstall "lynx" "Check for lynx: "
 if [[ $? == 0 ]]; then
    yumInstall "lynx"
    isRPMInstall "lynx" "Check for lynx: "
    if [[ $? == 0 ]]; then
	echo "Can't get lynx. Script execution failed" 
	exit 1
    fi
    echo "Check lynx operation successfull"
 fi
 ver_recv=$(lynxInstall)
 is_p=$(echo "$ver_recv" | grep "$VERSION_OF_SCRIPT" | sed s/"\n"//g)
 if [ "$is_p" == "VERSION_OF_SCRIPT=\"$VERSION_OF_SCRIPT\"" ];then
     echo "Check script version: OK"
 else 
     echo -e "This version of install-lve is outdated\n Current version $VERSION_OF_SCRIPT\n Please download new version:\n 1) rm -f install-lve\n 2) wget $FTPSERVERCONF/install-lve\n 3) sh install-lve -a"
     exit 1
 fi
}
function checkForWhich {
 which lynx &>/dev/null
 if [[ $? != 0 ]]; then
  echo "Command: which is not installed. Preparing for installation..."
  yumInstallUpd "which"
  isRPMInstall "which" "Check for which: "
    if [[ $? == 0 ]]; then
	echo "Can't get which. Script execution failed" 
	exit 1
    fi
    echo "Check which operation successfull"
 fi
}

clear
checkForWhich
checkVersion
createTemplateBckp

#Get script options
while getopts "hlmarfqdcktuepwo" Option
do
  case $Option in
     h ) 
        echo "Script to install mod_hostinglimits"
        echo "key h - help message"
        echo "key l - install mod_hostinglimits"
        echo "key m - install mod_hostinglimits hook"
        echo "key r - rebuild httpd.conf (cPanel)"
        echo "key f - force install"
        echo "key q - quiet install (without yum,wget,rpm info)"
        echo "key d - show debug info"
        echo "key a - install mod_hostinglimits, hooks, rebuld httpd.conf and etc"
        echo "key c - install mod_fcgid module"
        echo "key k - install mod_fcgid hook"
        echo "key e - clear all changes from easyapache, apache will revert back to pre-LVE state after recompile"
        echo "key p - install Apache and mod_cgid patch"
#        echo "key w - type of apache is Worker"
#       echo "key o - type of apache is prefork"
        exit 0
     ;;
     l ) need_install_hostinglimits="install";;
     m ) need_hosting_hook="install";;
     r ) need_rebuild_conf="install";;
     f ) force="force";;
     q ) quite="quite";;
     d ) debug="debug";;
     c ) need_install_fcgid="install";;
     k ) need_hosting_fhook="install";;
     t ) need_install_sucgid="install";;
     u ) need_sucgid_hook="install";;
     e ) 
         clearAll
         need_rebuild_conf="install"
     ;;
     w ) is_set_type_apache="Worker";;
     o ) is_set_type_apache="Prefork";;
     p ) need_modcgid_patch="install";;
     a ) need_hosting_fhook="install"
        need_install_hostinglimits="install"
        need_hosting_hook="install"
        need_rebuild_conf="install"
        need_install_fcgid="install"                                                                                                                                        
	need_hosting_fhook="install" 
        need_modcgid_patch="install"
     ;;
  esac
done
shift $(($OPTIND - 1))
                               
#------------------------------------------------------------------------------

#Check for apache version and type
showMessage "Get Apache version(cPanel): " 
whereapache=$(which apachectl 2>/dev/null)

if [ -z "$whereapache" ]; then 
 whereapache="/usr/local/apache/bin/apachectl"
 if [ -e "$whereapache" ]; then
  ver="1.0"
  else
   showMessage "$FAILED  No Apache installed" "NEWLINE"
   exit 1
 fi
fi

versionnumber=$($whereapache -v)
ver=${versionnumber:23:3}


if [ "a$ver" == "a2.0" ]; then
    apver="2"
  else
  if [ "a$ver" == "a2.2" ]; then
    apver="2_2"
   else
    apver="1_3"
  fi
fi

showMessage $apver "NEWLINE"

if [[ "$apver" == "2_0" ]]; then
    echo "Apache 2.0 is not supported, please, update your Apache to 2.2 version or 1.3 version" "NEWLINE"
    exit 1
fi

apache_prefork=$($whereapache -V | grep -i prefork | grep -v "[/\\]")                                                                                        
apache_worker=$($whereapache -V | grep -i worker | grep -v "[/\\]")
apache_event=$($whereapache -V | grep -i event | grep -v "[/\\]")

if [ -n "$is_set_type_apache" ];then
  if [ "$is_set_type_apache" == "Worker" ];then
    apache_prefork=""
    apache_worker="Worker"
    apache_event=""
  else
    if [ "$is_set_type_apache" == "Event" ];then
     apache_prefork=""
     apache_worker=""
     apache_event="Event"
    else
     apache_prefork="Prefork"
     apache_worker=""
     apache_event=""
    fi
  fi    
fi

if [ -z "$apache_prefork" -a -z "$apache_worker" -a -z "$apache_event" ]; then
   showMessage "$FAILED  Apache must be build with MPM prefork, worker or event" "NEWLINE"
   exit 1
fi

if [ -n "$apache_prefork" -a -n "$apache_worker" ]; then
   showMessage "$FAILED Apache prefork and worker detection error" "NEWLINE"
   exit 1
fi

if [ -n "$apache_prefork" -a -n "$apache_event" ]; then
   showMessage "$FAILED Apache prefork and event detection error" "NEWLINE"
   exit 1
fi

if [ -n "$apache_worker" -a -n "$apache_event" ]; then
   showMessage "$FAILED Apache worker and event detection error" "NEWLINE"
   exit 1
fi
#------------------------------------------------------------------------------
#Prepare for installation
showMessage "Download required packages:" "NEWLINE"

#Check for wget
isRPMInstall "wget" "Check for wget: "
if [[ $? == 0 ]]; then
    yumInstall "wget"
    isRPMInstall "wget" "Check for wget: "
    if [[ $? == 0 ]]; then
	showMessage "Cannot find wget. Script execution failed" "NEWLINE"
	exit 1
    fi
    showMessage "Operation completed successfully" "NEWLINE"
fi    

echo "CentOS release 5.5 (Final)" > /etc/redhat-release

#------------------------------------------------------------------------------
#LVE-DEVEL

isRPMInstallForce "liblve-devel" "LVE-DEVEL installation: "
if [[ $? == 0 ]]; then
    isRPMInstall "liblve-devel" "Test for liblve-devel rpm"
    if [[ $? == 0 ]]; then
        yumi="yumInstallUpd"
    else
        yumi="yumInstall"
    fi
    "$yumi" "liblve-devel"
    if [[ $? != 0 ]]; then
        showMessage "LVE-DEVEL install $FAILED" "NEWLINE"
        exit 1
    fi
    showMessage "LVE-DEVEL successfully intaslled" "NEWLINE"	
fi

isRPMInstallForce "liblve" "LIBLVE installation: "
if [[ $? == 0 ]]; then
    isRPMInstall "liblve" "Test for liblve rpm"
    if [[ $? == 0 ]]; then
        yumi="yumInstallUpd"
    else
        yumi="yumInstall"
    fi
    "$yumi" "liblve"
    if [[ $? != 0 ]]; then
        showMessage "liblve install $FAILED" "NEWLINE"
        exit 1
    fi
    showMessage "liblve successfully intaslled" "NEWLINE"	
fi


#-----------------------------------------------------------------------------
#Check for cmake

showMessage "Check for cmake: "
iswherecmake=0
iswherecmake=$(whereis cmake)
iswherecmake=${iswherecmake:6}

if [ -z "$iswherecmake" ]; then
    showMessage $FAILED "NEWLINE"
    showMessage "CMake installation in progress" "NEWLINE"
    isyumcmake=$(yum list | grep cmake)
    if [ -z "$isyumcmake" ]; then
	showMessage "Get Cmake from HTTP(source code)"
	wgetInstall "$FTPSERVERSRC/$CMAKESRC"
	if [ $? != 0 ]; then
	{
    	    showMessage $FAILED "NEWLINE"
    	    exit 1
	} fi
	showMessage $OK "NEWLINE"
	
	showMessage "Make cmake from source code: " "NEWLINE"
	pathtorpm=$(getPathToFile "cmake*gz")
	if [[ $? == 0 ]]; then
	    showMessage "$FAILED Can't find cmake sources" "NEWLINE"
	    exit 1
	fi
	makeSource $pathtorpm "cmake*"
	if [[ $? != 0 ]]; then
	    showMessage "$FAILED build cmake error" "NEWLINE"
	    exit 1
	fi
	showMessage $OK "NEWLINE"
    else
	    isRPMInstall "cmake" "Test for cmake rpm"
	    if [[ $? == 0 ]]; then
		yumi="yumInstallUpd"
	    else
		yumi="yumInstall"
	    fi
	    "$yumi" "cmake"
	    if [[ $? != 0 ]]; then
    		showMessage "Cmake install $FAILED" "NEWLINE"
    		exit 1
	    fi
	    showMessage "Cmake successfully installed" "NEWLINE"	
    fi
else
    showMessage "Operation successfully completed" "NEWLINE"
fi
showMessage "Operation successfull" "NEWLINE"


#Modify main template file 
if [ -e "$pathtoconfig1/main.default" ]; then
 sed 's#Include "/usr/local/apache/conf/conf\.d/\*"# #g' "$pathtoconfig1/main.default" | sed 's#<Location /whm-server-status>#Include "/usr/local/apache/conf/conf\.d/\*"\n<Location /whm-server-status>#g'> main.default     
 mv main.default $pathtoconfig1 
 showMessage "File $pathtoconfig1/main.default successfully modified..." "NEWLINE"
else 
 showMessage "ATTENTION!!!! Can't find main.default template" "NEWLINE"
fi

if [ -e "$pathtoconfig3/main.default" ]; then
 sed 's#Include "/usr/local/apache/conf/conf\.d/\*"# #g' "$pathtoconfig3/main.default" | sed 's#<Location /whm-server-status>#Include "/usr/local/apache/conf/conf\.d/\*"\n<Location /whm-server-status>#g'> main.default     
 mv main.default $pathtoconfig3 
 showMessage "File $pathtoconfig3/main.default successfully modified..." "NEWLINE"
else 
 showMessage "ATTENTION!!!! Can't find main.default template" "NEWLINE"
fi

#install hook for upcp
#Check for configs(2)
getFilesFromFtp $HOOKUPCP $FTPSERVERCONF "postupcp.template"
if [[ $? == 0 ]]; then
 exit 1
fi
showMessage "Operation completed successfully..." "NEWLINE"

showMessage "Installing POSTUPCP hook..." "NEWLINE"
if [ -e "/scripts" ];then
 fname=$(escapeString "$FTPSERVERSRC")
 arnm=$(escapeString "$archname2")
 drnm=$(escapeString "$archname2")
 pname=$(escapeString "$pathtoconfig1")
 pcnf2=$(escapeString "$pathtoconfig2")
 pcnf3=$(escapeString "$pathtoconfig3")
 apv=$(escapeString "$apver")
 predata=$(sed -e s/\$OK/\"$OK\"/g -e s/\$FAILED/\"$FAILED\"/g -e s/\$MODLIMSRC/"$MODLIMSRC"/g -e s/\$FTPSERVERSRC/"$fname"/g -e s/\$archname2/"$arnm"/g -e s/\$pathtoconfig1/"$pname"/g -e s/\$pathtoconfig2/"$pcnf2"/g -e s/\$pathtoconfig3/"$pcnf3"/g -e s/\$apver/"$apv"/g $HOOKUPCP)
 if [ -e "$postupcp" ];then
  resultdata=$(cat $postupcp | grep "#CLOUD POSTUPCP INSTALL")
  if [ -z "$resultdata" ]; then
   echo "$predata">>$postupcp
  else
   showMessage "UPCP hook $postupcp already modified..." "NEWLINE"
  fi
 else
  echo "$predata">$postupcp		
  chmod +x $postupcp
 fi
else
 showMessage "Cannot find POSTUPCP hook $hook...$FAILED" "NEWLINE"
fi
showMessage "Operation completed successfully..." "NEWLINE"


#-----------------------------------------------------------------------------
#Check for addition parameters

if [ -n "$need_install_hostinglimits" ]; then
#mod_hostinglimits
 if [ -n "$apache_worker" -a "$apver" == "2_2" -o -n "$apache_prefork" -o -n "$apache_event" -a "$apver" == "2_2" ]; then

    #Check for mod_hostinglimits sources
    getFilesFromFtp $MODLIMSRC $FTPSERVERSRC "mod_hostinglimits"
    if [[ $? == 0 ]]; then
    	exit 1
    fi
    showMessage "Operation completed successfully..." "NEWLINE"

    #Check for configs(1)
    getFilesFromFtp $CONFFILE2 $FTPSERVERCONF "modhostinglimits.conf"
    if [[ $? == 0 ]]; then
    	exit 1
    fi
    showMessage "Operation completed successfully..." "NEWLINE"

    
    check_for_need_condition=1
    path_to=$(getPathToApacheModules "$pathtomodules1" "$pathtomodules2" "$pathtomodules3")
    if [[ $? == 0 ]]; then
	check_for_need_condition=0
    else
	if [ -e "$path_to/mod_hostinglimits.so" -a -z "$force" ]; then
	    check_for_need_condition=0
	fi
    fi

    if [ "$check_for_need_condition" == 0 ]; then
	showMessage "Installation of mod_hostinglimits aborted. Maybe it is already present..." "NEWLINE"
    else
        installModule_hosting $archname2 $needpath1 "mod_hostinglimits" $path_to "modhostinglimits.conf" "hostinglimits_module"
    fi
    showMessage "Operation completed successfully..." "NEWLINE"
 fi
    
fi    


if [ -n "$need_hosting_hook" ]; then
 if [ -n "$apache_worker" -a "$apver" == "2_2" -o -n "$apache_prefork" -o -n "$apache_event" -a "$apver" == "2_2" ]; then
    
    #Check for configs(2)
    getFilesFromFtp $HOOKCONF2 $FTPSERVERCONF "modhosting_hook.template"
    if [[ $? == 0 ]]; then
	exit 1
    fi
    showMessage "Operation completed successfully..." "NEWLINE"

    showMessage "Installing EasyApache hook for mod_hostinglimits..." "NEWLINE"
    if [ -e "/scripts" ];then
	fname=$(escapeString "$FTPSERVERSRC")
	arnm=$(escapeString "$archname2")
	drnm=$(escapeString "$archname2")
	pname=$(escapeString "$pathtoconfig1")
	pcnf2=$(escapeString "$pathtoconfig2")
	pcnf3=$(escapeString "$pathtoconfig3")
	apv=$(escapeString "$apver")
	predata=$(sed -e s/\$OK/\"$OK\"/g -e s/\$FAILED/\"$FAILED\"/g -e s/\$MODLIMSRC/"$MODLIMSRC"/g -e s/\$FTPSERVERSRC/"$fname"/g -e s/\$archname2/"$arnm"/g -e s/\$pathtoconfig1/"$pname"/g -e s/\$pathtoconfig2/"$pcnf2"/g -e s/\$pathtoconfig3/"$pcnf3"/g -e s/\$apver/"$apv"/g hook2.conf)
	if [ -e "$hook" ];then
	    resultdata=$(cat $hook | grep "#CLOUD HOOKLIMIT INSTALL")
	    if [ -z "$resultdata" ]; then
		echo "$predata">>$hook
	    else
		showMessage "EasyApache hook $hook already modified..." "NEWLINE"
	    fi
	else
	    echo "$predata">$hook		
	    chmod +x $hook
	fi
    else
	showMessage "Cannot find EasyApache hook $hook...$FAILED" "NEWLINE"
    fi
    showMessage "Operation completed successfully..." "NEWLINE"
 fi
fi

#-----------------------------------------------------------------------------
#Check for addition parameters

if [ -n "$need_install_fcgid" -a "$apver" == "2_2"  ]; then
#mod_hostinglimits

    #Check for mod_hostinglimits sources
    getFilesFromFtp $MODULESRC_fcgid $FTPSERVERSRC "mod_fcgid"
    if [[ $? == 0 ]]; then
        exit 1
    fi
    showMessage "Operation completed successfully..." "NEWLINE"

    check_for_need_condition=1
    path_to=$(getPathToApacheModules "$pathtomodules1" "$pathtomodules2")
    if [[ $? == 0 ]]; then
        check_for_need_condition=0
    else
        if [ -e "$path_to/mod_fcgid.so" -a -z "$force" ]; then
            check_for_need_condition=0
        fi
    fi

    if [ "$check_for_need_condition" == 0 ]; then
        showMessage "Installation of mod_fcgid aborted. Maybe it is already present..." "NEWLINE"
    else
    
        installModule_fcgid $MODULESRC_fcgid $needpath1 "mod_fcgid" $path_to "mod_fcgid.conf" "fcgid_module"
  
    fi
    showMessage "Operation completed successfully..." "NEWLINE"
    
fi    

#------------------------------------------------------------------------------
#hook installation

if [ -n "$need_hosting_fhook" -a "$apver" == "2_2" ]; then

    showMessage "Installing EasyApache hook for mod_fcgid..." "NEWLINE"
    if [ -e "/scripts" ];then
        fname=$(escapeString "$FTPSERVERSRC")
        arnm=$(escapeString "$MODULESRC_fcgid")
        arnm2=$(escapeString "$ARCHNAME")
        pcnf1=$(escapeString "$pathtoconfig1")
        pcnf2=$(escapeString "$pathtoconfig2")
        path_to=$(getPathToApacheModules "$pathtomodules1" "$pathtomodules2")
        ptmod=$(escapeString "$path_to")
        apv=$(escapeString "$apver")

predata=$(sed -e s/\$OK/\"$OK\"/g -e s/\$FAILED/\"$FAILED\"/g -e s/\$MODULESRC/"$MODULESRC_fcgid"/g -e s/\$FTPSERVERSRC/"$fname"/g -e s/\$archname1/"$arnm"/g -e s/\$archname2/"$arnm2"/g -e s/\$pathtoconfig1/"$pcnf1"/g -e s/\$pathtoconfig2/"$pcnf2"/g -e s/\$apver/"$apv"/g -e s/\$pathtomod/"$ptmod"/g mod_fcgid.hook)


        if [ -e "$hook_fcgid" ];then
            resultdata=$(cat $hook_fcgid | grep "#CLOUD MOD_FCGID INSTALL")
            if [ -z "$resultdata" ]; then
                echo "$predata">>$hook_fcgid
            else
                showMessage "EasyApache hook $hook already modified..." "NEWLINE"
            fi
        else
            echo "$predata">$hook_fcgid               
            chmod +x $hook_fcgid
        fi
    else
        showMessage "EasyApache hook $hook not found...$FAILED" "NEWLINE"
    fi
    showMessage "Operation completed successfully..." "NEWLINE"
fi


#-----------------------------------------------------------------------------
#Check for addition parameters

if [ -n "$need_install_sucgid" -a -n "$apache_worker" -a "$apver" == "2_2" -o  -n "$need_install_sucgid" -a -n "$apache_event" -a "$apver" == "2_2" ]; then
#mod_sucgid

    #Check for mod_sucgid sources
    getFilesFromFtp $MODULESRC_sucgid $FTPSERVERSRC "mod_sucgid"
    if [[ $? == 0 ]]; then
	exit 1
    fi
    showMessage "Operation successfull..." "NEWLINE"

    #Check for configs(1)
    getFilesFromFtp $CONFFILE_sucgid $FTPSERVERCONF "modsucgid.conf"
    if [[ $? == 0 ]]; then
	exit 1
    fi
    showMessage "Operation successfull..." "NEWLINE"

    
    check_for_need_condition=1
    path_to=$(getPathToApacheModules "$pathtomodules1" "$pathtomodules2")
    if [[ $? == 0 ]]; then
	check_for_need_condition=0
    else
	if [ -e "$path_to/mod_sucgid.so" -a -z "$force" ]; then
	    check_for_need_condition=0
	fi
    fi

    if [ "$check_for_need_condition" == 0 ]; then
	showMessage "Installation of mod_sucgid aborted. Maybe it already presents..." "NEWLINE"
    else
    
        installModule_sucgid $ARCHNAME_sucgid $needpath1 "mod_sucgid" $path_to "$CONFFILE_sucgid" "sucgid_module"
  
    fi
    showMessage "Operation successfull..." "NEWLINE"
    
fi    

if [ -n "$need_install_sucgid" -a -n "$apache_worker" -a "$apver" == "2_2" -o  -n "$need_install_sucgid" -a -n "$apache_event" -a "$apver" == "2_2" ]; then

    #Check for configs(2)
    getFilesFromFtp $HOOKFILE_sucgid $FTPSERVERCONF "mod_sucgid_hook.template"
    if [[ $? == 0 ]]; then
	exit 1
    fi
    showMessage "Operation successfull..." "NEWLINE"
    
    showMessage "EasyApache hook for mod_sucgid installing..." "NEWLINE"
    if [ -e "/scripts" ];then
	fname=$(escapeString "$FTPSERVERSRC")
	arnm=$(escapeString "$MODULESRC_sucgid")
	drnm=$(escapeString "$ARCHNAME_sucgid")
	pname=$(escapeString "$pathtoconfig1")
	apv=$(escapeString "$apver")

	predata=$(sed -e s/\$OK/\"$OK\"/g -e s/\$FAILED/\"$FAILED\"/g -e s/\$MODULESRC/"$MODULESRC_sucgid"/g -e s/\$FTPSERVERSRC/"$fname"/g -e s/\$archname1/"$arnm"/g -e s/\$pathtoconfig1/"$pname"/g -e s/\$dirname/"$drnm"/g $HOOKFILE_sucgid)
	if [ -e "$hook" ];then
	    resultdata=$(cat $hook | grep "#CLOUD HOOK INSTALL")
	    if [ -z "$resultdata" ]; then
		echo "$predata">>$hook
	    else
		showMessage "EasyApache hook $hook already modified..." "NEWLINE"
	    fi
	else
	    echo "$predata">$hook		
	    chmod +x $hook
	fi
    else
	showMessage "EasyApache hook $hook not find...$FAILED" "NEWLINE"
    fi
    showMessage "Operation successfull..." "NEWLINE"    
  
fi

#------------------------------------------------------------------------------
#Install Apache and mod_cgid patch

if [ -n "$need_modcgid_patch" -a "$apver" == "2_2" -a -n "$apache_worker" ];then
    #Check for configs(2)
    getFilesFromFtp $HOOKFILE_cgid $FTPSERVERCONF "patch_mcgid.template"
    if [[ $? == 0 ]]; then
	exit 1
    fi
        
    showMessage "Installing EasyApache hook for mod_cgid..." "NEWLINE"
    if [ -e "/scripts" ];then
	fname=$(escapeString "$FTPSERVERSRC")
	arnm=$(escapeString "$PATCH_NAME")
	pname=$(escapeString "$pathtoconfig1")
	pc=$(escapeString "$needpath1")
	predata=$(sed -e s/\$OK/\"$OK\"/g -e s/\$FAILED/\"$FAILED\"/g -e s/\$FTPSERVERSRC/"$fname"/g -e s/\$patch_name/"$arnm"/g -e s/\$pathtoconfig1/"$pname"/g -e s/\$PATH_TO_CONFIG/"$pc"/g $HOOKFILE_cgid)
	
	if [ -e "$hook_fcgid" ];then
	    resultdata=$(cat $hook_fcgid | grep "#CLOUD MCGIDPATCH INSTALL")
	    if [ -z "$resultdata" ]; then
		echo "$predata">>$hook_fcgid
	    else
		showMessage "EasyApache hook $hook_fcgid already modified..." "NEWLINE"
	    fi
	else
	    echo "$predata">$hook_fcgid		
	    chmod +x $hook_fcgid
	fi
	
        echo -e '\E[44;31m'"\033[1mATTENTION!!! To enable LVE for CGI scripts, re-compile Apache via EasyApache.\033[0m"
        echo 'Use perl /scripts/easyapache --profile=<profile name> for example: _last_success.yaml - profiles from directory /var/cpanel/easy/apache/profile/'
        echo 
        tput sgr0

    else
	showMessage "EasyApache hook $hook_fcgid or $hook not found...$FAILED" "NEWLINE"
    fi
    showMessage "Operation completed successfully..." "NEWLINE"
fi

#------------------------------------------------------------------------------

#rebuild conf file
     

if [ -n "$need_rebuild_conf" ]; then
                               
    if [ -e "$rebuildscript" ]; then
	"$rebuildscript"
	showMessage "Rebuilding conf ok..." "NEWLINE"
    else
	showMessage "Rubuilding conf faild..." "NEWLINE"
    fi

fi


exit 0
