#!/usr/bin/env bash
#
# $Id: sfcb.init-redhat.in,v 1.2 2005/06/13 12:50:33 mihajlov Exp $
#
# chkconfig: 35 20 80
#
# description: Small Footprint CIM Broker Service
#
# processname: vami-sfcbd
#
# config: /opt/vmware/etc/sfcb/sfcb.cfg
#
### BEGIN INIT INFO
# Provides:     vami-sfcb
# Required-Start:       vaos
# Required-Stop:
# Default-Start:        3 5
# Default-Stop:
### END INIT INFO


export TEXTDOMAINDIR=/opt/vmware/lib/locale
export TEXTDOMAIN=vami-sfcb

# SD::
export CIMPLE_HOME=/opt/vmware/var/log/cimple

#if the following is true, then we can expect this script to work using
# the redhat style init function.  Otherwise we have to use the generic
# version of the code
prog=vami-sfcbd

if [ -f /etc/rc.d/init.d/functions ]; then

  # Source function library.
  . /etc/init.d/functions

  echo $PATH | grep -q /opt/vmware/sbin ||PATH=/opt/vmware/sbin:$PATH

  start() {
      echo -n `gettext "Starting"` "$prog: "
      if [ "`pidof $prog`" = "" ]
      then		
	  daemon "/opt/vmware/sbin/$prog -d" &&
	  touch "/var/lock/subsys/$prog" &&
	  echo_success && echo "" && return 0 || echo_failure && echo "" && return 1
      else
          echo_success && echo "" && return 0
      fi
  }	

  stop() {
      echo -n `gettext "Shutting down"` "$prog: "
      if [ "`pidof $prog`" != "" ]
      then
          killproc $prog
          RET=$?
          if [ "`pidof $prog`" != "" ]
          then
              sleep 5
              killall -9 $prog > /dev/null 2>&1
              RET=$?
          fi
          echo ""
          [ $RET -eq 0 ] && rm -f /var/lock/subsys/$prog
          return $RET
      else
          echo_success && echo "" && return 0
      fi
  }

  reload() {
      echo -n `gettext "Reloading"` $prog `gettext configuration: "`
      killproc $prog -HUP &&
      return 0 || return 1
  }

  initialize() {
      echo -n `gettext "Initializing"` "$prog: "
      if [ "`pidof $prog`" != "" ]
      then 
          nohup /opt/vmware/share/vami/vami_sfcb_initialize "`pidof $prog`" 1>/dev/null 2>&1 &
      else
          echo_failure
          echo ""
          return 1
      fi
      echo_success
      echo ""
      return 0
  }

  start_and_test() {
    # start sfcb and check for status
    # retry for 10 times
    for (( j=0; j<10; j++ ))
    do
      # start sfcb
      start
      if [ $? -eq 0 ]; then
	      echo -n `gettext "Checking"` $prog `gettext "status: "`
	    
	      # check sfcb status
	      # try every 5 second and timeout after 60 seconds
	      for (( i=0; i<12; i++ ))
	      do
	        # wait for 5 seconds, allow sfcb to start
	        sleep 5

                # test sfcb while running a query
                # the query will timeout in 30 seconds if sfcb is stuck
       	        nohup /opt/vmware/share/vami/vami_sfcb_test 1>/dev/null 2>&1
	        ret=$?
	        if [ $ret -eq 0 ]; then
	          echo_success
	          echo ""
		
	          echo -n `gettext "Testing"` "$prog: "
	          echo_success
	          echo ""
		  return 0
	        fi
	        
	        if [ $ret -eq 2 ]; then
	          # timeout
	          break
	        fi

	        # failed
	        echo -n "."
	      done
	    
	      echo `gettext " restarting"` "$prog."
	      echo_failure
	      echo "" 
      fi

      # stop sfcb
      stop
      sleep 10
    done

    # start sfcb
    start
  
    echo -n `gettext "Testing"` "$prog: "
    echo_failure 
    echo ""
    return 1
  }

  case "$1" in
    start)
	start_and_test
	initialize
	;;
    stop)
	stop
	;;
    status)
	status $prog
	;;
    restart)
    	stop
	start_and_test
	initialize
	;;
    reload)
	reload
	;;
    initialize)
	initialize
	;;
    condrestart)
	[ -f /var/lock/subsys/$prog ] && restart || :
	;;
    *)
	echo `gettext "Usage:"` $prog "{start|stop|status|reload|restart|condrestart|initialize}"
	exit 1
	;;
  esac
  exit $?


else #end of redhat style script
     # this block is the generic script

  echo $PATH | grep -q /opt/vmware/sbin ||PATH=/opt/vmware/sbin:$PATH

	start() {
	    echo -n `gettext "Starting"` "$prog: "
	    if [ "`pidof $prog`" = "" ]
	    then
            /opt/vmware/sbin/$prog -d && echo `gettext "done."` && return 0 || echo `gettext "failed."`
	        return 1
	    else
	        echo `gettext "done."`
	        return 0        
	    fi
	}   
	
	stop() {
	    echo -n `gettext "Shutting down"` "$prog: "
	    if [ "`pidof $prog`" != "" ]
	    then 
	        killall $prog > /dev/null 2>&1 && sleep 5
	            RET=$?
	            killall -9 $prog > /dev/null 2>&1
	            if [ $RET -eq 0 ]; then
	                echo `gettext "done."`
	            else
	                echo `gettext "failed."`
	            fi
	        return $RET
	    else
	        echo `gettext "done."` && return 0
	    fi
	}

  reload() {
	echo -n `gettext "Reloading"` $prog `gettext "configuration: "`
	killall -HUP $prog && echo "done." && return 0 ||
	echo `gettext "failed."`
	return 1
  }

  initialize() {
        echo -n `gettext "Initializing"` "$prog: "
	if [ "`pidof $prog`" != "" ]
	then 
            nohup /opt/vmware/share/vami/vami_sfcb_initialize "`pidof $prog`" 1>/dev/null 2>&1 &
        else
            echo `gettext "failed."`
            return 1
        fi
        echo `gettext "done."`
        return 0
  }

  start_and_test() {
    # start sfcb and check for status
    # retry for 10 times
    for (( j=0; j<10; j++ ))
    do
      # start sfcb
      start
      if [ $? -eq 0 ]; then
	      echo -n `gettext "Checking"` $prog `gettext "status: "`
	    
	      # check sfcb status
	      # try every 5 second and timeout after 60 seconds
	      for (( i=0; i<12; i++ ))
	      do
	        # wait for 5 seconds, allow sfcb to start
	        sleep 5

                # test sfcb while running a query
                # the query will timeout in 30 seconds if sfcb is stuck
       	        nohup /opt/vmware/share/vami/vami_sfcb_test 1>/dev/null 2>&1
	        ret=$?
	        if [ $ret -eq 0 ]; then
		  echo `gettext " done."`
		
		  echo `gettext "Testing"` $prog: `gettext "done."`
		  return 0
	        fi
	        
	        if [ $ret -eq 2 ]; then
	          # timeout
	          break
	        fi

	        # failed
	        echo -n "."
	      done
	    
	      echo `gettext " failed, restarting"` $prog.
      fi

      # stop sfcb
      stop
      sleep 10
    done

    # start sfcb
    start
  
    echo `gettext "Testing"` $prog: `gettext "failed."`
    return 1
  }

  case "$1" in
    start)
	start_and_test
	initialize
	;;
    stop)
	stop
	;;
    status)
	echo -n `gettext "Checking"` $prog `gettext "status: "`
	ps --noheaders -C $prog || echo `gettext "stopped." `
	;;
    restart)
    	stop
	start_and_test
	initialize
	;;
    initialize)
	initialize
	;;
    reload)
	reload
	;;
    *)
	echo `gettext "Usage:"` $prog "{start|stop|status|reload|restart|initialize}"
	exit 1
	;;
  esac
  exit $?

fi # end of generic style script
