#! /bin/bash
#
# chkconfig: 35 99 12
# frel Bring up/down  cvplatform service
#

# Source function library.

CWD=$(pwd)
rc=0
cvhome=""
cvbase=""
cvma=""

#Read a key from commvault registry file
getregkey()
{
    registryfile=$1
    registrykey=$2

    cat ${registryfile} | while read line
    do
        key=`echo $line | cut -d' ' -f1`
        if [ "$key" = "$registrykey" ]; then
            echo $line | awk '{print $2}'
            break
        fi
    done
}

setenv()
{
    #Get the commvault home directory
    cvhome=`getregkey /etc/CommVaultRegistry/Galaxy/Instance001/Base/.properties "dGALAXYHOME"`   
    cvbase="$cvhome/Base"
    cvma="$cvhome/MediaAgent"   
    export LD_LIBRARY_PATH=$cvbase:$cvma:/lib/:/lib64:/usr/lib:/usr/lib64:$LD_LIBRARY_PATH
    export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
}

excmd()
{
    command="$*"
    ${command}
    ret=$?  
    if [ $ret != 0 ]; then
         echo "Execution of ${command} failed with return code:$ret"
         exit 1 
    fi 
}

excmd_no_err()
{
    command="$*"
    ${command}
    ret=$?  
    if [ $ret != 0 ]; then
         echo "Execution of ${command} failed with return code:$ret"
    fi 
}

resume_upgrade()
{
    remotecache=`grep ^sHyperScaleRemoteCache /etc/CommVaultRegistry/Galaxy/Instance001/MediaAgent/.properties | awk '{print $2}'`
    remotecache=`echo "$remotecache" | awk '{print tolower($0)}'`
    upgpending=`grep ^sHyperScaleUpgradePending /etc/CommVaultRegistry/Galaxy/Instance001/MediaAgent/.properties | awk '{print $2}'`
    upgpending=`echo "$upgpending" | awk '{print tolower($0)}'`
    cvmgrreboot=$(grep ^sHyperScaleManagerResumeAfterReboot /etc/CommVaultRegistry/Galaxy/Instance001/MediaAgent/.properties | awk '{print $2}')
    if [ "x$remotecache" = "xyes" -a "x$upgpending" = "xyes" ];then
        /opt/commvault/MediaAgent/cvupgradeos.py -resumeAfterReboot
    elif [ -e "$cvmgrreboot" ]; then
        echo "HyperScale resume after reboot with input file: $cvmgrreboot."
        /opt/commvault/MediaAgent/task_manager/cvmanager.py "$cvmgrreboot"
    fi
}

addregkey()
{
    regfile=$1
    key=$2
    value=$3
    key_present=`grep ^$key $regfile`

    #Remove the key from .properties file if already present, and then add
    if [ "x$key_present" != "x" ];then
        cat $regfile | grep -v ^$key > ${regfile}.bak
        mv ${regfile}.bak $regfile
   fi
    echo "$key $value" >> $regfile
}

deleteregkey()
{
    regfile=$1
    key=$2
    key_present=`grep ^$key $regfile`

    #Remove the key from .properties file if already present
    if [ "x$key_present" != "x" ];then
        cat $regfile | grep -v ^$key > ${regfile}.bak
        mv ${regfile}.bak $regfile
   fi
}


check_enable_selinux()
{
    se_linux_enabled=`getregkey /etc/CommVaultRegistry/Galaxy/Instance001/MediaAgent/.properties "sRestartCVSecurity"`
    if [ "$se_linux_enabled" = "Yes" ]; then
        /opt/commvault/MediaAgent/cvsecurity.py restart_cv_services -i Instance001
    fi
}

enable_firewalld()
{
   excmd_no_err "systemctl start firewalld"   
   excmd_no_err "systemctl enable firewalld"   
   excmd_no_err "/usr/bin/firewall-cmd --set-default-zone trusted"   
}

disable_firewalld()
{
   excmd_no_err "systemctl stop firewalld"   
   excmd_no_err "systemctl disable firewalld"   
}

# See how we were called.
setenv
case "$1" in
  start)
        resume_upgrade
        ;;
  stop)
        ;;
  restart|reload|force-reload)
        cd "$CWD"
        $0 stop
        $0 start
        rc=$?
    ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
        exit 2
esac

exit $rc
