#!/bin/bash -f
#==============================================================
#
#  Abs:  Start Alarm Handler
#
#  Name: alhgui
#
#  param 1: acc            = accelerator (e.g. facet,lcls,xta,asta)
#  param 2: TOPalhConfigNm = name of toplevel alhConfig file (e.g. FACET,LCLS_MCC, etc.)
#  param 3: Mode           = This is an optional field for mode (e.g. MCCMode,passiveMode,SUP,SPY)
#  param 4: dev            = This is an optial field for development systems (e.g. -d)
#

function usage {
   echo "========================================="
   echo Usage
   echo "   Param 1: accelerator, in lower case (facet, lcls, etc.)"
   echo "   Param 2: top-level alhConfig filename (e.g. FACET, LCLS, SPEAR, etc.)"
   echo "   Param 3: Mode (e.g. MCCMODE, passiveMode,SUP,SPY)"
   echo "   Param 4: development test, use -d (optional)"
   echo " "
   echo " for example:"
   echo "    alhgui spear SPEAR"
   echo " "
   echo " runs the top-level alhgui, for example from the <acc>home display Alarm Tree button." 
   echo " finds the top-level alhConfig filename in the GLOBAL alhConfig directory."
   echo " alhgui assumes that TOOLS and the EPICS environment are already set"
   echo "========================================="
   exit
}

if [ $# -lt 2 ]; then
   usage
fi

if [ "$1" = "-h" -o "$1" = "-H" ]; then
   usage
fi

# we default to the user being MCCMode mode - check later to see if that's ok based on host/user
# if the host/user is ok, we run MCCMode by default unless passiveMode is requested
export modeReq="MCCMode"
export acc=$1
export TOPalhConfigNm=$2
dev=N

if [ $# -gt 2 ]; then
   if [ "$3" = "MCCMode" -o "$3" = "SUP" ]; then
      export modeReq="MCCMode"
   elif [ "$3" = "passiveMode" -o "$3" = "SPY" ]; then
      export modReq="passiveMode"
   elif [ "$3" = "-d" ]; then
      dev=Y
   fi
fi
if [ $# -eq 4 ]; then
 if [ "$4" = "-d" ]; then
   dev=Y
  fi
fi

# capture whether we're running on production or not, and set the prod symlink name accordingly
# default is the "standard lcls" development (meaning lcls-dev2) scenario, where each accelerator 
# directory has its own symlink 
# similar for asta and xta, where there would be 2 symlinks, one for xta and one for asta
export prodlink=prod
export onProd="Y"
case $acc in
  "lcls")
    if [ -d /afs/slac/g/lcls ]; then
      export FACILITY=dev
      export FACILITY_ROOT=/afs/slac/g/$acc
      export onProd="N"
    else
      export FACILITY=$acc
      export FACILITY_ROOT=/usr/local/$acc
    fi
     ;;
  "facet")
    if [ -d /afs/slac/g/lcls ]; then
      export FACILITY=dev
      export FACILITY_ROOT=/afs/slac/g/lcls
      export onProd="N" 
      export prodlink=prod-$acc
    else
      export FACILITY=$acc
      export FACILITY_ROOT=/usr/local/$acc
    fi
    ;;
    
  "xta")
     if [ "$dev" = "Y" ]; then
       export FACILITY_ROOT=/afs/slac/g/lcls 
       export FACILITY=$dev
     else
       export FACILITY_ROOT=/afs/slac/g/acctest
       export FACILITY=$acc
     fi
     export prodlink=prod-$acc
     ;;

  "asta")
     if [ "$dev" = "Y" ]; then
       export FACILITY_ROOT=/afs/slac/g/lcls 
       export FACILITY=$dev
     else
       export FACILITY_ROOT=/afs/slac/g/acctest
       export FACILITY=$acc
     fi
     export prodlink=prod-$acc
     ;;
  "spear")
     ;;
  *)
   exit 1
   ;;
esac

#=========================================================================
# now determine whether or not we should run in MCCMode (ack allowed) mode
export MCCModeOK=NO
#echo Host is $HOSTNAME

# check for privileged users or places
# can only "ack" if privileged
echo Running on $HOSTNAME
if [ $USER == "mccops1" -o $USER == "eoicuser" -o $USER == "spear" ]; then
   export MCCModeOK=YES
fi
if [ $HOSTNAME == "lcls-opi01" -o \
     $HOSTNAME == "lcls-opi02" -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 == "lcls-opi20" -o \
     $HOSTNAME == "lcls-opi21" -o \
     $HOSTNAME == "lcls-opi22" -o \
     $HOSTNAME == "lcls-opi23" -o \
     $HOSTNAME == "lcls-opi24" -o \
     $HOSTNAME == "lcls-opi25" -o \
     $HOSTNAME == "lcls-opi30" -o \
     $HOSTNAME == "lcls-opi31" -o \
     $HOSTNAME == "lcls-opi32" -o \
     $HOSTNAME == "lcls-dev2" ]; then
   export MCCModeOK=YES
fi

# This logic makes it so that people can ack alarms from the sunray sessions.  
# This will change in the (hopefully near) future!!
# Note: this doesn't work for facet
if [ $USER == "physics" ]; then
   if [ $HOSTNAME == "lcls-srv02" -o $HOSTNAME == "lcls-srv02.slac.stanford.edu" ]; then
      export MCCModeOK=YES
   fi
elif [ $USER == "fphysics" ]; then
   if [ $HOSTNAME == "facet-srv02" -o $HOSTNAME == "facet-srv02.slac.stanford.edu" ]; then
      export MCCModeOK=YES
   fi
fi

# if requesting supervisor, then check to make sure it's ok
# in the end, default to passiveMode mode
if [ "$modeReq" = "MCCMode" ]; then
    if [ "$MCCModeOK" == "YES" ]; then
       export mode=MCCMode
    else
       export mode=passiveMode
    fi
else
    export mode=passiveMode
fi

#=========================================================================
# then proceed
export ALHTOP=$FACILITY_ROOT/tools/AlarmConfigsTop
source $ALHTOP/SCRIPT/AlarmEnvSetup.bash

# source EPICS7 environment
if [ -f $EPICS_SETUP/fixed-epics-setup.bash ]; then
    source $EPICS_SETUP/fixed-epics-setup.bash
    if [ -f $EPICS_SETUP/epicsenv-7.0.2-1.1.bash ]; then
        source $EPICS_SETUP/epicsenv-7.0.2-1.1.bash
    fi 
fi

export ALHCONFIGFILES=$ALHTOP/GLOBAL/$prodlink/$acc/alh

# define ALHMODE for spear
export ALHMODE=spy

if [ "$mode" = "passiveMode" ]; then
#   echo "running as passiveMode"
   alh -a OpAlarms -o OpMods -D -l $ALHLOGFILES -f $ALHCONFIGFILES -mainwindow -noerrorpopup -S -m 10000 $ALHCONFIGFILES/$TOPalhConfigNm.alhConfig 
else
#   echo "running as MCCMode"
   alh -a OpAlarms -o OpMods -D -l $ALHLOGFILES -f $ALHCONFIGFILES -global -mainwindow -noerrorpopup -m 10000 $ALHCONFIGFILES/$TOPalhConfigNm"_MCC".alhConfig > /dev/null 2>&1 
fi

# end of script
