#!/bin/ksh
#
# The scr_kill scripts kills any remaining enterprise server java processes.
#
#set -xv

if [ "$G1SYS" = "" ]
then
   . ./setup
fi
: ${G1SYS:?}
$G1ECHO "\nExecuting server stop script ..."
if [ "$G1SYS" = "LINUX" ]
then
   UNC_USER=$(ps -e -o user -o pid -o cmd --cols 180|grep "$G1DCG/server" | grep -v grep | awk -F" " 'NR==1 {print $1;}')
else
   UNC_USER=$(ps -ef|grep "$G1DCG/server" | grep -v grep | awk -F" " 'NR==1 {print $1;}')
fi
if [ "$UNC_USER" = "" ]
then
#   $G1ECHO "\nThe Pitney Bowes Spectrum(TM) Technology Platform processes do not exist.  Nothing to stop. \n"
   exit
else
   if [ "$UNC_USER" != "root" ]
   then
      $G1ECHO "\nYou must be logged in as user:$UNC_USER or have ROOT privileges to stop the Pitney Bowes Spectrum(TM) Technology Platform processes"
   else
      $G1ECHO "\nYou must have ROOT privileges to stop the Pitney Bowes Spectrum(TM) Technology Platform processes"
   fi
   $G1ECHO "Otherwise, you will not have permission to kill the server processes. \n"
   #$G1ECHO "Press 'Enter' key to continue (Ctrl+C to quit) \c"
   #read xxx
fi
$G1ECHO "\nThe following processes will be killed: \n"
if [ "$G1SYS" = "LINUX" ]
then
   ps -e -o pid -o cmd --cols 180|grep "$G1DCG/server"|grep -v grep
else
   ps -ef|grep "$G1DCG/server"|grep -v grep
fi
#$G1ECHO "\nPress 'Enter' key to continue (Ctrl+C to quit) \c"
#read xxx
$G1ECHO ""
$G1ECHO "Stopping processes..."
status=0
if [ "$G1SYS" = "LINUX" ]
then
   for a in $(ps -e -o pid -o cmd --cols 180|grep "$G1DCG/server" | grep -v grep | awk '{print $1}')
   do
      kill -9 $a >/dev/null 2>&1
      status=$?
   done
else
   for a in $(ps -ef|grep "$G1DCG/server" | grep -v grep | awk '{print $2}')
   do
      kill -9 $a >/dev/null 2>&1
      status=$?
   done
fi 
if [ $status -ne 0 ]
then
   $G1ECHO "Stop server process failed.\n"
   exit
fi
if [ "$G1SYS" = "LINUX" ]
then
   for a in $(ps -e -o pid -o cmd --cols 180|grep unicoder|grep -v grep |grep -v "unc\/server\/java"| awk '{print $1}')
   do
      kill -9 $a >/dev/null 2>&1
      status=$?
   done
else
   for a in $(ps -ef|grep unicoder|grep -v grep |grep -v "unc\/server\/java"| awk '{print $2}')
   do
      kill -9 $a >/dev/null 2>&1
      status=$?
   done
fi
if [ $status -eq 0 ]
then
   $G1ECHO "The Pitney Bowes Spectrum(TM) Technology Platform processes have been stopped.\n"
else
   $G1ECHO "Stop server process failed.\n"
fi
exit
