#!/bin/csh -f
set com = $0
set pid = $$

/etc/renice +4 -p $pid > /dev/null		# run at low priority

set load = `uptime | awk -F. '{print $1}'`
@ loadavg = $load[$#load]
if ($loadavg >= 7) then
	echo 'This program cannot be run when the 1-minute load average exceeds 7.'
	exit
endif

onintr intrend				# if interrupted, goto intrend

if ($#argv > 2) then			# if too many arguments
	echo 'Too many arguments.'
	goto err
endif

touch /site/usr/tmp/monitor.$user.$pid.old

set file = $HOME/$1
if ($1 == '' || $1 == '-lis') then
	set file = $HOME/.anyone
endif
if (! -e $file) then			# if file doesn't exist
	echo "Can't find file" $file
	goto err
endif

if ($1 == '-lis' || $2 == '-lis') then
	set monlis = 'true'
else
        set monlis = 'false'
endif
if ($1 == '-lis' && $2 != '') then
	echo 'Invalid argument' $2
	goto err
endif

set off = `rwho -a | egrep '^'$user' '`		# Make sure person running
set on = `echo $off | awk '{ print $1 }'`	# monitor is logged in
unset off

while ($on == $user)			# run while person's logged in
    rsh ringworld -n  rwho -a | fgrep -fi $file >&! /site/usr/tmp/monitor.$user.$pid.present
   rsh trillian -n rwho -a | fgrep -fi $file >>&! /site/usr/tmp/monitor.$user.$pid.present
    rsh apollo -n rwho -a | fgrep -fi $file >>&! /site/usr/tmp/monitor.$user.$pid.present
    rsh zeus -n rwho -a | fgrep -fi $file >>&! /site/usr/tmp/monitor.$user.$pid.present
    rsh clio -n rwho -a | fgrep -fi $file >>&! /site/usr/tmp/monitor.$user.$pid.present
    rlogin achilles -l rwho -a >>&! /site/usr/tmp/monitor.$user.$pid.achilles
    fgrep -fi $file /site/usr/tmp/monitor.$user.$pid.achilles >>&! /site/usr/tmp/monitor.$user.$pid.present
    rsh M4-035-T -n rwho -a | fgrep -fi $file >>&! /site/usr/tmp/monitor.$user.$pid.present
    rlogin media-lab -l rwho -a >>&! /site/usr/tmp/monitor.$user.$pid.amt
    fgrep -fi $file /site/usr/tmp/monitor.$user.$pid.amt >>&! /site/usr/tmp/monitor.$user.$pid.present
    rlogin borax.lcs.mit.edu -l rwho -a >>&! /site/usr/tmp/monitor.$user.$pid.borax
    fgrep -fi $file /site/usr/tmp/monitor.$user.$pid.borax >>&! /site/usr/tmp/monitor.$user.$pid.present
    rlogin vx.lcs.mit.edu -l who >>&! /site/usr/tmp/monitor.$user.$pid.vx
    fgrep -fi $file /site/usr/tmp/monitor.$user.$pid.vx | sed s/tty/vx:tty/ >>&! /site/usr/tmp/monitor.$user.$pid.present
#   rlogin eddie -l who >>&! /site/usr/tmp/monitor.$user.$pid.eddie
#   fgrep -fi $file /site/usr/tmp/monitor.$user.$pid.eddie | sed s/tty/eddie:tty/ >>&! /site/usr/tmp/monitor.$user.$pid.present

    # /site/usr/tmp/monitor.$user.$pid.present is the file containing
    # everyone in $file who's in the rwho output of any of the above
    # machines. 

    cat /site/usr/tmp/monitor.$user.$pid.present | awk '{ printf "%-9.8s %-21.20s %-4.3s %-3.2s %-6.5s \n", $1, $2, $3, $4, $5 }' >&! /site/usr/tmp/monitor.$user.$pid.current
    # /site/usr/tmp/monitor.$user.$pid.current is the first five items
    # on each line of the rwho 
    # output from /site/usr/tmp/monitor.$user.$pid.present.  This
    # removes the amount of idle time, so fgrep doesn't think the
    # entry is different if the number of minutes idle changes.

    touch /site/usr/tmp/monitor.$user.$pid.deep-thought /site/usr/tmp/monitor.$user.$pid.xx /site/usr/tmp/monitor.$user.$pid.oz
    finger @deep-thought@xx.lcs.mit.edu | fgrep -fi $file >&! /site/usr/tmp/monitor.$user.$pid.deep-thought
    finger @xx.lcs.mit.edu | fgrep -fi $file >&! /site/usr/tmp/monitor.$user.$pid.xx
    finger @oz.ai.mit.edu@xx.lcs.mit.edu | fgrep -fi $file >&! /site/usr/tmp/monitor.$user.$pid.oz
#   finger @borax.lcs.mit.edu | fgrep -fi $file >&! /site/usr/tmp/monitor.$user.$pid.borax
#   finger @vx.lcs.mit.edu | fgrep -fi $file >&! /site/usr/tmp/monitor.$user.$pid.vx
#   finger @media-lab.mit.edu | fgrep -fi $file >&! /site/usr/tmp/monitor.$user.$pid.amt
    # dump finger output from selected other machines
    # into /site/usr/tmp/monitor.$user.$pid.machine-name 

    cat /site/usr/tmp/monitor.$user.$pid.deep-thought | awk '{printf "%-.34s DEEP-THOUGHT.MIT.EDU \n", $0}' >>&! /site/usr/tmp/monitor.$user.$pid.current
    cat /site/usr/tmp/monitor.$user.$pid.xx | awk '{printf "%-.34s XX.LCS.MIT.EDU \n", $0}' >>&! /site/usr/tmp/monitor.$user.$pid.current
    cat /site/usr/tmp/monitor.$user.$pid.oz | awk '{printf "%-.34s OZ.AI.MIT.EDU \n", $0}' >>&! /site/usr/tmp/monitor.$user.$pid.current
#   cat /site/usr/tmp/monitor.$user.$pid.borax | awk '{printf "%-.42s BORAX.LCS.MIT.EDU \n", $0}' >>&! /site/usr/tmp/monitor.$user.$pid.current
#   cat /site/usr/tmp/monitor.$user.$pid.vx | awk '{printf "%-.33s VX.LCS.MIT.EDU \n", $0}' >>&! /site/usr/tmp/monitor.$user.$pid.current
#   cat /site/usr/tmp/monitor.$user.$pid.amt | awk '{printf "%-.33s MEDIA-LAB.MIT.EDU \n", $0}' >>&! /site/usr/tmp/monitor.$user.$pid.current
    # dump the first 34 characters (username and real name) into
    # /site/usr/tmp/monitor.$user.$pid.current.  Characters after the
    # first 34 include the command names, which would make fgrep think
    # the entry changed and print it.

    sort +0df +1 -o /site/usr/tmp/monitor.$user.$pid.sorted -T /site/usr/tmp /site/usr/tmp/monitor.$user.$pid.current
    touch /site/usr/tmp/monitor.$user.$pid.new
    fgrep -v -f /site/usr/tmp/monitor.$user.$pid.old /site/usr/tmp/monitor.$user.$pid.sorted >&! /site/usr/tmp/monitor.$user.$pid.new
    if (! -z /site/usr/tmp/monitor.$user.$pid.new) then	# /site/usr/tmp/monitor.$user.$pid.new is people who have
        echo 				# logged in since the last pass.
        echo 'Logged in:'		# if this file is not zero length,
        cat /site/usr/tmp/monitor.$user.$pid.new		# cat it.
        if ($monlis == 'true') then
            set monlis = 'doit'
        endif
    endif

    touch /site/usr/tmp/monitor.$user.$pid.left
    fgrep -v -f /site/usr/tmp/monitor.$user.$pid.sorted /site/usr/tmp/monitor.$user.$pid.old >&! /site/usr/tmp/monitor.$user.$pid.left
    if (! -z /site/usr/tmp/monitor.$user.$pid.left) then	# /site/usr/tmp/monitor.$user.$pid.left is people who have
        echo 				# logged out since the last pass.
        echo 'Logged out:'		# if this file is not zero length,
        cat /site/usr/tmp/monitor.$user.$pid.left		# cat it.
        if ($monlis == 'true') then
            set monlis = 'doit'
        endif
    endif

    if ($monlis == 'doit') then
        echo ''
        echo 'Current users:'
        cat /site/usr/tmp/monitor.$user.$pid.sorted
        set monlis = 'true'
    endif

    \mv /site/usr/tmp/monitor.$user.$pid.sorted /site/usr/tmp/monitor.$user.old
    \rm -f /site/usr/tmp/monitor.$user.$pid.*
    \mv /site/usr/tmp/monitor.$user.old /site/usr/tmp/monitor.$user.$pid.old
    # /site/usr/tmp/monitor.$user.$pid.old is now the current file;
    # all others get deleted. 
    sleep 60			# be at least marginally load-conscious
    set off = `rwho -a | egrep '^'$user' '`	# make sure you're still
    set on = `echo $off | awk '{ print $1 }'`	# logged in for the above
    unset off					# while statement.
end
echo "Can't find you in rwho.  Try again in a minute or so."
\rm -f /site/usr/tmp/monitor.$user.*		# remove file when finished.
exit 0

err:					# error ending
	echo 'Usage:' $com:t '[file] [-lis]'
	\rm -f /site/usr/tmp/monitor.$user.*
	exit 1

intrend:	# if interrupted, remove files created and exit.
	\rm -f /site/usr/tmp/monitor.$user.*
	echo 
	if ($monlis == 'true') then
	    echo $com:t $file:t '-lis interrupted...exiting.'
	else
	    echo $com:t $file:t 'interrupted...exiting.'
	endif
	exit 2

Have fun with it.
						Jeff Bigler
c


