#!/bin/bash
#==============================================================
# createDetachedScreen.bash
#
# ARGUMENT:
#   1 ioc name
#
# USE:
#   will be called by a script to invoke createDetachedScreen.bash for each 
#   hard IOC in screeniocs.
#
#   the main purpose is to restart ioc screen logging when that has stopped.
#
# Facility:  SLAC/LCLS
#
# Auth:  06-Aug-2010, Judy Rock (jrock)
# Rev:   dd-mmm-yyyy, Reviewer's Name (USERNAME)
#
# Rem:   Screen logging management
#----------------------------------------------------------------------------
# Mod:
#==============================================================
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
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 socket already
# if there is, exit
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. 
   echo "==> "EXITING...
   exit 1
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


