#!/bin/csh -f

if (foo$1 == foo) then
	set schmuck = kkkken
else
	set schmuck = $1
endif
set foundat = ()

echo "--------------------"
echo " Hunting for $schmuck"
echo "--------------------"
echo ""
echo -n "Asking zephyr ..."

set where = (`zlocate $schmuck | awk '/^([^H]|H[^i])/ {print $1}'`)
if ( $#where == 0) then
	echo zephyr says nothing useful.
else
    foreach host ($where)
	echo ""
	echo Zephyr says to look at $host.
	echo ""
	finger @$host | tee /dev/tty | awk '/^'${schmuck}'/ {print $1}' > /tmp/fbar.$$
	echo ""
	if ( -z /tmp/fbar.$$ ) then
		echo It looks like zephyr lied.
	else
		set foundat = ($host $foundat)
		echo And there he is\!
	endif
    end
endif

echo ""
echo "Now let me check the log..."
echo ""
set logfile = /afs/athena/user/k/${schmuck}/lib/log
if ( ! -r $logfile ) then
	echo "Oops --- there's no log file\!"
else
	set logh = (`cat $logfile | tee /dev/tty | grep "Logged in" | head -3 | awk '{print $4}'`)
	if ( $#logh == 0) then
		echo weird -- no logins in a while... hmmm..
	else
		foreach host ($logh)
			echo "Let me try $host ..."
			echo ""
			finger @$host | tee /dev/tty | awk '/^'${schmuck}'/ {print $1}' > /tmp/fbar.$$
			if ( -z /tmp/fbar.$$ ) then
				echo "Not there..."
			else
				echo "Aha\!  He can't hide\!"
				set foundat = ($host $foundat)
			endif
		end
	endif
endif

echo ""
if ( $#foundat == 0 ) then
	echo "Sorry\!  That's all I can do."
	echo ""
	exit
endif

foreach machine ($foundat)
	echo -n "Shall I try to talk at $machine ? "
	set foo = $<
	if ($foo == "y" || $foo == "yes") then
		talk $schmuck@$machine
		exit
	endif
endif
