#!/usr/bin/bash

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


restore_cloudlinux_cgi() {
  if ! cmp --silent /usr/local/cpanel/whostmgr/docroot/cgi/CloudLinux.cgi /usr/share/l.v.e-manager/cpanel/cgi/CloudLinux.cgi; then
    echo "$1"
    cp -f /usr/share/l.v.e-manager/cpanel/cgi/CloudLinux.cgi /usr/local/cpanel/whostmgr/docroot/cgi/CloudLinux.cgi
  fi
}

# execution via cronjob ?
if [ "$1" = "--cronjob" ]; then
    if /usr/local/cpanel/scripts/upcp-running > /dev/null ; then
        # upcp is running. don't restore lvemanager
        exit 0
    fi
    if cmp --silent /usr/local/cpanel/whostmgr/docroot/cgi/CloudLinux.cgi /usr/share/l.v.e-manager/cpanel/cgi/CloudLinux.cgi; then
        # lvemanager does not need to be restored (lvemanager has not been changed by upcp)
        exit 0
    fi
fi

/usr/share/l.v.e-manager/cpanel/utils/install_module.pl -u
/usr/local/cpanel/bin/register_cpanelplugin /usr/share/l.v.e-manager/cpanel/lveversion/selectphpversion.plugin
/usr/share/l.v.e-manager/install-lvemanager-plugin.py --postupcp

# Run installer to fix possible issues with broken x-ray plugin
if [ -f "/usr/share/lvemanager-xray/plugins/install-xray-plugin.py" ]; then
    /usr/share/lvemanager-xray/plugins/install-xray-plugin.py -i
fi

for theme in /usr/local/cpanel/base/frontend/*; do
    if [ -d ${theme} ]; then
        if [ -e ${theme}/dynamicui.conf ]; then
            if grep stats/resourceusage.html ${theme}/dynamicui.conf > /dev/null 2>&1; then
                rm -f ${theme}/dynamicui/dynamicui_enduserlve.conf > /dev/null 2>&1
            fi
        fi
    fi
done

restore_cloudlinux_cgi 'failed to restore CloudLinux Manager'

# LVEMAN-1139
if [ -x /usr/share/l.v.e-manager/branding_script ]; then
    /usr/share/l.v.e-manager/branding_script
fi

restore_cloudlinux_cgi 'CloudLinux.cgi should not be changed by branding_script! Reverting the changes...'

# LVEMAN-1160: create symlinks for alt-php for scl support for cpanel >= 11.66.0.11
if [ -x /opt/alt/alt-php-config/multiphp_reconfigure.py ]; then
    /opt/alt/alt-php-config/multiphp_reconfigure.py
fi

# reconfigure support linksafe
/usr/bin/cl-linksafe-reconfigure

exit 0
