#!/bin/bash -f
#==============================================================
#
#  Abs:  Start Alarm Handler
#
#  Name: astaalh
#
#  Args: 
#    $1 = sup, spy, spear - which mode of alh to run 
#         (supervisor, spy, or spear spy)
#         Default is spy.
#
#  Usage:  For all modes, command line options are:
#             -l $ALHLOGFILES    = where log files are
#             -f $ALHCONFIGFILES = where config files are
#             -a Alarms.log = name of alarm log files
#             -o OpMods.log = loc and name of op mod log files
#             -noerrorpopup = popup windows for errors is disabled
#             -m 10000 = max number of alarms in the alarm file
#          For supervisor and spy modes, command line options are:
#             Removed for now: -B = message broadcast system
#             -mainwindow = skip display of the runtime window
#          For sup mode, command line options are:
#         # removed this for now!
#         #    -oCM = log operator mods to CMLOG
#         #    -wCM = wordy CMLOG messages
#          For spy and sup, command line options are:
#             -D = disable all log output
#          For spy, command line options are:
#             -S = passive
#          For daemon, command line options are:
#             -aCM = log alarms to CMLOG
#             -s = silent
#             -S = passive
#
#--------------------------------------------------------------
# MODS:
#  jrock  09-Dec-2011  updated to use asta $TOOLS
#  lahey  05-Dec-2011  created initial ASTA version from a copy of FACET version!
#                      *** Later change environment setup once the ASTA files are available.
#  jrock  02-Feb-2011  created FACET version!
#  jrock  15-Dec-2008  source ENVS.bash instead of epicsReset.bash to
#                      get the matlab setup
#  jrock  12-May-2008  add logic so that any eoicdesk or mccops1 user can run as sup
#                      and any user on the OPIs can run as sup
#  jrock   7-May-2008  if on afs, source afs epicsReset
#  jrock  05-Mar-2008  added hostnames without ".slac.stanford.edu"
#                      run in sup mode without logging to cmlog!  
#  jrock   8-Nov-2007  translating to bash
#  jrock  16-Apr-2007  for sup and spy, -a OpAlarms instead of Alarms.  Alarms is reserved
#                      for the logging process only. 
#  jrock   5-Feb-2007  Check host before running in "sup" mode.  Can only do this 
#                      from a kiosk machine, or for now from lcls-dev2.
# 
# Make sure any files written by this process are group-writeable.
#
# set environment
if [ -d /afs/slac/g/facet ]; then
#    source /afs/slac/g/facet/epics/setup/epicsReset.bash
    source /afs/slac/g/acctest/tools/script/ENVS_acctest.bash
else
#    source /usr/local/facet/epics/setup/epicsReset.bash
    source /usr/local/acctest/tools/script/ENVS_acctest.bash

fi

cd $ALHLOGFILES
umask 000

export supOK=NO
echo Host is $HOSTNAME

export supOK=YES

# if running without a parameter, then use sup no matter what (this is for running from an icon or from 
# lclshome in the control room)
# if running with sup parameter, then check to make sure it's ok
# in the end, default to spy mode
#
export ALHCONFIGFILES=$TOOLS/alh/config-xta

echo CONFIGFILES ARE HERE: $ALHCONFIGFILES

if [ $# -lt 1 ]; then
    if [ $supOK == "YES" ]; then
       export mode=sup
    else
       export mode=spy
    fi
elif [ $1 = "sup" ]; then
    if [ $supOK == "YES" ]; then
       export mode=sup
    else
       echo Cannot run in sup mode as user $USER on host $HOSTNAME.  Switching to spy mode.
       export mode=spy
    fi
else
    export mode="spy"
fi

if [ $mode = "sup" ]; then 
#    alh -a OpAlarms -o OpMods -D -l $ALHLOGFILES -f $ALHCONFIGFILES -global -mainwindow -noerrorpopup -m 10000 $ALHCONFIGFILES/XTA_MCC.alhConfig > /dev/null 2>&1 &
    alh -a OpAlarms -o OpMods -D -l $ALHLOGFILES -f $ALHCONFIGFILES -global -mainwindow -noerrorpopup -m 10000 $ALHCONFIGFILES/XTA_MCC.alhConfig 
elif [ $mode = "spy" ]; then
#    alh -a OpAlarms -o OpMods -D -l $ALHLOGFILES -f $ALHCONFIGFILES -mainwindow -noerrorpopup -S -m 10000 $ALHCONFIGFILES/XTA.alhConfig > /dev/null 2>&1 &
    alh -a OpAlarms -o OpMods -D -l $ALHLOGFILES -f $ALHCONFIGFILES -mainwindow -noerrorpopup -S -m 10000 $ALHCONFIGFILES/XTA.alhConfig 
fi
# End of script
