#!/bin/sh

# killme sends a specified signal to all processes owned by the user
# running the command. If run by root, a user must be specified. The default
# signal is -HUP.

me=`whoami`
[ "$me" ] || exit

# When run by root, a non-root process owner must specified:
if [ $me = "root" ] ; then
    if [ $1 ] ; then
	me=$1
	echo "Will kill processes owned by $me ..."
	shift
    else
	echo "Usage as root: killme user [signal]"
	echo "  signal is -HUP by default"
	exit
    fi
    if [ $me = "root" ] ; then
	echo "root cannot kill root processes this way"
	exit
    fi
fi

# Get the signal to send:
[ $1 ] && flag=$1
[ $flag ] || flag="-HUP"

thishost=`hostname`

echo "
This command will kill all of your processes on the cunix hosts.

OK to continue? [y/n] "

read response
if [ "$response" != "y" ] ; then
    exit
fi

echo "
Killing your processes on other hosts
Please wait while we check each host...
Press Ctrl-C to stop
"

# Run skill or pkill on the various timeshare hosts:
for host in `ourhosts -t +h $thishost`; do
    echo $host ...
    /opt/local/bin/timeout 15 rsh $host 'u=`uname -r`; 
    [ $u = 5.5.1 ] && killcmd=/opt/local/bin/skill; 
    [ $u != 5.5.1 ] && killcmd=/usr/bin/pkill; 
    $killcmd $flag -u $me 2>/dev/null'
done

# Check whether to kill processes on the current host:
echo "
Done.

Killing processes on THIS host may log you out! OK to do that? [y/n] "
read response
if [ "$response" != "y" ] ; then
    echo "
NOT killing on this host. For your information, these are the processes
you have running on this host:"
    ps
    exit
else
    
    # Use skill on Solaris 2.5 and pkill on Solaris 2.6+:
    if [ `uname -r` = 5.5.1 ]; then
	killcmd=/opt/local/bin/skill
    else
	killcmd=/usr/bin/pkill
    fi
    exec $killcmd $flag -u $me 2>/dev/null
fi

