#! /bin/bash
#
# chkconfig: 35 99 12
# HyperScale  Bring up/down  HyperScale 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
}

disable_ipv6()
{
    #Disable IPV6
    sysctl -w net.ipv6.conf.all.disable_ipv6=1
    sysctl -w net.ipv6.conf.default.disable_ipv6=1
}

activate_mdns()
{
    #Get the list of all active network interfaces. From this list pick the first 
    #interface with an active link and assign it an IP address using avahi-autoip.
    [ -e "$cvma/cvnwconfigmgr.py" ] && excmd_no_err "$cvma/cvnwconfigmgr.py exportavahi"

    #Assuming that avahi provided IP address is assigned to one of the interfaces we will go and restart avahi-daemon
    systemctl restart avahi-daemon
}

create_nwintfx_file()
{
    [ -e "$cvma/cvcreate_nwintfx.py" ] && excmd_no_err "$cvma/cvcreate_nwintfx.py"
}


create_nwintfx_list()
{
    #Check if the file /etc/nwinfx exists. If not we will add the list of network interfaces to this file.
    if [ ! -e "/etc/nwintfx" ]; then
        for intfx in $(ifconfig | grep flags | awk '{print $1}' | cut -d ':' -f 1)
        do
            echo $intfx >> /etc/nwintfx
        done
    fi
}


mount_ws_dav_vm()
{
    if $(grep -qw '/ws/dav_vm' /etc/fstab) && ! $(systemctl status libvirtd 2>&1 > /dev/null); then $(mount /ws/dav_vm; systemctl start libvirtd); 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_irqbalance()
{
   excmd_no_err "/usr/sbin/irqbalance"
}

set_bonding_opts()
{
    /opt/commvault/MediaAgent/cvavahi.py set_nwbond_mode
}

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)
        create_nwintfx_file
        rc=0
        nw_intfx_list=""
        set_bonding_opts
        create_nwintfx_list
        if [ -e /etc/nwintfx ]; then
            nw_intfx_list=`cat /etc/nwintfx`
            for intfx in $nw_intfx_list; do
                /usr/sbin/ifup $intfx
            done
        fi   
        # Enable firewall only if the registry is enabled
        result="N"
        result=$(getregkey "/etc/CommVaultRegistry/Galaxy/Instance001/MediaAgent/.properties" "sHSEnableFirewall")
        if [ "${result}" = "Y" ] || [ "${result}" = "y" ]; then
            enable_firewalld
        else
            disable_firewalld
        fi
        mount_ws_dav_vm
        activate_mdns
        resume_upgrade
        enable_irqbalance
        check_enable_selinux
        ;;
  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
