#!/bin/bash
#==============================================================
#  Name:  lclshome-Event
#
#  Rem:   This script launches Stephen&Stephen's LCLS EPICS Home Screen.
# 
#  Usage: lclshome-Event
#
#  Auth:  07-Dec-2006, Jingchen Zhou   (jingchen)
#  Rev:   dd-mmm-yyyy,  Whoever    (user):
#         08-Dec-2006, Jingchen Zhou (jingchen)
#         - added OS check
#         23-Mar-2007, Jingchen Zhou (jingchen)
#         - set up the LCLS environment if not
#         27-Mar-2007, Jingchen Zhou (jingchen)
#         - set edm as noedit mode if used by production accounts
#         18-Jun-2008, Jingchen Zhou (jingchen)
#         - updated the account list for the CA network 
#         07-Dec-2011, Terri Lahey (lahey)
#         - update Pelham's script to use read-only PV gateway
#           & both local & released EDM files
#         01-Feb-2012, Terri Lahey (lahey)
#         - test the special LCLS-Event border on DEV
#         02-Feb-2012, Terri Lahey (lahey)
#         - use autogenerated screens in temporary directory
#         /afs/slac/g/lcls/tools/edm/TimingUsabilityDisplay
#         which is at the same level as the edm/display directory
#         This level was used by linac upgrade to avoid "finding"
#         the files by accident while people are testing their
#         screens.
#--------------------------------------------------------------

# ==============================================
# Let's override the original lclshome and 
# run a newer version of edm
# ==============================================
#export EPICS_EXTENSIONS_VER=Sergei
#export EDM=""
# ==============================================

CMD="edm"
ACCLIST="laci physics acclegr eoicdesk linac mccops1 lclsops1 lclsops2 lclsops3 lclsops4 lclsops5"

if [ `uname -s` != "Linux" ] ; then
    echo "Please run $0 on a public Linux machine"
    exit 1
fi

if [ -z "${EDM}" ] ; then
#    . /usr/local/lcls/epics/setup/epicsReset.bash
    . ~/.bashrc
fi

cmdpath=`which ${CMD}`
if [ ! -x "${cmdpath}" ] ; then
    echo "ERROR: cannot find ${CMD}"
    exit 1
fi

if [ ! -f ${EDM}/lcls/lcls_main.edl ] ; then
    echo "ERROR: cannot find lcls_main.edl"
    exit 1
fi 

USER=`whoami` 
INLIST=`echo ${ACCLIST} | grep ${USER}`
mode="-x"
if [ -n "${INLIST}" ]; then  
    mode="-x "
fi


#export EDMFILES=$TOOLS/edm/config-Beta
#export EDMOBJECTS=$EDMFILES
#export EDMPVOBJECTS=$EDMFILES
#export EDMFILTERS=$EDMFILES
#export EDMACTIONS=$EDMFILES

# Define EDMFILTERS, and CA ports depending upon AFS = development or mcclogin vs NFS = production
if [ -d /afs/slac/g/lcls ]; then

        export EDMFILTERS=/afs/slac/g/lcls/epics/iocTop/users/lahey/tools/edm/config/homeFilterTEST

#DEV - repeat whole list removing the errorneous /tools/edm/display from near the front of the DEV definition
        export EDMDATAFILES=.:..:/afs/slac/g/lcls/tools/edm/TimingUsabilityDisplay:/afs/slac/g/lcls/tools/edm/display/mgnt:/afs/slac/g/lcls/tools/edm/display/slc:/afs/slac/g/lcls/tools/edm/display/event:/afs/slac/g/lcls/tools/edm/display/vac:/afs/slac/g/lcls/tools/edm/display/lcls:/afs/slac/g/lcls/tools/edm/display/pps:/afs/slac/g/lcls/tools/edm/display/bcs:/afs/slac/g/lcls/tools/edm/display/mps:/afs/slac/g/lcls/tools/edm/display/bpms:/afs/slac/g/lcls/tools/edm/display/llrf:/afs/slac/g/lcls/tools/edm/display/prof:/afs/slac/g/lcls/tools/edm/display/laser:/afs/slac/g/lcls/tools/edm/display/mc:/afs/slac/g/lcls/tools/edm/display/ws:/afs/slac/g/lcls/tools/edm/display/watr:/afs/slac/g/lcls/tools/edm/display/temp:/afs/slac/g/lcls/tools/edm/display/alh:/afs/slac/g/lcls/tools/edm/display/blm:/afs/slac/g/lcls/tools/edm/display/toroid:/afs/slac/g/lcls/tools/edm/display/misc:/afs/slac/g/lcls/tools/edm/display/fbck:/afs/slac/g/lcls/tools/edm/display/und:/afs/slac/g/lcls/tools/edm/display/cud:/afs/slac/g/lcls/tools/edm/display/cf:/afs/slac/g/lcls/tools/edm/display/xray:/afs/slac/g/lcls/tools/edm/display/knob:/afs/slac/g/lcls/tools/edm/display/autosave:/afs/slac/g/lcls/tools/edm/display/ads:/afs/slac/g/lcls/tools/edm/display/camac:/afs/slac/g/lcls/tools/edm/display/photon_diag:/afs/slac/g/lcls/tools/edm/display/klys

#       use readonly EPICS Gateway for CA
        export EPICS_CA_ADDR_LIST=lcls-prod01:5068
        export EPICS_CA_REPEATER_PORT=5069
        export EPICS_CA_SERVER_PORT=5068

else

        export EDMFILTERS=/usr/local/lcls/tools/edm/config/homeFilterTEST
        export EDMDATAFILES=.:$TOOLS/edm/TimingUsabilityDisplay:$EDMDATAFILES
fi



${cmdpath} -eolc ${mode} lcls_main

if [ $? -ne 0 ]; then
    echo "ERROR: $0 failed" 
    exit 1
fi

exit 0

