#!/bin/sh
#****************************************************************************************
#
# Startup script for the Common Console Framework
#
# Change log:
#
# 01/26/2007 Kurt Schroeder  -01 Make sure browser local browser is ended when CCFW ends.
#
#****************************************************************************************

dir=$1
if [ -z "$dir" ]; then
   dir=$PWD
else
   cd $dir
fi

user=$2

# Include the common hmc functions
. $dir/hmcfunctions

setupEnv $dir
echo "CCFW environment is:"
export

# fix umask and chmod the data/persist diectories.
umask g+w
chmod -R g+w "$dir/data/persist"

cleanupTmp $dir

hmcinitlog "The Common Console Framework daemon is being started."

# run the init scripts
runInit $dir $user

# Setup to use the local X server...the JVM needs this to be able
# to build graphics on the fly using AWT classes, whihc will not load
# when there is no X server.
export DISPLAY=127.0.0.1:0

# Run the manager forever.
loopCtrl=""
while [ -z "$loopCtrl" ]; do
   if [ -n "`isUnified`" ]; then # In a unified JVM environment
      if [ -z "$user" ]; then
         runUnified $dir 
      else
         runUnified $dir $user
      fi
   else  # Honor the presence/absence of the marker file
      if [ -z "$user" ]; then
         runmanagerOnly $dir 
      else
         runmanagerOnly $dir $user
      fi
   fi
   hmcinitlog "The Common Console Framework daemon has ended with rc=$jvmRc."
   echo "Common Console Framework daemon ended with rc=$jvmRc."
   #-01 start
   # Make sure the browser gets killed as well, now that the JVM has ended.
   if [ `whoami` == "root" ]; then
      echo "Killing all processes owned by user 'browser'..."
      ps -eo pid,uname | while read pid owner rest;
      do
         if [ "$owner" == "browser" ]; then
            echo "Killing process $pid owned by $owner..."
            kill -9 "$pid"
         fi
      done
   fi
   #-01 end
   case "$jvmRc" in
      102) 
           loopCtrl="halt"
           halt
           ;;
      103) 
           loopCtrl="poweroff" 
           poweroff
           ;;
      101) 
           loopCtrl="reboot" 
           reboot
           ;;
      *) 
           sleep 10
           ;;
   esac
done
