#!/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 general functions and definitions

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

#
# killall_with_timeout
#
# This function will kill processes using killall.  If the timeout expires and
# the process didn't die and the signal wasn't -KILL it retries with -KILL
#
# arg1: signal to send ex: -INT
# arg2: process name
# arg3: timeout (in seconds)
#

killall_with_timeout() {
   SIGNAL=$1
   SERVERNAME=$2
   SHORTSERVERNAME=$SERVERNAME
   let KILLTIMEOUT=$3
   SERVERPS=`ps $PSOPT | grep $SERVERNAME | grep -v grep`
   until [ "$SERVERPS" = "" ] || [ $KILLTIMEOUT -lt 0 ]; do
      $KILLALL $SIGNAL $SHORTSERVERNAME > /dev/null 2>&1
      KSTATUS=$?
      
      # if killall fullpath finds nothing use killall basename
      if [ $KSTATUS != 0 ] && [ $SERVERNAME = $SHORTSERVERNAME ]; then
	 SHORTSERVERNAME=`$BASENAME $SERVERNAME`
      fi

      sleep 1
      SERVERPS=`ps $PSOPT | grep $SERVERNAME | grep -v grep`
      let KILLTIMEOUT=KILLTIMEOUT-1
      if [ $KILLTIMEOUT -lt 0 ] && [ $SIGNAL != -KILL ]; then
         SIGNAL="-KILL"
         let KILLTIMEOUT=$3
      fi
   done

   # warn the user if we failed
   if [ $KILLTIMEOUT -lt 0 ]; then
      echo "ERROR: Could not kill $SERVERNAME"
   fi
}

#
# main starts here
#

if [ "`whoami`" != "root" ]; then
   echo "You must be root to run `basename $0`"
   exit 1
fi

BB=/usr/discreet/backburner
DEFAULT_MGR=`cat $BB/cfg/manager.host`
PSOPT="-elf"
OS=`uname`

KILLALL=/usr/bin/killall
BASENAME=/bin/basename
KILLALLTIMEOUT=30
