#!/bin/bash
#==============================================================
# restartIOCConsole.bash 
#
# ARGUMENT:
#   1 ioc name
#
# USE:
#   will be called by a script for each hard IOC in screeniocs.
#
#   the main purpose is to restart ioc screen logging when that has stopped.
#   and also to ioTSmgr reset for those IOCs that need it.
#
# Facility:  SLAC/LCLS
#
# Auth:  06-Aug-2010, Judy Rock (jrock)
# Rev:   dd-mmm-yyyy, Reviewer's Name (USERNAME)
#
# Rem:   Screen logging management
#----------------------------------------------------------------------------
# Mod:
#  22-May-2012,  Judy Rock (jrock)
#    remove obsolete pipe file instead of doing an iocConsole cleanup!
#==============================================================
if [ -d /afs/slac/g/lcls ]; then
   export LCLS_ROOT=/afs/slac/g/lcls
   export SCREEN_HOST="lcls-dev2"
else
   export LCLS_ROOT=/usr/local/lcls
   export SCREEN_HOST="lcls-daemon1"
fi

source $LCLS_ROOT/tools/script/ENVS.bash

# verify argument
if [ $# != 1 ]; then
   echo "==> "Please call with 1 parameter: ioc name
   exit 1
fi

# get the terminal server and port from screeniocs
ioc=$1

## TESTING
#ts=`grep "^$ioc" /u/cd/jrock/work_iocConsole/TESTscreeniocs | awk '{print $2}'`
#port=`grep "^$ioc" /u/cd/jrock/work_iocConsole/TESTscreeniocs | awk '{print $3}'`
ts=`grep "^$ioc" $IOC_SCREEN/screeniocs | awk '{print $2}'`
port=`grep "^$ioc" $IOC_SCREEN/screeniocs | awk '{print $3}'`

echo "==> -----------------------------------------------"
echo "==> "$ioc $ts $port

#UNCOMMENT THIS LATER
#if [ "$ts" = "" ] || [ "$port" = "" ]; then
#  echo $ioc not found in screeniocs file
#  exit 2
#fi
if [ "$port" = "sioc" ]; then
  echo $ioc is a soft ioc...EXITING
  exit 2
fi

CWD=`pwd`

#echo "==> "$CWD

# don't start SCREEN session if there is no $IOC_DATA directory for the
# ioc - i.e. a place for the screenlog.0 file!  otherwise this file will
# be dumped in a strange place
if [ ! -e $IOC_DATA/$ioc ]; then
   echo "==> "$IOC_DATA/$ioc directory does not exist
   echo "==> "Cannot restart logging...EXITING
   exit 1
fi

# yes, check this first - don't proceed if the ioc or ts is down
# test to see if the ioc is up - if it's not, exit now
ping -c 1 $ioc
if [ $? != 0 ]; then
   echo "==> "IOC $ioc is not pingable
   echo "==> "EXITING...
   exit 1
fi

# test to see if the terminal server is up - if it's not, exit now
ping -c 1 $ts
if [ $? != 0 ]; then
   echo "==> "TERMINAL SERVER $ts is not pingable
   echo "==> "EXITING...
   exit 1
fi

#FIRST APPEND THE screenlog.0 FILE to screenlog.0-LOG
if [ -e $IOC_DATA/$ioc/screenlog.0 ]; then
   echo "==> "cat-ing screenlog.0 file to screenlog.0-LOG
   cat $IOC_DATA/$ioc/screenlog.0 >> $IOC_DATA/$ioc/screenlog.0-LOG
   rm $IOC_DATA/$ioc/screenlog.0
else
   echo "==> "no $IOC_DATA/$ioc/screenlog.0 file found
fi

echo "==> "Doing cleanup for $ioc
iocConsole -cleanup $ioc

numPROCS=`ssh $SCREEN_HOST  -l laci ps -ef | grep " $ioc" | grep SCREEN | wc -l`
if [ $numPROCS -gt 0 ]; then
   echo "==> "process still exists: cleanup for $ioc failed - please check manually
   exit
fi

# check for pipe file
# and if found, issue a wipe command
numSOCKS=`ssh $SCREEN_HOST -l laci ls -1 /home/screen/sock/S-laci/*.$ioc* | wc -l`
if [ $numSOCKS -gt 0 ]; then
   echo "==> "pipe file still exists: cleanup for $ioc failed - please check manually
   exit
fi 

# all is well, restart the session
echo "==>"
echo "==> "Now restarting screen process
if [ -d $IOC/$ioc/screenrc ]; then
   export SCREENRC=$IOC/$ioc/screenrc
else
   export SCREENRC=$IOC_SCREEN/screenrc
fi

# now start the SCREEN session
# echo Starting SCREEN for $ioc $ts $port
echo "==> "Restarting iocConsole session for $ioc...
ssh $SCREEN_HOST -l laci "cd $IOC_DATA/$ioc;umask 000;/home/screen/bin/screen -d -m -S $ioc -t $ioc -c $SCREENRC telnet $ts $port"

echo "==> "Checking the restart
numPROCS=`ssh $SCREEN_HOST -l laci ps -ef | grep " $ioc" | grep SCREEN | wc -l`
if [ $numPROCS -lt 1 ]; then
   echo "==> "$ioc create SCREEN process FAILED - trying iocTSmgr reset now...
   cd $CWD

   iocTSmgr $ioc reset
   echo "==> "and now retrying screen start...
   ssh $SCREEN_HOST -l laci "cd $IOC_DATA/$ioc;umask 000;/home/screen/bin/screen -d -m -S $ioc -t $ioc -c $SCREENRC telnet $ts $port"

   numPROCS=`ssh $SCREEN_HOST -l laci ps -ef | grep " $ioc" | grep SCREEN | wc -l`
   if [ $numPROCS -lt 1 ]; then
      echo "==> "$ioc create SCREEN process FAILED AGAIN - please check manually
      cd $CWD
      exit 2
   fi
fi

# echo "==> "Checking for new socket...
numSOCKS=`ssh $SCREEN_HOST -l laci ls -1 /home/screen/sock/S-laci/*.$ioc* | wc -l`
if [ $numSOCKS -lt  1 ]; then
   echo "==> "$ioc create SCREEN socket FAILED - please check manually
   cd $CWD
   exit 2
fi

cd $CWD
echo "==> "DONE!
exit 0


