#!/bin/bash

## cloudlinux_ea3_to_ea4 INFO
# Short Description     :convert cPanel EA3 to EA4 under CloudLinux
## cloudlinux_ea3_to_ea4 INFO

#0.8 first switch to stable
version="2.12"
ACCEPT_ALL="n"
SOLO_MARKER="/etc/cloudlinux-edition-solo"
CL_CONTAINER_MARKER="/etc/cloudlinux-container"

_newArgs=( "$@" )
_found_second_iteration=""
for arg in "${_newArgs[@]}"; do
    if [ "$arg" == "--second-iteration" ]; then
	_found_second_iteration="y"
    fi
done
if [ -z "$_found_second_iteration" ]; then
    sh $0 --second-iteration "$@" 2>&1 | sed 's/error/--**warning**--/gI' 
    exit $?
fi

ACTION="help"
IS_MODLSAPI="n"
IS_MODPASS="n"
IS_ALTPHP="n"
IS_CAGEFS="n"
IS_BETA="n"
IS_TOMCAT="n"
ALLOW_NO_BEST=""

timestamp(){
    date +%s | tr -d '\n'
}

OS_VERSION="$(rpm -q --qf %{version} `rpm -q --whatprovides redhat-release` | cut -c 1)"
if [ ! -e /var/log/cloudlinux_ea3_to_ea4.dblog ]; then
    mkdir -p /var/log/cloudlinux_ea3_to_ea4.dblog
fi
tm=$(timestamp)
log="/var/log/cloudlinux_ea3_to_ea4.dblog/cloudlinux_ea3_to_ea4.log.$tm"
cplog="/usr/local/cpanel/logs/packman/migrate_ea3_to_ea4.log.$tm"
CP_LOG_FLAG="--logfile_path"

cagefs_wrapper (){
	if [ -e /usr/sbin/cagefsctl ]; then
		if /usr/sbin/cagefsctl --help | grep wait-lock &>/dev/null ; then
			/usr/sbin/cagefsctl --wait-lock "$@" &>/dev/null
			echo "command: /usr/sbin/cagefsctl --wait-lock $@ ended"
		else
			while true; do
				RESULT=$(/usr/sbin/cagefsctl "$@" 2>&1)
				if [[ $? -ne 0 && "$RESULT" == *"cagefsctl is already running"* ]]; then
					echo "Waiting 5 seconds and try again to start cagefs command: /usr/sbin/cagefsctl $@"
					sleep 5
				else
					echo "command: /usr/sbin/cagefsctl $@ ended"
					break
				fi
			done
		fi
	fi
}

confirm () {
    confirm_custom "Revert back to EA3(y) or stop immediately(N) [y/N]"
}

confirm_custom () {
    echo "$1"
    if [ "$ACCEPT_ALL" == "y" ]; then
        echo "Accepted: Y"
        true
    else
        read -r response
        case $response in
            [yY]) 
                true
                ;;
            *)
                exit 1
                ;;
        esac
    fi
}

print_help ()
{
    cat << EOF >&2
cloudlinux_ea3_to_ea4 [ADDITIONS] ACTIONS
Usage:

  -h, --help                            Print this message
  Actions (required parameter, shows what should script do):
  -c, --convert                         Convert EA3 to EA4
  -r, --revert                          Revert to EA3
  -f, --restore-cpanel-ea4-repo         Restore EA4 native repositories
  -d, --remove-passenger-directives     Remove old mod_passenger directives from /home/* .htaccess files
  -v, --version                         Show current script version
  Additions(optional parameter, adds to action installation of extra components):
  -m, --mod_lsapi                       Install mod_lsapi
  -p, --mod_passenger                   Install alt-mod-passenger
  -a, --altphp                          Install/Update alt-php
  -b, --beta                            Install EA4 packages from beta
  -t, --stable                          Install EA4 packages from stable
  -y, --yes                             Accept all
  ADDITIONS parameters can't be use without ACTIONS
EOF
}

detect_mod_ruid2_installed(){
    is_mod_ruid2=`rpm -q ea-apache24-mod_ruid2`
    if [ $? -eq 0 ] && [ ! -f "${SOLO_MARKER}" ] && [ ! -f "${CL_CONTAINER_MARKER}" ]; then
	echo "You are running mod_ruid2 that doesn't support Memory Limits, CageFS & PHPSelector. Do you want to switch to performance optimized mod_lsapi profile, and get all the benefits of CloudLinux"
	echo "If you want to switch to performance optimized profile, follow next commands:"
	echo "yum erase ea-apache24-mod_ruid2"
	echo "yum install ea-apache24-mod_lsapi liblsapi liblsapi-devel ea-apache24-mod_suexec"
	#confirm_custom "Do you want to switch to performance optimized profile [y/n]"
	#yum_wrapper "erase" "ea-apache24-mod_ruid2"
	#yum_wrapper "install" "ea-apache24-mod_lsapi liblsapi liblsapi-deve ea-apache24-mod_suexecl"
	#if [ -e /usr/bin/switch_mod_lsapi ]; then
	#    /usr/bin/switch_mod_lsapi --setup
	#fi
	#if [ -e /scripts/restartsrv_httpd ]; then
	#    /scripts/restartsrv_httpd 2>&1 | tee -a $log
	#fi
    fi
}

restore_ea4_repo(){
    if [ ! -e /etc/yum.repos.d/EA4.repo ]; then
	wget -O /etc/yum.repos.d/EA4.repo https://securedownloads.cpanel.net/EA4/EA4.repo 2>&1 | tee -a $log
	yum-config-manager --enable EA4 2>&1 | tee -a $log
    fi
}

is_ea4_installed(){
    if [ -e /etc/cpanel/ea4/is_ea4 ]; then
	echo -n "y"
    fi
    echo -n ""
}

get_skip_cloudlinux (){
    if [ -e /scripts/migrate_ea3_to_ea4 ]; then
	RESULT=$(cat /scripts/migrate_ea3_to_ea4 | grep "skip-cloudlinux")
	if [ -n "$RESULT" ]; then
            echo -n "--skip-cloudlinux"
            return
	fi
    fi
    echo -n ""
}

get_exit_codes (){
    if [ -e /scripts/migrate_ea3_to_ea4 ]; then
	RESULT=$(cat /scripts/migrate_ea3_to_ea4 | grep "EXIT CODES")
	if [ -n "$RESULT" ]; then
            echo -n "--exit-codes"
            return
	fi
    fi
    echo -n ""
}

get_is_tomcat (){
    if [ "$IS_TOMCAT" == "y" ]; then
        echo -n "--allow-unsafe-amount-of-tomcat-users"
        return
    fi
    echo -n ""
}

run_migration_script (){
    if [ -e /usr/local/cpanel/3rdparty/bin/perl -a -e /usr/local/cpanel/scripts/migrate_ea3_to_ea4 ]; then
	skip=$(get_skip_cloudlinux)
	ext_codes=$(get_exit_codes)
	is_tomcat=$(get_is_tomcat)
	echo "Will be executed /scripts/migrate_ea3_to_ea4 --run --yes --force $skip" | tee -a $log
	if [ -n "$is_tomcat" ]; then
	    /usr/local/cpanel/3rdparty/bin/perl -x $0 "$skip" "$is_tomcat" "$CP_LOG_FLAG" "$cplog" "$ext_codes"
	else
	    /usr/local/cpanel/3rdparty/bin/perl -x $0 "$skip" "$CP_LOG_FLAG" "$cplog" "$ext_codes"
	fi
	RES=$?
	if [ $RES -ne 0 ] ; then
	    echo "Err execution of /usr/local/cpanel/scripts/migrate_ea3_to_ea4"
	    if [ "$ext_codes" == "--exit-codes" ]; then
		return $RES
	    else
		return 255
	    fi
	fi
	echo <<'__END__' 2>&1
#!/usr/local/cpanel/3rdparty/bin/perl

require "/usr/local/cpanel/scripts/migrate_ea3_to_ea4";

my $num_args = $#ARGV + 1;
my $skp = "";
my $tmk = "";
my $ext_codes = "";
my $cp_log = "";
if ($num_args > 0) {
        foreach (@ARGV) {
            my $data = $_;
            if ($data eq "--skip-cloudlinux"){
               $skp = $data
            }
            if ($data eq "--allow-unsafe-amount-of-tomcat-users"){
               $tmk = $data
            }
            if ($data =~ "migrate_ea3_to_ea4.log") {
               $cp_log = $data
            }
            if ($data eq "--exit-codes"){
               $ext_codes = $data
            }
        }
}

if ($ext_codes eq "") { 
my $orig_provision_error = \&scripts::migrate_ea3_to_ea4::_provision_error;
my $had_provision_error = 0;
{
    no warnings 'redefine';
    *scripts::migrate_ea3_to_ea4::_provision_error = sub {
         $had_provision_error = 1;
         exit(255);
    };
}
}
if ($skp eq "") {
scripts::migrate_ea3_to_ea4::script('--run', '--yes', '--force', '--logfile_path', $cp_log, $tmk) or die "Err during execution scripts::migrate_ea3_to_ea4\n";
} else {
scripts::migrate_ea3_to_ea4::script('--run', '--yes', '--force', '--logfile_path', $cp_log, $tmk, $skp) or die "Err during execution scripts::migrate_ea3_to_ea4\n";
}
if ($had_provision_error) {
        print "Err during execution scripts::migrate_ea3_to_ea4\n";
        scripts::migrate_ea3_to_ea4::exit_testable(255);
        # ea4 failed to install but you should have ea3 again, assuming that everything was able to be setup properly
    }
else {
        print "scripts::migrate_ea3_to_ea4 successfully executed\n";
        scripts::migrate_ea3_to_ea4::exit_testable(0);
        # you should have ea4 now, assuming that everything was able to be setup properly
}

__END__
    else
	echo "Can't find perl or /usr/local/cpanel/scripts/migrate_ea3_to_ea4"
	return 1
    fi
    return 0
}

detect_package_installed (){
    pkg="$1"
    if [ -n "$pkg" ]; then
        /bin/rpm -q "$pkg"
        if [ $? -eq 0 ] ; then
            return 1
        fi
    fi
    return 0
}

detect_version_of_cpanel (){
    if [ -e /usr/local/cpanel/cpanel ]; then
        RES1=`/usr/local/cpanel/cpanel -V | cut -d" " -f1`
        VER1=`echo "$RES1" | cut -d. -f1`
        VER2=`echo "$RES1" | cut -d. -f2`
        
        VERi1=$((VER1-55))
        VERi2=$((VER2-9))
        if [ "$VERi1" -gt "0" ]; then
            echo "OK"
            return
        fi
        if [ "$VERi1" -eq "0" -a "$VERi2" -gt "0" ]; then
            echo "OK"
            return
        fi
        
    fi
    echo "FAILED"
}

print_delimiter (){
    echo ""
    echo ""
    echo ""
    echo ""
    echo ""
    echo ""
    echo ""
    echo ""
    echo ""
    echo ""
    echo ""
    echo ""
}

is_ruid (){
    if [ -e /usr/sbin/httpd ]; then
        RES=`/usr/sbin/httpd -M 2>&1 | grep ruid`
        if [ -n "$RES" ];then
            echo "YES"
            return
        fi
    fi
    echo "NO"
}

yum_wrapper(){
    local ACT=$1
    local LIST=$2
    local EXTRA_PARAMS=$3

    if [ -n "$LIST" -a -n "$ACT" ];then
        yum -y $ACT $LIST $EXTRA_PARAMS 2>&1 | tee -a $log; RET=${PIPESTATUS[0]}
        if [ $RET -ne 0 ] ; then
            yum -y $ACT $LIST $EXTRA_PARAMS 2>&1 | tee -a $log; RET=${PIPESTATUS[0]}
            if [ $RET -ne 0 ] ; then
                yum -y $ACT $LIST $EXTRA_PARAMS 2>&1 | tee -a $log; RET=${PIPESTATUS[0]}
                if [ $RET -ne 0 ] ; then
                    echo "You can find complete log in $log"
                    echo "Err while process installation"
                    exit 1
                fi
            fi
        fi
        return
    fi
    echo "Empty package list. Abnormal situation"
    exit 1
}

IS_NEEDED_CPANEL=`detect_version_of_cpanel`

if [ "$IS_NEEDED_CPANEL" != "OK" ]; then
    echo "cPanel version should be more or equal 11.55.999.66. Installation aborted."
    exit 1
fi

# read the options
TEMP=`getopt -o crmpasvbtufdy --long convert,revert,mod_lsapi,mod_passenger,altphp,help,second-iteration,version,beta,stable,restore-cpanel-ea4-repo,remove-passenger-directives,allow-unsafe-amount-of-tomcat-users,yes,allow-nobest -- "$@"`
eval set -- "$TEMP"

# extract options and their arguments into variables.
while true ; do
    case "$1" in
        --help)
            print_help
            exit 0
            ;;
        -c|--convert) ACTION="convert" ; shift ;;
        -r|--revert) ACTION="revert" ; shift ;;
        -f|--restore-cpanel-ea4-repo) ACTION="restore-cpanel-ea4-repo" ; shift ;;
        -d|--remove-passenger-directives) ACTION="remove-passenger-directives" ; shift ;;
        -m|--mod_lsapi) IS_MODLSAPI="y" ; shift ;;
        -p|--mod_passenger) IS_MODPASS="y" ; shift ;;
        -a|--altphp) IS_ALTPHP="y" ; shift ;;
        -s|--second-iteration) EMPTY_ACTION="" ; shift ;;
        -b|--beta) IS_BETA="y" ; shift ;;
        -u|--allow-unsafe-amount-of-tomcat-users) IS_TOMCAT="y"; shift ;;
        -y|--yes) ACCEPT_ALL="y" ; shift ;;
        -n|--allow-nobest) ALLOW_NO_BEST="--nobest" ; shift ;;
        -t|--stable)
            yum-config-manager --disable cl-ea4-testing 2>&1 | tee -a $log
            yum downgrade ea-* 2>&1 | tee -a $log
            exit 0
            ;;
        -v|--version)
            echo "Version: $version"
            exit 0
            ;;
        --) shift ; break ;;
        -*)
            echo "$0: error - unrecognized option $1" 1>&2
            print_help
            exit 1
            ;;
        *) echo "Internal error!" ; exit 1 ;;
    esac
done

echo "Running version: $version" 2>&1 | tee -a $log

skip=$(get_skip_cloudlinux)
detect_package_installed cagefs
if [ $? -eq 1 ]; then
    IS_CAGEFS="y"
fi
IS_EA4_INSTALLED=$(is_ea4_installed)

migrate_lsapi_conf () {
    OP=$1
    if [ "$OP" == "EA3" ]; then
        if [ -e /usr/local/apache/conf/conf.d/lsapi.conf ]; then
            cp /usr/local/apache/conf/conf.d/lsapi.conf /usr/share/lve/modlscapi/ea3.conf
            echo "EA3 lsapi.conf saved as ea3.conf in /usr/share/lve/modlscapi/" 2>&1 | tee -a $log
        fi
        # accept removing mod_lsapi
        return 0;
    fi

    # Call before install ea-apache24-mod_lsapi so that new lsapi.conf will be saved as lsapi.conf.rpmnew
    if [ "$OP" == "EA4" ]; then
        if [ -e /usr/share/lve/modlscapi/ea3.conf ]; then
            echo "Begin migration lsapi.conf from EA3 into EA4" 2>&1 | tee -a $log
            cp /usr/share/lve/modlscapi/ea3.conf /etc/apache2/conf.d/lsapi.conf
            # Removing deprecated for EA4 lsapi configuration
            sed -i '/LoadModule/d'   "/etc/apache2/conf.d/lsapi.conf"
            sed -i '/lsapi_engine/d' "/etc/apache2/conf.d/lsapi.conf"
            sed -i '/AddHandler/d'   "/etc/apache2/conf.d/lsapi.conf"
            sed -i '/AddType/d'      "/etc/apache2/conf.d/lsapi.conf"
            sed -i '/FilesMatch/d'   "/etc/apache2/conf.d/lsapi.conf"
        fi
        return 0;
    fi

    echo "Migrate EA3 lsapi.conf into EA4 failed: unknown option" 2>&1 | tee -a $log
    return 1;
}

passenger_directive_warning () {
    if grep -Eq '^[[:space:]]*Passenger' "$1"; then
        if [ "$2" == "remove" ]; then
            echo "$(tput setaf 2)"
            grep -E '^[[:space:]]*Passenger' "$1" 2>&1 | tee -a $log
            sed -i '/^[[:space:]]*Passenger/d' "$1"
            echo "$(tput sgr 0)Was removed from "$1"" 2>&1 | tee -a $log
        else
            echo "$(tput setaf 1)"
            echo "WARNING: Remove old mod_passenger directives from "$1":$(tput sgr 0)" 2>&1 | tee -a $log
            grep -E '^[[:space:]]*Passenger' "$1" 2>&1 | tee -a $log
            echo "To remove old mod_passenger directives from the .htaccess files automatically run:" 2>&1 | tee -a $log
            echo "$(tput setaf 2)sh cloudlinux_ea3_to_ea4 --remove-passenger-directives $(tput sgr 0)" 2>&1 | tee -a $log
        fi
    fi
}

export log
export -f passenger_directive_warning

if [ "$ACTION" == "restore-cpanel-ea4-repo" ]; then
    if [ -n "$IS_EA4_INSTALLED" ]; then
	PASSENGER=$(rpm -qa | grep "ea-apache24-mod-alt-passenger")
	echo "Romoving of CloudLinux related packages and CloudLinux EA4 repository" 2>&1 | tee -a $log
	yum_wrapper "erase" "ea-apache24-mod_hostinglimits ea-apache24-mod_lsapi ea-apache24-alt_mod_passenger ea-apache24-mod-alt-passenger"
	yum_wrapper "erase" "cloudlinux-ea4-release"
	echo "Install cPanel's native EA4 packages" 2>&1 | tee -a $log
	restore_ea4_repo
	yum_wrapper "downgrade" "ea-modsec-sdbm-util ea-php* ea-ruby* ea-apache* ea-libcurl ea-libxml2 ea-openssl* ea-libnghttp2* ea-nghttp2 ea-brotli ea-profiles-cpanel ea-cpanel-tools ea-documentroot ea-libmcrypt ea-apr*"
	echo "Delete EA4 CloudLinux related packages" 2>&1 | tee -a $log
	#yum_wrapper "erase" "ea-php*cloudlinux"
	#yum_wrapper "erase" "ea-apache*cloudlinux"
	#yum_wrapper "erase" "ea-*cloudlinux"
	if [ "$OS_VERSION" == "7" ]; then
	    systemctl daemon-reload
	fi
	if [ -e /scripts/restartsrv_httpd ]; then
	    /scripts/restartsrv_httpd 2>&1 | tee -a $log
	fi
	if [ -n "$PASSENGER" ]; then
	    yum_wrapper "install" "ea-ruby24-mod_passenger"
	    find '/home/' -name '.htaccess' -type f -exec bash -c 'passenger_directive_warning "$0"' {} \;
	fi
	echo "End of process" 2>&1 | tee -a $log
    fi
elif [ "$ACTION" == "convert" ]; then
    echo "Enabling of EA4 repository for CloudLinux" 2>&1 | tee -a $log
    yum_wrapper "install" "yum-utils"
    yum localinstall -y http://repo.cloudlinux.com/cloudlinux/EA4/cloudlinux-ea4-release-latest-${OS_VERSION}.noarch.rpm --disablerepo='*' &>> $log

    ENABLED_REPOS=""
    [[ "$IS_BETA" == "y" ]] && ENABLED_REPOS="--enablerepo=cloudlinux-updates-testing,cl-ea4-testing"

    yum_wrapper "update" "yum-rhn-plugin"
    detect_package_installed lvemanager
    if [ $? -eq 1 ]; then
	yum_wrapper "update" "lvemanager"
    fi

    if [ ! -f "${SOLO_MARKER}" ] && [ ! -f "${CL_CONTAINER_MARKER}" ]; then
        if [ "$IS_CAGEFS" == "y" ]; then
	    yum_wrapper "update" "cagefs"
        fi
        if [ "$IS_CAGEFS" == "y" ]; then
	    if [ -e /usr/share/cagefs-skeleton ]; then
                echo "CageFS installed and inited already" &>>$log
	    else
                cagefs_wrapper --init &>>$log
	    fi
        fi
        if [ "$IS_ALTPHP" == "y" ]; then
	    print_delimiter 2>&1 | tee -a $log
	    echo "Begin Installation of alt-php" 2>&1 | tee -a $log
	    yum_wrapper "groupinstall" "alt-php"
        fi
        print_delimiter 2>&1 | tee -a $log
        echo "Checking if mod_lsapi or alt_mod_passenger are installed. There are conflicts with mod_lsapi and mod_passenger for EA4" 2>&1 | tee -a $log
        migrate_lsapi_conf EA3
        yum_wrapper "erase" "alt-mod-passenger mod_lsapi"
    fi

    print_delimiter 2>&1 | tee -a $log
    echo "Begin converting to EA4" 2>&1 | tee -a $log

    if [ -z "$IS_EA4_INSTALLED" ]; then

	echo "EA4 not detected. Starting of migrate_ea3_to_ea4 script" 2>&1 | tee -a $log
	if [ -e /scripts/migrate_ea3_to_ea4 ]; then
	    codes=$(get_exit_codes)
	    if [ "$IS_BETA" == "y" ]; then
		echo "Install EA4 packages from beta" 2>&1 | tee -a $log
		yum-config-manager --enable cl-ea4-testing 2>&1 | tee -a $log
	    fi
	    if [ -f /etc/cagefs/cagefs.mp ] &&
	       grep -m 1 -P "^/usr/local/apache/domlogs$" /etc/cagefs/cagefs.mp &> /dev/null; then
		sed -i ':a;N;$!ba;sI/usr/local/apache/domlogs\nIIg' /etc/cagefs/cagefs.mp
		domlogs_del=1
	    fi
	    run_migration_script 2>&1 | tee -a $log
	    result=${PIPESTATUS[0]}
	    if [[ $domlogs_del == 1 ]]; then
	    	printf "/usr/local/apache/domlogs\n" >> /etc/cagefs/cagefs.mp
		domlogs_del=0
	    fi
	    if [ "$IS_BETA" == "y" ]; then
		yum-config-manager --disable cl-ea4-testing 2>&1 | tee -a $log
	    fi
	    if [ $result -ne 0 ] ; then
		print_delimiter 2>&1 | tee -a $log
		err_code=""
		if [ -z "$codes" ]; then
		    if [ "$IS_BETA" == "y" ]; then
			echo "Install EA4 packages from beta" 2>&1 | tee -a $log
			yum-config-manager --enable cl-ea4-testing 2>&1 | tee -a $log
		    fi
		    if [ -f /etc/cagefs/cagefs.mp ] &&
		       grep -m 1 -P "^/usr/local/apache/domlogs$" /etc/cagefs/cagefs.mp &> /dev/null; then
			sed -i ':a;N;$!ba;sI/usr/local/apache/domlogs\nIIg' /etc/cagefs/cagefs.mp
			domlogs_del=1
		    fi
		    run_migration_script | tee -a $log
		    result=${PIPESTATUS[0]}
		    if [[ $domlogs_del == 1 ]]; then
			printf "/usr/local/apache/domlogs\n" >> /etc/cagefs/cagefs.mp
			domlogs_del=0
		    fi
		    if [ "$IS_BETA" == "y" ]; then
			yum-config-manager --disable cl-ea4-testing 2>&1 | tee -a $log
		    fi
		    if [ $result -ne 0 ] ; then
			echo "You can find complete log in $log"
			echo "Failed while process installation"
			err_code="y"
		    fi
		else
		    if [ $result -eq 10 -o $result -eq 11 -o $result -eq 20 -o $result -eq 31 ]; then
			echo "You can find complete log in $log"
			echo "Failed while process installation"
			exit 1
		    elif [ $result -eq 30 ]; then
			echo "You can find complete log in $log"
			echo "Default profile installed successfully"
		    else
			echo "You can find complete log in $log"
			echo "Failed while process installation"
			err_code="y"
		    fi
		fi
	    fi

	    if [ -n "$err_code" ]; then
		newArgs=( "$@" )
		ARRAY=()
		for arg in "${newArgs[@]}"; do
		    if [ "$arg" != "--convert" -a "$arg" != "-c" ]; then
			ARRAY+=("$arg")
		    fi
		done
		confirm && /usr/bin/sh $0 --revert "${ARRAY[@]}"
		exit 1
	    fi
	else
	    print_delimiter 2>&1 | tee -a $log
	    echo "File /scripts/migrate_ea3_to_ea4 does not exist. Aborted..." 2>&1 | tee -a $log
	    exit 1
	fi
	CHECK_LOG_ERR=`cat $cplog | grep "fatal error"`
	if [ -n "$CHECK_LOG_ERR" ]; then
	    cat $cplog 2>&1 | tee -a $log
	    echo "EasyApache 4 was unable to convert your EasyApache 3 profile, with fatal errors."
	    echo "You can find complete log in $log"
	    echo "Failed while process installation, please contact https://www.cloudlinux.com/support"
	    exit 1
	fi
	CHECK_LOG_UNSUCCESS=`cat $cplog | grep "EasyApache 4 has successfully been installed"`
	if [ -z "$CHECK_LOG_UNSUCCESS" ]; then
	    cat $cplog 2>&1 | tee -a $log
	    echo "/scripts/migrate_ea3_to_ea4 wasn't successful executed for some reasons. See $cplog"
	    echo "You can find complete log in $log"
	    echo "Failed while process installation, please contact https://www.cloudlinux.com/support"
	    exit 1
	fi
	restore_ea4_repo
    fi

    print_delimiter 2>&1 | tee -a $log
    [[ "$IS_BETA" == "y" ]] && echo "Installing EA4 packages from beta" 2>&1 | tee -a $log
    yum_wrapper "update" "ea-*" "${ENABLED_REPOS} ${ALLOW_NO_BEST}"

    if [ ! -f "${SOLO_MARKER}" ] && [ ! -f "${CL_CONTAINER_MARKER}" ]; then
        print_delimiter 2>&1 | tee -a $log
	    echo "Begin Installation of mod_hostinglimits" 2>&1 | tee -a $log
        [[ "$IS_BETA" == "y" ]] && echo "Installing mod_hostinglimits from beta" 2>&1 | tee -a $log
        yum_wrapper "install" "ea-apache24-mod_hostinglimits" "$ENABLED_REPOS"

        if [ "$IS_MODLSAPI" == "y" ]; then
            print_delimiter 2>&1 | tee -a $log
            echo "Begin Installation of mod_lsapi" 2>&1 | tee -a $log
            if [ "$IS_BETA" == "y" ]; then
                echo "Installing mod_lsapi from beta" 2>&1 | tee -a $log
                yum-config-manager --enable cl-ea4-testing 2>&1 | tee -a $log
            fi
            migrate_lsapi_conf EA4
            yum_wrapper "install" "ea-apache24-mod_lsapi liblsapi liblsapi-devel" "${ENABLED_REPOS} ${ALLOW_NO_BEST}"
            /usr/bin/switch_mod_lsapi --setup 2>&1 | tee -a $log
            if [ "$IS_BETA" == "y" ]; then
                yum-config-manager --disable cl-ea4-testing 2>&1 | tee -a $log
            fi
        fi

        if [ "$IS_MODPASS" == "y" ]; then
            print_delimiter 2>&1 | tee -a $log
            echo "Begin Installation of mod_passenger" 2>&1 | tee -a $log
            [[ "$IS_BETA" == "y" ]] && echo "Installing mod_passenger from beta" 2>&1 | tee -a $log
            yum_wrapper "install" "apache24-passenger" "$ENABLED_REPOS"
        fi

        if [ "$IS_CAGEFS" == "y" ]; then
	    print_delimiter 2>&1 | tee -a $log
	    echo "Begin CageFS skeleton updating" 2>&1 | tee -a $log
	    if [ -e /etc/cagefs/cagefs.mp ]; then
	        if [ ! -e /var/www/cgi-bin ]; then
		    sed -i -e '/^\/var\/www\/cgi-bin$/d' /etc/cagefs/cagefs.mp 2>&1 | tee -a $log
	        fi
	    fi
	    if [ ! -e /etc/cagefs/cagefs.mp ]; then
	        cagefs_wrapper --create-mp &>>$log
	    fi
	    cagefs_wrapper --force-update &>>$log
	    cagefs_wrapper --remount-all &>>$log
	    if [ -e /usr/share/cagefs/setup_multiphp_integration ]; then
                /usr/share/cagefs/setup_multiphp_integration
	    fi
        fi
    fi

    print_delimiter 2>&1 | tee -a $log
    echo "Check for RUID and install suexec if needed" 2>&1 | tee -a $log
    RUID=`is_ruid`
    if [ "$RUID" == "NO" ];then
        [[ "$IS_BETA" == "y" ]] && echo "Installing suexec from beta" 2>&1 | tee -a $log
        yum_wrapper "install" "ea-apache24-mod_suexec" "$ENABLED_REPOS"
    fi

    if [ "$OS_VERSION" == "7" ]; then
	systemctl restart httpd
    else
	service httpd restart
    fi

elif [ "$ACTION" == "revert" ]; then

    echo "Revert back to EA3" 2>&1 | tee -a $log
    if [ ! -e /usr/local/apache.ea3 ]; then
        echo "This system never had EasyApache 3. Reverting is not possible." 2>&1 | tee -a $log
        exit 1
    fi
    yum_wrapper "erase" "ea-apache24-mod_hostinglimits ea-apache24-mod_lsapi ea-apache24-alt_mod_passenger"
    yum_wrapper "erase" "cloudlinux-ea4-release"
    /scripts/migrate_ea3_to_ea4 --revert --run --yes --force $skip  2>&1 | tee -a $log; test ${PIPESTATUS[0]} -eq 0
    if [ $? -ne 0 ] ; then
	print_delimiter 2>&1 | tee -a $log
	/scripts/migrate_ea3_to_ea4 --revert --run --yes --force $skip 2>&1 | tee -a $log; test ${PIPESTATUS[0]} -eq 0
	if [ $? -ne 0 ] ; then
            echo "You can find complete log in $log"
            echo "Err while process installation"
            exit 1
	fi
    fi

    if [ "$IS_MODLSAPI" == "y" ]; then
	print_delimiter 2>&1 | tee -a $log
	echo "Begin Installation of mod_lsapi" 2>&1 | tee -a $log
	yum_wrapper "install" "mod_lsapi liblsapi liblsapi-devel" "$ALLOW_NO_BEST"
	/usr/bin/switch_mod_lsapi --setup 2>&1 | tee -a $log
    fi

    if [ "$IS_CAGEFS" == "y" ]; then
	print_delimiter 2>&1 | tee -a $log
	echo "Begin CageFS skeleton updating" 2>&1 | tee -a $log
	if [ -e /etc/cagefs/cagefs.mp ]; then
            if [ ! -e /var/www/cgi-bin ]; then
		sed -i -e '/^\/var\/www\/cgi-bin$/d' /etc/cagefs/cagefs.mp 2>&1 | tee -a $log
            fi
	fi
	if [ ! -e /etc/cagefs/cagefs.mp ]; then
		cagefs_wrapper --create-mp &>>$log
	fi
	cagefs_wrapper --force-update &>>$log
	cagefs_wrapper --remount-all &>>$log
	if [ -e /usr/share/cagefs/setup_multiphp_integration ]; then
            /usr/share/cagefs/setup_multiphp_integration
	fi
    fi

    if [ "$IS_MODPASS" == "y" ]; then
	print_delimiter 2>&1 | tee -a $log
	echo "Begin Installation of mod_passenger" 2>&1 | tee -a $log
	yum_wrapper "install" "alt-mod-passenger"
    fi
    if [ "$OS_VERSION" == "7" ]; then
	systemctl daemon-reload
    fi
    if [ -e /scripts/restartsrv_httpd ]; then
	/scripts/restartsrv_httpd 2>&1 | tee -a $log
    fi
elif [ "$ACTION" == "remove-passenger-directives" ]; then
    find '/home/' -name '.htaccess' -type f -exec bash -c 'passenger_directive_warning "$0" "remove"' {} \;
else
    print_help
    exit 1
fi

print_delimiter 2>&1 | tee -a $log

if [ "$ACTION" == "convert" ]; then
    detect_mod_ruid2_installed
    RUID=`is_ruid`
    if [ "$RUID" == "NO" ];then
	yum_wrapper "install" "ea-apache24-mod_suexec"
    fi
fi

echo "You can find complete log in $log"
exit 0
