#!/bin/bash
#==============================================================
# createDetachedScreen_facet.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/FACET
#
# Auth:  21-Jun-2011, Judy Rock (jrock)
# Rev:   dd-mmm-yyyy, Reviewer's Name (USERNAME)
#
# Rem:   Screen logging management
#----------------------------------------------------------------------------
# Mod:
#==============================================================
currHost=`uname -n`
currUser=`whoami`
echo running on $currHost as $currUser

if [ -d /afs/slac/g/facet ]; then
   export FACET_ROOT=/afs/slac/g/facet
   export SCREEN_HOST="facet-dev2"
else
   export FACET_ROOT=/usr/local/facet
   export SCREEN_HOST="facet-daemon1"
fi

source $FACET_ROOT/tools/script/ENVS_facet.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/facet_screeniocs | awk '{print $2}'`
port=`grep "^$ioc" $IOC_SCREEN/facet_screeniocs | awk '{print $3}'`

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

if [ "$ts" = "" ] || [ "$port" = "" ]; then
  echo IOC not found in facet_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
if [ "$currHost" = "facet-daemon1" ] && [ "$currUser" = "flaci" ]; then
   numPROCS=`ps -ef | grep " $ioc" | grep SCREEN | wc -l`
else
   numPROCS=`ssh $SCREEN_HOST -l flaci ps -ef | grep " $ioc" | grep SCREEN | wc -l`
fi

if [ $numPROCS -gt 0 ]; then
   echo "==> "$numPROCS SCREEN process already exists for $ioc.  
   echo "==> "EXITING...
   exit 1
fi

# if there's no screen process and there is a socket, then cleanup is required
# next, verify there's no socket already
# if there is, cleanup
if [ "$currHost" = "facet-daemon1" ] && [ "$currUser" = "flaci" ]; then
   numSOCKS=`ls -1 /home/screen/sock/S-flaci/*.$ioc* | wc -l`
else
   numSOCKS=`ssh $SCREEN_HOST -l flaci ls -1 /home/screen/sock/S-flaci/*.$ioc* | wc -l`
fi

if [ $numSOCKS -gt 0 ]; then
   echo "==> "$numSOCKS socket file already exists for $ioc. 
   echo "==> "DOING CLEANUP...
   iocConsole $ioc -cleanup
   # 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: WILL PROCESS HERE, FOR NOW FAKING IT OUT
# now start the SCREEN session
 echo Starting SCREEN for $ioc $ts $port
ssh $SCREEN_HOST -l flaci "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...
if [ "$currHost" = "facet-daemon1" ] && [ "$currUser" = "flaci" ]; then
   numPROCS=`ps -ef | grep " $ioc" | grep SCREEN | wc -l`
else
   numPROCS=`ssh $SCREEN_HOST -l flaci ps -ef | grep " $ioc" | grep SCREEN | wc -l`
fi
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 flaci "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
   if [ "$currHost" = "facet-daemon1" ] && [ "$currUser" = "flaci" ]; then
      numPROCS=`ps -ef | grep " $ioc" | grep SCREEN | wc -l`
   else
      numPROCS=`ssh $SCREEN_HOST -l flaci ps -ef | grep " $ioc" | grep SCREEN | wc -l`
   fi
   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...
if [ "$currHost" = "facet-daemon1" ] && [ "$currUser" = "flaci" ]; then
   numSOCKS=`ls -1 /home/screen/sock/S-flaci/*.$ioc* | wc -l`
else
   numSOCKS=`ssh $SCREEN_HOST -l flaci ls -1 /home/screen/sock/S-flaci/*.$ioc* | wc -l`
fi
if [ $numSOCKS -lt  1 ]; then
   echo "==> "$ioc create SCREEN socket FAILED
   cd $CWD
   exit 2
fi

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


