#!/bin/bash
#
# Description: Kill the process group id contained in the first line, of the lines
#              returned by the "ps -Aww -o pid,pgid,args" command, that has 
#              the token in it.
#
# Change Activity:
#   10/19/2004 P. Callaghan - initial version
#
#STARTUSAGE
#
# Usage:
#   killGroupOf token
#
# Where:
#   token is the token/string to check for in the lines.
#
#ENDUSAGE
#****************************************************************************

# Function to return the process group ID of the PID running the specified program.
# Arguments
#  program
getpgid() {
   # Loop through "ps" output to find the PGID.
   ps -Aww -o pid,pgid,args | while read -r pid pgid cmd; do
      #echo $pgid
      #echo $cmd
      if [ "$cmd" == "$1" ]; then
         echo $pgid   # Return the PGID to our caller.
         break
      fi
   done
}

echo "killGroupOf called with argument of $1"

if [ $# -lt 1 ] ; then
exit 1
fi

pgid=$(getpgid "$1")
if [ -z $pgid ]; then
   exit 2
else
   #echo kill -9 -$pgid
   kill -9 -$pgid 
   exit $?
fi
