#! /bin/bash
#
# /etc/init.d/pegasus
#
#   and its symbolic links
#
# Provides: pegasus
# chkconfig: 35 99 10
# description: OpenPegasus CIMOM
#

# source function library

. /etc/init.d/functions

export PEGASUS_HOME=/var/pegasus
export LD_LIBRARY_PATH=${PEGASUS_HOME}/lib:/lib:/usr/lib:/usr/lib/vmware/lib:/usr/lib/vmware/hostd:/lib:/usr/lib

export PEGLVL=$(( ${PEGLVL} + 1 ))
QUEUE_DIR=${PEGASUS_HOME}/vmware/install_queue
watchdog=/usr/bin/vmware-watchdog

subsys=pegasus

# Pegasus specific command line options
OPTIONS="daemon=false"

CIMSERVER=${PEGASUS_HOME}/bin/cimserver
MSG="Pegasus CIMOM (cimserver)"

start() {
   echo -n "Starting ${MSG}..."

   [ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys
   touch /var/lock/subsys/$subsys

   $watchdog -s cimserver -u 60 -q 5 $CIMSERVER $OPTIONS > /dev/null 2>&1 &
   success
   echo
}

stop() {
   $watchdog -k cimserver > /dev/null 2>&1

   # Check to make sure it's running first
   pid=`pidof -o $$ -o $PPID -o %PPID ${CIMSERVER}`
   if [ -n "$pid" ] ; then
      echo -n "Stopping ${MSG}..."
      # attempt a graceful shutdown
      error_msg=`cimserver -s 2>&1`
      # Give it a few seconds to finish shutting down
      sleep 2
   else
      echo "${MSG} is already stopped."
      return 0
   fi

   # Check to see if it shut down 
   pid=`pidof -o $$ -o $PPID -o %PPID ${CIMSERVER}`
   if [ -n "$pid" ] ; then
      # It's still running, so kill it forcefully
      killproc ${CIMSERVER}
      RETVAL=$?
      echo
      rm -f /var/lock/subsys/$subsys
      return $RETVAL
   else
      success
      echo
      return 0
   fi
}

restart() {
   ## If first returns OK call the second, if first or
   ## second command fails, set echo return value.
   $0 stop  &&  $0 start
}

cleanup_queue() {
   # For DEBUG purposes, save the log, script, and files
   #LAST=`ls ${1}.log* | grep '[^0-9]' | sort -n | tail -1 | cut -f3 -d.`
   #NEXT=$(( ${LAST} + 1 ))
   #if [ -f ${1} ] ; then 
      #mv ${1} ${1}.done.${NEXT}
   #fi
   #if [ -d ${1}_files ] ; then
      #mv ${1}_files ${1}_files.${NEXT}
   #fi
   #if [ -f ${1}.log ] ; then
      #mv ${1}.log ${1}.log.${NEXT}
   #fi

   # For production
   rm -rf ${1} ${1}_files ${1}.log
}
   
process_queue() {
   # Don't infinitely recurse so that the queue scripts
   # can call this startup script if they need to start/stop
   # pegasus during their processing
   if [ "$PEGLVL" -gt 1 ] ; then
      # Recursive call so skip the queue
      # The top level script will handle the queue
      return
   fi
   SCRIPTS=`ls ${QUEUE_DIR} | grep -v '[^0-9]' | sort -n`
   if [ "${SCRIPTS}" = "" ] ; then
      # No scripts to run so just return
      return
   fi

   # There is at least one queued script to process

   # Retry things twice in case we have any failures
   # If it doesn't work on the second try, give up and try
   # again next time this init script is run.
   for retry in 1 2 ; do
      for i in ${SCRIPTS}; do
	 echo -n "Processing ${QUEUE_DIR}/${i} "
	 # Run the script and check the return status to see if it failed
	 if ${SHELL} ${QUEUE_DIR}/${i} >> ${QUEUE_DIR}/${i}.log 2>&1 ; then
	    cleanup_queue ${QUEUE_DIR}/${i}
	    success; echo ""
	 else
	    failure; echo ""
	    echo "ERROR: See log - ${QUEUE_DIR}/${i}.log"
	    # Shutdown the daemon, as it may be hung or unresponsive
	    # leading to our first failure.  (The post install script
	    # will start the daemon if it expects it to be running
	    # in order to do its job
	    stop > /dev/null 2>&1
	    break
	 fi
      done
      SCRIPTS=`ls ${QUEUE_DIR} | grep -v '[^0-9]' | sort -n`
   done
   # Always shut it down to make sure the following start is clean
   stop > /dev/null 2>&1
   sleep 1
}

case "$1" in
   start)
      process_queue
      if $watchdog -r cimserver > /dev/null 2>&1 ; then
	 echo "Pegasus is already running"
      else
	 start
      fi
      ;;
   stop)
      # Don't process the queue on stop so we can shutdown quickly
      stop
      ;;
   restart)
      process_queue
      restart
      ;;
   reload)
      process_queue
      restart
      ;;
   status)
      status ${CIMSERVER}
      ;;
   *)
      echo "Usage: $0 {start|stop|status|restart|reload}"
      exit 1
      ;;
esac
