#!/bin/bash -norc
#==============================================================
#
#  Abs:  Stops Python scripts
#
#  Name: Python_stop
#  $1 :  Python script to stop
#  $2 :  Python parameter
#
#  EX)
#      Python_stop femto.py VITARA2
#
# ------------------------------------------------------------
# This script gets called from lclshome or facethome which
# performs an SSH call to lcls-srv03  or facet-srv02 and kills
# the python script provided
# ------------------------------------------------------------
#
#  Facility:  SLAC
#
#  Auth: 01-Oct-2013, Brobeck
#
#  MOD:
#
#  22-Sep-2014, Brobeck
#    Added support for test facilities
#
#  14-Oct-2013, Brobeck
#    Added Facet support
#==============================================================
#
##########################################################
# -Check to see what user is running script and which
#  server
##########################################################

if [ "$USER" == physics ] || [ "$USER" == laci ]
  then

     # Make sure physics/laci is running the script on lcls-srv03
     #
     if  [ `uname -n` != "lcls-srv03" ]
       then
           echo ""
	   echo "This script must be run from the physics or laci account"
	   echo "on lcls-srv03 - Preferably from lclshome"
	   echo ""
	   exit
     fi
  elif [ "$USER" == fphysics ] || [ "USER" == flaci ]
    then
           if [ `uname -n` != "facet-srv02" ]
	     then
	         echo ""
		 echo "This script must be run from the fphysics or flaci account"
		 echo "on facet-srv02 - Preferably from facethome"
		 echo ""
		 exit
	   fi
  elif [ "$USER" == acctf ]
    then
           if    [ `uname -n` != "testfac-srv02" ]
	     then
	         echo ""
		 echo "This script must be run from the acctf account"
		 echo "on testfac-srv02 - Preferably from astahome"
		 echo ""
		 exit
	   fi
   else
           printf "\n\n\tThis script starts up the Python scripts for\n"
	   printf "\tLCLS, FACET, or ASTA.\n\n"
	   printf "\tLCLS:\t physics and laci accounts on lcls-srv03\n"
	   printf "\tFacet:\tfphysics and flaci accounts on facet-srv02\n"
	   printf "\tASTA:\tacctf account on testfac-srv02n\n\n\n"
	   exit
fi


MDATE=`date '+%d_%b_%Y_%H_%M'`

########################
# kill the process ID
########################
PID=`ps awwx | grep "python.*$1.*$2" | grep -v grep | awk '{print $1}'`
if [ ! "$PID" = "" ]; then
   kill -kill $PID
   echo "****************************************" >> $PHYSICS_DATA/log/python/$1$2.log
   printf "\n\tStopping $1 $2 ($MDATE)\n\n" >> $PHYSICS_DATA/log/python/$1$2.log
fi

sleep 5
########################
# kill the tail files
########################
TPID=` ps awwx | grep "tail.*$1$2" | grep -v grep | awk '{print $1}'`
if [ ! "$TPID" = "" ]; then
   kill -kill $TPID
fi
