#!/bin/bash -f
#==============================================================
#
#  Abs:  Start Alarm Handler
#
#  Name: facetalh
#
#  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  01-May-2012  added opi21 to list of mcc opis
#  jrock  25-Apr-2012  added opi21 to list of mcc opis
#  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/facet/tools/script/ENVS_facet.bash
else
#    source /usr/local/facet/epics/setup/epicsReset.bash
    source /usr/local/facet/tools/script/ENVS_facet.bash

fi

cd $ALHLOGFILES
umask 000

export supOK=NO
echo Host is $HOSTNAME

# check for privileged users or places
if [ $USER == "mccops1" -o $USER == "eoicuser" ]; then
   export supOK=YES
fi
if [ $HOSTNAME == "lcls-opi01.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi02.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi21.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi31.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi03.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi04.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi05.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi06.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi07.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi08.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi09.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi10.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi11.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi12.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi13.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi14.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi15.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi16.slac.stanford.edu" -o \
     $HOSTNAME == "lcls-opi01" -o \
     $HOSTNAME == "lcls-opi02" -o \
     $HOSTNAME == "lcls-opi21" -o \
     $HOSTNAME == "lcls-opi31" -o \
     $HOSTNAME == "lcls-opi03" -o \
     $HOSTNAME == "lcls-opi04" -o \
     $HOSTNAME == "lcls-opi05" -o \
     $HOSTNAME == "lcls-opi06" -o \
     $HOSTNAME == "lcls-opi07" -o \
     $HOSTNAME == "lcls-opi08" -o \
     $HOSTNAME == "lcls-opi09" -o \
     $HOSTNAME == "lcls-opi10" -o \
     $HOSTNAME == "lcls-opi11" -o \
     $HOSTNAME == "lcls-opi12" -o \
     $HOSTNAME == "lcls-opi13" -o \
     $HOSTNAME == "lcls-opi14" -o \
     $HOSTNAME == "lcls-opi15" -o \
     $HOSTNAME == "lcls-opi16" -o \
     $HOSTNAME == "facet-builder" ]; then
   export supOK=YES
fi

# This kludge logic makes it so that people can ack alarms from the sunray sessions.  This will change in the 
# (hopefully near) future!!
if [ $USER == "physics" ]; then
   if [ $HOSTNAME == "lcls-srv02" -o $HOSTNAME == "lcls-srv02.slac.stanford.edu" ]; then
      export supOK=YES
   fi
fi

if [ $USER == "fphysics" ]; then
   if [ $HOSTNAME == "facet-srv01" -o $HOSTNAME == "facet-srv01.slac.stanford.edu" ]; then
      export supOK=YES
   fi
fi
# 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
#
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/FACET_MCC.alhConfig > /dev/null 2>&1 &
elif [ $mode = "spy" ]; then
    alh -a OpAlarms -o OpMods -D -l $ALHLOGFILES -f $ALHCONFIGFILES -mainwindow -noerrorpopup -S -m 10000 $ALHCONFIGFILES/FACET.alhConfig > /dev/null 2>&1 &
fi
# End of script
