#!/bin/bash
#==============================================================
# restartALLIOCLogging.bash
#
# ARGUMENTS:
#
# OPTION:     
#
# Facility:  SLAC/LCLS
#
# Auth:  07-Jun-2005, Jingchen Zhou   (JINGCHEN)
# Rev:   dd-mmm-yyyy, Reviewer's Name (USERNAME)
#
# Rem:   Screen logging management
#        - save screenlog.0 to screenlog.1
#        - screenlog.0 will be recreated automatically by Screen program 
#           if removed.        
#        - remove screenlog.1 if older than 3 days.
#        - run as cron job
#        - Note: automatic restart of the screen logging daemon will be added 
#----------------------------------------------------------------------------
# Mod:
#       21-Feb-2012, Judy Rock (jrock)
#       added 1 second delay - just in case!
#       30-Mar-2011, Judy Rock (jrock)
#       for soft IOCs screenlogd check, first find which host (siocs are now on lcls-daemon0 and lcls-daemon1)
#==============================================================
EMAIL="jingchen@slac.stanford.edu"
#EMAIL="jrock@slac.stanford.edu"

emailcmd="/usr/bin/Mail -s $0 ${EMAIL}"

if [ "$1" != "GO" ]; then
   echo "=========================================================================="
   echo THIS SCRIPT WILL RESTART IOCCONSOLE AND SCREEN LOGGING FOR ALL "HARD" IOCS 
   echo IN THE LCLS SYSTEM.
   echo IT WILL ALSO ATTEMPT iocTSmgr ON ANY TS PORT THAT SEEMS TO NEED IT
   echo TO RUN THIS SCRIPT FOR REAL PLEASE ENTER:
   echo "   restartALLIOCLogging.bash GO "
   echo "=========================================================================="
   exit
fi

echo PROCEEDING...

#
# Set up LCLS_ROOT
#
if [ -d /afs/slac/g/lcls ]; then
   export SCREEN_HOST=lcls-dev1
   export SEARCH_SCREEN_HOST=lcls-dev1
   export LCLS_ROOT=/afs/slac/g/lcls
else 
   export SCREEN_HOST=lcls-daemon1
   export SEARCH_SCREEN_HOST=lcls-daemon
   export LCLS_ROOT=/usr/local/lcls 
fi
#
# Set up LCLS_DATA
#
if [ -d /nfs/slac/g/lcls ]; then
   export LCLS_DATA=/nfs/slac/g/lcls  
else
   export LCLS_DATA=/u1/lcls
fi

IOC_SCREEN=$LCLS_ROOT/epics/iocCommon
IOC_DATA=$LCLS_DATA/epics/ioc/data

# go through all the hard iocs (ioc-*, eioc-*)
# createDetachedScreen.bash checks for a screen process and socket file
# if these aren't found, creates a new screen session.
# the new screen process will write to the existing screenlog.0; if the file
# is not found, it will create one

echo STARTING SCREEN PROCESSES THAT NEED IT...
iocs=`grep ^[e]*ioc-.*$SCREEN_HOST $IOC_SCREEN/screeniocs | awk '{print $1}'`
for i in $iocs
do
  echo checking screen process for $i
  createDetachedScreenFORRESTART_dev.bash $i
  sleep 1
done

#/usr/local/lcls/tools/script/check_screenlogdLog.bash
#cat /tmp/screenlogdLogErrList.log | Mail -s "RESTART LOGGING PROC createDetachedScreen errors" jrock@mccdev.slac.stanford.edu jingchen@mccdev.slac.stanford.edu

echo DONE

exit 0

