#!/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 05
# description: discreet backburner server

# 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_SVR_NICE="nice -18"
case "$1" in
'start')
      if [ -f $BB/backburnerServer ]; then
         currentServer="`ps $PSOPT | grep $BB/backburnerServer | grep -v grep | awk '{print $2}'`"
         if [ "$currentServer" != "" ]; then
            echo "The backburner server is (already) running: PID $currentServer"
         else
            # Validate mgr name - must start with alpha or numeric char and not be empty
            case "$DEFAULT_MGR" in
               [a-zA-Z0-9]*)
                  echo -n "Starting the backburner server..."

                  # Clean up ServerJob directory
                  rm -rf $BB/Network/ServerJob/*
                  
		  # 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_server
                  fi

                  # start up the server
                  $BB_SVR_NICE $BB/backburnerServer -m $DEFAULT_MGR > /dev/null 2>&1 & 
                  sleep 3

                  # ensure that it is running
                  if [ "`ps $PSOPT | grep $BB/backburnerServer | grep -v grep`" != "" ]; then
                     echo " OK"
                  else
                     echo " FAILED"
                  fi
                  ;;
               *)
                  echo "WARNING: Cannot start backburner server - manager name in manager.host file must start with alphabetic or numeric char.";
                  ;;
               esac
         fi
      else
         echo "FATAL: Cannot start backburner server as it is not installed.";
      fi

   ;;

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

      echo "Stopping backburner applications..."

      if [[ $OS = Linux* ]]; then
         IPNUM=`uname -m | tr "IP" "ip"`
         OSMESABURN=burn_osmesa_LINUX_`uname -r | cut -d"-" -f1 | cut -d"." -f1,2`_$IPNUM 
         GPUBURN=burn_gpu_LINUX_`uname -r | cut -d"-" -f1 | cut -d"." -f1,2`_$IPNUM
         BURNAPP=burn_LINUX_`uname -r | cut -d"-" -f1 | cut -d"." -f1,2`_$IPNUM
         OLDBURNAPP=burn_LINUX_`uname -r | cut -d"-" -f1 | cut -d"." -f1,2`

         if [ "`ps $PSOPT | grep $BURNAPP | grep -v grep`" != "" ]; then
            killall_with_timeout -KILL $BURNAPP $KILLALLTIMEOUT
         fi
         if [ "`ps $PSOPT | grep $OLDBURNAPP | grep -v grep`" != "" ]; then
            killall_with_timeout -KILL $OLDBURNAPP $KILLALLTIMEOUT
         fi
         if [ "`ps $PSOPT | grep $OSMESABURN | grep -v grep`" != "" ]; then
            killall_with_timeout -KILL $OSMESABURN $KILLALLTIMEOUT
         fi
         if [ "`ps $PSOPT | grep $GPUBURN | grep -v grep`" != "" ]; then
            killall_with_timeout -KILL $GPUBURN $KILLALLTIMEOUT
         fi
      fi

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

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

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