#!/bin/sh
# 
# @(#)BegWS :1.0
# @(#)CR    :(c) Copyright 2001-2012 Hewlett-Packard Development Company, L.P.
# @(#)Name  :HP Software Control
# @(#)Ver   :11.10.035
# @(#)FileN :OVCtrl
# @(#)FileV :11.10.035
# @(#)Rtype :Released
# @(#)CBID  :2012-08-22_1931
# @(#)OS    :Linux
# @(#)OSV   :2.6
# @(#)Arch  :x64
# @(#)Bits  :64
# @(#)Desc  :HP Software Control Script
# @(#)Patch :
# @(#)Build :0000
# @(#)EndWS :
# 
#!/bin/sh

# chkconfig: 345 99 1
### BEGIN INIT INFO
# Provides: OVCtrl
# Required-Start: $syslog +pctl
# Required-Stop: $syslog +pctl
# Should-Start: $ALL
# Should-Stop: $ALL
# Default-Start: 3 4 5
# Default-Stop: 0 1 6 
# Description: Start the OVCtrl application
### END INIT INFO

SCRIPT_NAME="$0"
NAMESPACE="ctrl"
ATTRIBUTE="START_ON_BOOT"
ATTRIBUTE_RUN="RUN_PROFILE"
SERVICE_NAME="OVCtrl"
ATTRIBUTE_INT="START_CHK_INTERVAL"

OV_BIN="/opt/OV/bin"

SU_VAR="/bin/su"

           PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin;export PATH;

usage()
{
    echo "USAGE: OVCtrl start|stop|restart|START|STOP|enable|disable|status" 1>&2
    exit 1
}

is_ovc_running()
{
   sleep 1
		    PSCOUNT=`ps -ef | egrep  -e ' ovc -' -e '/ovc -' |  grep -v grep | wc -l`
		    PSCOUNT1=`ps -ef | egrep  -e ' ovc -' |  grep -v grep | wc -l`
		    PSCOUNT2=`ps -ef | egrep  -e ' ovc ' |  grep -v grep | wc -l`
    if [ $PSCOUNT -eq 0  -a  $PSCOUNT1 -eq 0 -a $PSCOUNT2 -eq 0 ]   
    then
        return 0
    else
        return 1
    fi	
}

IsSELinuxEnabled()
{
    [ ! -d /etc/selinux ] && return 1

    [ ! -f /etc/selinux/config ] && return 1

    typeset -i lcount=`grep -c -e '^ *SELINUX=disabled' /etc/selinux/config`
    [ $lcount -ne 0 ] && return 1

    typeset -i lcount=`grep -c -e '^ *SELINUX=enforcing' -e '^ *SELINUX=permissive' /etc/selinux/config`
    [ $lcount -ne 0 ] && return 0
}

start_service()
{
    if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE`" = "true" ]
    then
       if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE_RUN`" = "true" ]
       then
           if IsSELinuxEnabled
           then
               eval '/sbin/runuser - root -c "/opt/OV/bin/ovc -start -boot"'
           else
               /bin/su - root -c "/opt/OV/bin/ovc -start -boot"
           fi
           RETVAL=$?
       else
           PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export PATH;$OV_BIN/ovc -start -boot
           RETVAL=$?
       fi
       [ $RETVAL -eq 0 ] && touch  /var/lock/subsys/OVCtrl
        echo
       sleep 3
        return $RETVAL

    else
        echo "The service $SERVICE_NAME has been disabled. Please run the command '$SCRIPT_NAME enable' to enable it or use '
$SCRIPT_NAME [START|STOP]' to start or stop the service manually."
    fi
}

stop_service()
{
    if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE`" = "true" ]
    then
        if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE_RUN`" = "true" ]
        then
        if IsSELinuxEnabled
	then
           eval '/sbin/runuser - root -c "/opt/OV/bin/ovc -kill"'
        else
           /bin/su - root -c "/opt/OV/bin/ovc -kill"
        fi
           RETVAL=$?
        else
           PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export PATH;$OV_BIN/ovc -kill
           RETVAL=$?
        fi
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/OVCtrl
        echo
        
        return $RETVAL
     
    else
           echo "The service $SERVICE_NAME has been disabled. Please run the command '$SCRIPT_NAME enable' to enable it or use '
$SCRIPT_NAME [START|STOP]' to start or stop the service manually."
    fi
}

restart_service()
{
    if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE`" = "true" ]
    then
        if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE_RUN`" = "true" ]
        then
        if IsSELinuxEnabled
	then
           eval '/sbin/runuser - root -c "/opt/OV/bin/ovc -restart"'
        else
           /bin/su - root -c "/opt/OV/bin/ovc -restart"
        fi
           return $?
        else
           PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export PATH;$OV_BIN/ovc -restart
           return $?
        fi
    else
           echo "The service $SERVICE_NAME has been disabled. Please run the command '$SCRIPT_NAME enable' to enable it or use '
$SCRIPT_NAME [START|STOP]' to start or stop the service manually."
    fi
}

enable_service()
{
    if ! $OV_BIN/ovconfchg -ns $NAMESPACE -set $ATTRIBUTE true
    then
        echo "Unable to set the required settings to enable the service $SERVICE_NAME." 1>&2
        return 1
    else
        echo  "The service $SERVICE_NAME is now enabled and will be started during the next bootup."
        return 0
    fi
}

disable_service()
{
    if ! $OV_BIN/ovconfchg -ns $NAMESPACE -set $ATTRIBUTE false
    then
        echo "Unable to set the required settings to disable the service $SERVICE_NAME." 1>&2
        return 1
    else
        echo  "The service $SERVICE_NAME is now disabled and will not be started during the next bootup."
        return 0
    fi
}
if [ $# -ne 1 ]; then
    usage
fi

targetval=$1
count=1
retval_ovc_chk=1

if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE_INT`" ]
    then
        start_chk_interval=`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE_INT`
    else
        start_chk_interval=180
fi

while [ $retval_ovc_chk -ne 0 ]
do
        if [ $count -gt $start_chk_interval ]
        then
                echo "ovc instance still running ..proceeding further"
        break
        fi
        count=`expr $count + 1`
        is_ovc_running
        retval_ovc_chk=$?
done
 
case $targetval in

    start_msg)
        echo "Starting Control Daemon"
        ;;

    stop_msg)
        echo "Stopping Control Daemon"
        ;;

    'start')
           start_service
        ;;
    'stop')
        stop_service
        ;;

    'restart')
        restart_service
        ;;

    START)

       if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE`" = "true" ]
       then
           if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE_RUN`" = "true" ]
           then
           if IsSELinuxEnabled
	   then
             eval '/sbin/runuser - root -c "/opt/OV/bin/ovc -start -boot"'
           else
             /bin/su - root -c "/opt/OV/bin/ovc -start -boot"
           fi
               exit $?
           else
               PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export PATH;$OV_BIN/ovc -start -boot
              exit $?
           fi
       fi
       ;;

    STOP)
       if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE`" = "true" ]
       then
           if [ "`$OV_BIN/ovconfget $NAMESPACE $ATTRIBUTE_RUN`" = "true" ]
           then
           if IsSELinuxEnabled
	   then
             eval '/sbin/runuser - root -c "/opt/OV/bin/ovc -kill"'
           else
             /bin/su - root -c "/opt/OV/bin/ovc -kill"
           fi
              exit $?
           else
              PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin;export PATH; $OV_BIN/ovc -kill
              exit $?
           fi
       fi
       ;;

    enable)
        enable_service
        exit $?
        ;;
    disable)
        disable_service
        exit $?
        ;;
    status)
        $OV_BIN/ovc -status
        exit $?
        ;;
    *)
        usage
        ;;
esac

