#!/bin/sh
#
# slay 1.2 - kill all processes belonging to the specified user(s).
# by Chris Ausbrooks <fish@bucket.ualr.edu> 
# based on kall (a script of unknown origin)
# Please mail me any suggestions or corrections.
# Donations welcome.

# Revision history:
# 0.99	First attempt.
# 1.0	Added Butthead.
# 1.1	Added retribution.
# 1.2	Added slayee notification.

USER=`whoami`
SIGNAL=`echo $1 | grep '^\-.*'`
ME=`basename $0`
COOL='0'
MODE='mean'	# Mean mode gives retribution for incorrect usage.
BH='off' 	# Turning on BH makes the responses more interesting...
		# Try it sometime.

# Command line handling.
if [ "$SIGNAL" != "" ]; then
	shift
else
	SIGNAL="-KILL"
fi

# Help for loosers.
if [ "$1" = "" -o "$1" = "--help" ]; then
	echo "usage: $ME [-signal] name [name...]"
	if [ "$BH" = "on" ]; then
		echo "       Like, kills people and stuff."
		else echo "       Kills all processes belonging to any of the given names."
	fi
	exit -1 
fi

# Misuse trap.
if [ "$USER" != "$1" ]; then
	if [ "$USER" != "root" ]; then
		if [ "$MODE" = "mean" ]; then
			$ME -KILL $USER
			else if [ "$BH" = "on" ]; then
	 			echo "${ME}: Cut it out."
				else echo "${ME}: Only root gets to do that."
			fi
		fi
		exit 2
	fi
fi

# Main body.
while [ "$1" != "" ]; do

# NOTE:  You may have to modify the next line, since PS is non-portable.
# The 'awk' command picks out the process IDs to pass them on to kill.
	rprocs=`ps caux | awk '{if(user == $1) print $2}' user=$1 -`
	if [ "$rprocs" != "" ]; then
		if [ "$1" = "$USER" ]; then
			if [ "$BH" = "on" ]; then
				echo "${ME}: Beavis, don't make me have to smack you."
				else echo "${ME}: Illegal operation."
			fi
		fi
		COOL="1"
		if [ "$BH" = "on" ]; then
			echo "${ME}: $SIGNAL is kicking $1's butt!"
			echo -e "\\fI'm kicking your butt.\\f" | write $1
			else echo "${ME}: Sending $SIGNAL signal to $1's process(es)..."
			echo -e "\\fYour current session has been terminated.\\f" | write $1
		fi
		kill $SIGNAL $rprocs
	fi
	shift
done

# Error message.
if [ $COOL = "0" ]; then
	if [ "$BH" = "on" ]; then
		echo "${ME}: How old are you, Beavis?"
		else echo "${ME}: "Nothing done.
	fi
	exit 1
fi

# Non-error message.
if [ $COOL = "1" ]; then
	if [ "$BH" = "on" ]; then
		echo "${ME}: Whoa, I have the power supreme."
		else echo "${ME}: Done."
	fi
	exit 0
fi
