#!/bin/bash
#==============================================================
#
#  Abs:  doSnoop.bash
#        runs caSnooper on the production network, and checks
#        PVs for connectability.
#
#  Name: doSnoop.bash
#
#  Arguments:
#       1 top n PVs to list and check
#       2 snoop for n seconds
#
#  Facility: SLAC/LCLS
#
#  Auth: 3-Aug-2009, Jingchen Zhou      (jingchen)
#  Rev:  dd-mmm-yyyy, Reviewer's Name    (USERNAME)
#
#--------------------------------------------------------------
#  Mod: (latest to oldest)
#   28-Jan-2011 jrock use default EPICS_CA_ADDR_LIST
#   03-Feb-2011 jrock retry if caSnooper failure, max 5 times
#==============================================================


if [ "$1" = "" ] || [ "$2" = "" ]
  then
       echo =========================================================
       echo Usage:
       echo   doSnoop.bash numPVs seconds
       echo
       echo   numPVs is the number of PVs most queried
       echo   seconds is the number of seconds to Snoop
       echo
       echo Example:
       echo   doSnoop.bash 10 20
       echo =========================================================
       exit
fi

#. /usr/local/lcls/epics/setup/epicsReset.bash
source /usr/local/lcls/tools/script/ENVS.bash
#source /usr/local/lcls/epics/setup/go_epics_3-14-10.bash
#change to 12 for Murali's new version
source /usr/local/lcls/epics/setup/go_epics_3-14-12.bash

# exclude the photon gateways from the snoop
# 28-Jan-2011 use the default EPICS_CA_ADDR_LIST
#export EPICS_CA_ADDR_LIST="172.27.11.255:5068 mcc-dmz"

export EPICS_CA_BEACON_PERIOD=30.0
export EPICS_CA_CONN_TMO=45.0
#printenv EPICS_CA_ADDR_LIST
#printenv EPICS_CA_REPEATER_PORT
#printenv EPICS_CA_SERVER_PORT
caget SIOC:SYS0:AL00:MODE


#caSnooper -c3  -p50 -t10
#caSnooper -d3 -p50 -t10

let again=1
let numTries=0

while [ $again = 1 ]
do
 let numTries=numTries+1
 caSnooperEXE -p$1 -t$2 > /tmp/caSnooper.out
 let snoopReturn=$?
 if [ $snoopReturn = 0 ]; then
    testSnooperConns.pl /tmp/caSnooper.out
    let again=0
 else
    echo caSnooperEXE failed with return code $snoopReturn
 fi

 # if there have been 5 tries, then give up and exit
 if [ $numTries = 5 ]; then
   let again=0
   echo EXITING AFTER 5 TRIES
 fi
done 


