#!/bin/bash -norc
#==============================================================
#
#  Abs:  Script to execute EPICS caRepeater
#
#  Name: st.caRepeater
#
#  Rem:  This Type II script is used to setup and start up EPICS caRepeater.
#        It should be run before any CA client.
#
#  Facility:  SLAC/LCLS
#
#  Auth: 14-Mar-2007, Jingchen Zhou      (jingchen)
#  Rev:  dd-mmm-yyyy, Reviewer's Name    (USERNAME)
#        12-Nov-2007, Jingchen Zhou      (jingchen)
#        Converted to bash
#        Updated to support the standalone production environment
#==============================================================

# make sure no other caRepeater runs
PIDS=`pgrep -x caRepeater`
if [ "$PIDS" != "" ]; then
    kill -9 "$PIDS"
fi

sleep 1

# set up environment for caRepeater (such as EPICS_CA_REPEATER_PORT)

if [ -f /afs/slac/g/lcls/epics/setup/fixed-epics-setup.bash ]; then
    source /afs/slac/g/lcls/epics/setup/fixed-epics-setup.bash
    if [ -f /afs/slac/g/lcls/epics/setup/epicsenv-7.0.2-1.0.bash ]; then
        source /afs/slac/g/lcls/epics/setup/epicsenv-7.0.2-1.0.bash
    fi
elif [ -f /usr/local/lcls/epics/setup/fixed-epics-setup.bash ]; then
    source /usr/local/lcls/epics/setup/fixed-epics-setup.bash
    if [ -f /usr/local/lcls/epics/setup/epicsenv-7.0.2-1.0.bash ]; then
        source /usr/local/lcls/epics/setup/epicsenv-7.0.2-1.0.bash
    fi
else
    echo "ERROR: this machine is not supported for LCLS dev/prod" 
    exit 1    
fi

# start caRepeater

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

caRepeater > /dev/null &

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

exit 0

# End of script
