#!/bin/sh
# Copyright (c) 2003 Autodesk Canada Inc. / Autodesk, Inc. All Rights Reserved.
#
# These coded instructions, statements, and computer programs contain
# unpublished proprietary information written by Autodesk and
# are protected by Federal copyright law. They may not be disclosed
# to third parties or copied or duplicated in any form, in whole or
# in part, without the prior written consent of Autodesk.
#
#set -x
# The following comments are required by Redhat chkconfig
# chkconfig: 345 99 06
# description: discreet backburner manager

# Used to enable chkconfig of the services; these are really started by
# the backburner script


# Source Backburner general functions and definitions
. /etc/init.d/backburner_general

BB_MGR_NICE="nice -0"
case "$1" in
'start')
      if [ -f $BB/backburnerManager ]; then
         currentManager="`ps $PSOPT | grep $BB/backburnerManager | grep -v grep | awk '{print $2}'`"
         if [ "$currentManager" != "" ]; then
            echo "The backburner manager is (already) running: PID $currentManager"
         else
            echo -n "Starting the backburner manager..."

	    # On linux, lock file is needed for the stop script to be run 
	    # automatically when changing run levels (e.g. system shutdown).
	    # Touch the lock file regardless of what succeeds so that the
	    # stop script will be called in case of a partial start up.

	    if [[ $OS = Linux* ]]; then
	       touch /var/lock/subsys/backburner_manager
            fi

            $BB_MGR_NICE $BB/backburnerManager > /dev/null 2>&1 &
            sleep 3

            # ensure that it is running.
            if [ "`ps $PSOPT | grep $BB/backburnerManager | grep -v grep`" != "" ]; then
               echo " OK"
            else
               echo " FAILED"
            fi
         fi
      else
         echo "FATAL: Cannot start backburner manager as it is not installed."
      fi

   ;;

'stop')
      if [ "`ps $PSOPT | grep $BB/backburnerManager | grep -v grep`" != "" ]; then
         echo -n "Stopping backburner manager..."
         killall_with_timeout -TERM $BB/backburnerManager $KILLALLTIMEOUT
         if [ "`ps $PSOPT | grep $BB/backburnerManager | grep -v grep`" != "" ]; then
            echo " FAILED"
         else
            echo " OK"
         fi
      else
         echo "backburner manager not running."
      fi

      if [[ $OS = Linux* ]]; then
      	 rm -f /var/lock/subsys/backburner_manager
      fi
   ;;

'restart')
   $0 stop
   $0 start
   ;;

*)
   echo "usage: $0 {start|stop|restart}"
   ;;
esac
