#!/bin/bash
#
# Description: Reset all processes in the same process group ID as the CCFW Manager by killing
#              these processes and then starting the CCFW Manager.  The local browser is also
#              terminated by killing all processes running under the browser userid.
#
# Change Activity:
#   10/19/2004 P. Callaghan - initial version
#   12/15/2006 L. Brocious  -01 Kill local browser, add logging
#   11/25/2007 P. Callaghan -02 Restart hardware server (per Austin's request)
#   11/29/2007 L. Brocious  -03 Make sure we're run by root user so we have permission to kill
#                               kill currently running CCFW and properly start a new one
#
#STARTUSAGE
#
# Usage:
#   resetccfw
#
#ENDUSAGE
#****************************************************************************

# -03 Begin
# Make sure we're root
me=$(whoami)                # Current user's login name
if [ $me != "root" ]; then
   echo "You must be logged in as root to run this script; you are currently logged in as $me."
   exit 1
fi
# -03 End

# -01 Begin
logfn='/var/hsc/log/resetccfw.log'

# Add a line to our log file just to mark that it was this script that initiated 
# the CCFW restart
echo "resetccfw invoked on $(date) ($(date --utc))" >> $logfn
# -01 End

# Kill the currently running CCFW
export PATH=$PATH:/opt/hsc/bin/    # Make sure killGroupOf is in our PATH
killGroupOf '/bin/sh /opt/ccfw/runccfw /opt/ccfw ccfw'
killRC=$?
if [ "$killRC" != "0" ] ; then
   echo "Warning: killGroupOf exited with rc=$killRC; continuing on anyway..." | tee -a $logfn
fi

# -01 Begin
# Kill the local browser so it doesn't interfere with the new instance about to be
# started by CCFW.
pids=$(ps -U browser -o pid --no-heading)
if [ -n "$pids" ]; then
   kill -9 $pids
   killRC=$?
   if [ "$killRC" != "0" ] ; then
      echo "Warning: attempt to kill browser processes (PIDs=$pids) exited with rc=$killRC; continuing on anyway..." | tee -a $logfn
   fi
fi
# -01 End

# -02 Begin
# Restart the hardware server.
/opt/hsc/bin/restart_hdwrsvr
# -02 End

# Now restart CCFW, which will also restart the local browser
set -m
/bin/bash /opt/ccfw/startccfw & 

exit $?
