#!/bin/bash
#==============================================================
# createDetachedScreen.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-dev1"
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

ts=`grep "^$ioc" $IOC_SCREEN/screeniocs | awk '{print $2}'`
port=`grep "^$ioc" $IOC_SCREEN/screeniocs | awk '{print $3}'`

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

if [ "$ts" = "" ] || [ "$port" = "" ]; then
  echo IOC not found in screeniocs file
  exit 2
fi
if [ "$port" = "sioc" ]; then
  echo $ioc is a soft ioc - not processing
  exit 2
fi

CWD=`pwd`

#echo "==> "$CWD

#echo "==> Checking $ioc..."

# see if there's an existing screen process out there
# if there is, exit
numPROCS=`ssh $SCREEN_HOST  -l laci ps -ef | grep " $ioc" | grep SCREEN | wc -l`
if [ $numPROCS -gt 0 ]; then
   echo "==> "$numPROCS SCREEN process already exists for $ioc.  
   echo "==> "EXITING...
   exit 1
fi

# next, verify there's no pipe file already
# if there is, first check for any existing processes.  If none found, remove the pipe
numSOCKS=`ssh $SCREEN_HOST -l laci ls -1 /home/screen/sock/S-laci/*.$ioc* | wc -l`
if [ $numSOCKS -gt 0 ]; then
   echo "==> "$numSOCKS socket file already exists for $ioc: removing it
   pname=`ssh $SCREEN_HOST -l laci ls -1 /home/screen/sock/S-laci/*.$ioc*`
   echo "==> "REMOVING pipe file $pname ...
   deloutput=`ssh $SCREEN_HOST -l laci rm $pname`

   # check to make sure deletion occurred
   # if the file is still there, exit because something is seriously wrong with the operating envirnment
   numSOCKS=`ssh $SCREEN_HOST -l laci ls -1 /home/screen/sock/S-laci/*.$ioc* | wc -l`
   if [ $numSOCKS -gt 0 ]; then
      echo "PIPE FILE DELETION FAILED FOR $pname"
      echo "EXITING"
      exit
   fi

# THIS IS NOT NECESSARY.  THE PIPE FILE IS NOW ALWAYS DELETED, REMOVING ANY POTENTIAL REFERENCE TO AN EXISTING NEW PROCESS
#   echo "==> "old $numSOCKS socket file already exists for $ioc.
#   echo "==> "checking for possible existing processes...

#   pname=`ssh $SCREEN_HOST -l laci ls -1 /home/screen/sock/S-laci/*.$ioc*`
#   pfile=`echo $pname | cut -d "/" -f 6`
#   pnum=`echo $pfile | cut -d "." -f 1`
#   numPROCS=`ssh $SCREEN_HOST  -l laci ps -ef | grep $pnum | grep -v grep | wc -l`

#   if [ $numPROCS -eq 0 ]; then
#      echo "==> "REMOVING pipe file $pname ...
#      deloutput=`ssh $SCREEN_HOST -l laci rm $pname`
#      echo deletion result: $deloutput
#   else
#      echo "==> "STOP PROCESSING IOC $ioc - PROCESS $pnum MAY EXIST - NEEDS MANUAL INTERVENTION!
#      #echo -n "hit any key"
#      #read inp
#      exit
#   fi

fi

# 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 start SCREEN with logging...EXITING
   exit 1
fi

# 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

# backup screenlog.0 file to screenlog.0-<timestamp>
if [ -e $IOC_DATA/$ioc/screenlog.0 ]; then
   export CURRTIMESTAMP=`date '+%Y%m%d%H%M%S'`
   export BCKUP_FILE=screenlog.0-$CURRTIMESTAMP
   echo "==> "copying screenlog.0 file to $BCKUP_FILE
   cp $IOC_DATA/$ioc/screenlog.0 $IOC_DATA/$ioc/$BCKUP_FILE
fi

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

#echo $ioc: FAKING IT OUT
# now start the SCREEN session
# echo Starting SCREEN for $ioc $ts $port
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 for new detached screen process...
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"

   # echo "==> "Checking for new detached screen process...
   #echo $ioc: FAKING IT OUT
   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 - giving up
      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
   cd $CWD
   exit 2
fi

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


