#!/bin/bash -norc
#==============================================================
#
#  Abs:  Start the CA Snooper bash script.  Meant to be used
#        by the daily cron job run by laci on lcls-daemon2
#
#  Name: CASnooperChecker
#
#  Facility:  SLAC/LCLS
#
#  Auth: 28-Jan-2011, Mike Zelazny (zelazny@slac.stanford.edu)
#
#  Mod: 
#==============================================================
#
DT=`date "+%D - %T"`
SLAC="@slac.stanford.edu"
MAILLIST="controls-software-reports$SLAC"
#
# Setup LCLS Environment
#
  if [ -f /usr/local/lcls/tools/script/ENVS.bash ]; then
    . /usr/local/lcls/tools/script/ENVS.bash
  fi

  if [ -e ${LCLS_ROOT}/tools/matlab/setup/matlabSetup.bash ]; then
    . ${LCLS_ROOT}/tools/matlab/setup/matlabSetup.bash

#
# Log file
#
    if [ -z $PHYSICS_USER ]
    then
      user=`whoami`
    else
      if [ none = $PHYSICS_USER ]
      then
        user=`whoami`
      else
        user=$PHYSICS_USER
      fi
    fi
    date=`date`
    year=`echo $date | cut -d" " -f6`-
    month=`echo $date | cut -d" " -f2`-
    day=`echo $date | cut -d" " -f3`-
    time=`echo $date | cut -d" " -f4 | cut -c1-5`

    if [ -e /u1/lcls/tools/CASnooper ]; then
      log_file=/u1/lcls/tools/CASnooper/CASnooperChecker-$user-$year$month$day$time.log
    else
      log_file=CASnooperChecker-$user-$year$month$day$time.log
    fi 

#
# In case there is a core dump
#
    if [ -e /u1/lcls/tools/CASnooper ]; then
      cd /u1/lcls/tools/CASnooper
      ulimit -c unlimited
    fi

#
# Trim log files
#
    find /u1/lcls/tools/CASnooper -name "CASnooperChecker*.log" -mtime +3 -exec rm -f {} \;
#
# Start the script
#
    doSnoop.bash 500 600 | tee $log_file 2>&1

  else
    echo "Sorry, can't find LCLS environment setup"
  fi

cat $log_file | mailx -s "CA Snooper Checker ($DT)" $MAILLIST 
