#!/bin/sh
 
#
# COPYRIGHT    2001
# THE REGENTS OF THE UNIVERSITY OF MICHIGAN
# ALL RIGHTS RESERVED
#
# Permission is granted to use, copy, create derivative works
# and redistribute this software and such derivative works
# for any purpose, so long as the name of The University of
# Michigan is not used in any advertising or publicity
# pertaining to the use of distribution of this software
# without specific, written prior authorization.  If the
# above copyright notice or any other identification of the
# University of Michigan is included in any copy of any
# portion of this software, then the disclaimer below must
# also be included.
#
# THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
# FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
# PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY O
# MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
# WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
# REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
# FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
# CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
# OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
# IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGES.
#

# provides "kill by string" functionality
# signal 0 (-0) just prints the pid 
# returns 0 if at least one process found, 1 if no process found, 2 if error
# no search string can contain "grep" or "kbs"

SIG=
PROCS='('
while [ -n "$1" ]; do
	case $1 in
		-*)	SIG=$1;;
		*)	PROCS="$PROCS$1|";;
	esac
	shift
done
if [ "$PROCS" = "(" ]; then echo "Usage:  $0 [-sig] strings ..."; exit 2; fi

PROCS="${PROCS}mxyptlk)"
PIDS=`ps ax |  tee owt | grep -v grep | grep -v kbs | egrep "$PROCS" | awk '{ print $1 }'`

if [ -n "$PIDS" ]; then
	CMD="kill $SIG $PIDS"
	echo + $CMD
	if [ "$SIG" != '-0' ]; then
		$CMD
	fi
	exit 0
else
	echo "$0:  no processes found"
	exit 1
fi
