#!/bin/csh -f

# whois on the system seems kinda useless these days (11/11/18)
# It's therefore free to use this to query my LDAP-cache

switch (`uname -s``uname -r`)
case SunOS4*:
case SunOS5*:
case OSF1*:
	set WHOIS = /usr/ucb/whois
	breaksw
case Linux2*:
	set WHOIS = /usr/bin/whois
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

while($#argv)
	switch ("$1")
	case -h:
		set SERVER = $2
		shift; shift
		breaksw
	case -d:
		set DEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -*:
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	default:
		if (! $?TARGET) then
			set TARGET = $1
			shift
			breaksw
		endif
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

goto nwhois

if (! $?TARGET) exec echo "usage: whois [ -h host ] name"

set BASE = `basename $0`
set TMP = /tmp/$BASE.tmp
if (! -w $TMP) set TMP = $TMP.$$
#if (! $?SERVER) set SERVER = rs.internic.net
# above no longer seems to respond; 10/31/08
if (! $?SERVER) set SERVER = whois.arin.net

while (! $?DONE) 
	if ($?VERBOSE) echo `date +%T` $WHOIS -h $SERVER $TARGET
	$WHOIS -h $SERVER $TARGET >& $TMP
	set NSERVER = `grep "Whois Server: " $TMP | awk '{print $NF}'`
	if ("$NSERVER" != "") then
		if ($?VERBOSE) sed 's;\(.\);    \1;' $TMP
		set SERVER = $NSERVER
	else
		echo `date +%T` Results from ${SERVER}:
		cat $TMP
		set DONE
	endif
end

/bin/rm -f $TMP

nwhois:

set TFILE = ~watrous/LDAP-cache/$TARGET

if (-e $TFILE) then
    set CN = `grep ^cn: $TFILE | sed 's;cn:;;'`
    set TITLE = `grep ^title: $TFILE | sed 's;title:;;'`
    set TYPE = `grep ^employeeType: $TFILE | sed -e 's;employeeType:;;' -e 's;.$;&,;'`
    set TYPE = `echo $TYPE | sed 's;,$;;'`
    if ($?DEBUG) then
	echo ""
	cat $TFILE
	echo ""
    endif
    echo $TARGET is $CN, $TITLE "("$TYPE")"
else
    echo $TFILE does not exist
endif
