#!/bin/sh
############################################################## 
# whoiss, a wrapper for BSD-Unix whois 
# available from ftp://ftp.roble.com/unix/whoiss 
# marquis@roble.com
# 
# to_do: add error handling code for non-arin netblocks 
# see_also: http://mjhb.marina-del-rey.ca.us/cgi-bin/ipw.pl
############################################################## 
# $Id: whoiss,v 1.21 1999/03/17 01:04:28 marquis Exp marquis $
##############################################################

##### find a decent file viewer #####
if [ -x /usr/local/bin/less ]; then
	PAGER=${PAGER:-"/usr/local/bin/less -cein"} 
else
	PAGER=${PAGER:-more} 
fi

##### qualify the input #####
if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
	echo "USAGE: `basename $0` [-apnic|-a|-ripe|-r] <domainname|subnet|handle> [<...>]" 
	exit 1
fi

case $1 in
	##### allow user-specified (reverse) servers for apnic & ripe #####
	# (kludge: also defined below)
	-ripe|-r)
		mwserver=whois.ripe.net
		;; 
	-apnic|-a)
		mwserver=whois.apnic.net 
		;; 
	*)
		# else use default (arin)
		mwserver="" 
		;;
esac

for domain in $* ; do
	##### parse out the significant top level domain #####
#	DOMAIN="`echo $domain |tr [A-Z] [a-z]`"
#	TLD="`echo $DOMAIN | awk -F. '{print $NF}'`"
	TLD="`echo $domain | awk -F. '{print $NF}'`"

	##### determine the whois server #####
	case $TLD in
		-ripe|-apnic|-arin|-a|-r)
			# user defined server (kludge: also defined above)
			continue
			;;
		com|net|org|edu)
			#whois.internic.net, root monopoly holder
			wserver=whois.internic.net
			;;
		us)
			wserver=whois.isi.edu
			;;
		gov)
			wserver=whois.nic.gov
			;;
		ca)
			# per Richard Sexton  280se@mbz.org/richard@ns1.vrx.net
			wserver=ns3.vrx.net
			;;
		au)
			if [ "`echo $DOMAIN | sed 's/.au$//' | awk -F. '{print $NF}'`" = net ]; then
				wserver=whois.connect.com.au
			else
				wserver=whois.aunic.net
			fi
			;;
		cn|hk|kr|jp|tw)
			#whois.apnic.net, Asia and the Pacific region
			wserver=whois.apnic.net
			;;
		nl)
			wserver=domain-registry.nl
			;;
		al|am|at|az|ba|be|bg|by|ch|cy|cz|de|dk|dz|ee|eg|es|fi|fo|\
		fr|gb|ge|gr|hr|hu|ie|il|is|it|li|lt|lu|lv|ma|md|mk|mt|\
		no|pl|pt|ro|se|si|sk|sm|su|tn|tr|ua|uk|va|yu)
			#whois.ripe.net, Europe, Middle East and parts of Africa
			wserver=whois.ripe.net
			;;
		[0-9]*|net*|*-arin)
			#whois.arin.net	: reverse IP mapping
			wserver=whois.arin.net
			;;
		ru)
			# russia, was: whois.ripe.net
			wserver=whois.ripn.net
			;;
		mil)
			#us military
			wserver=whois.nic.mil
			;;
		*)
			#rs.internic.net, North and South America and parts of Africa
			wserver=rs.internic.net
			;;
	esac
	
	##### query correct server #####
	if [ "$mwserver" != "" ]; then
		wserver=$mwserver
	fi
#	echo "--> Querying the $wserver server for ${domain}:"
#	echo ""
#	whois -h $wserver $DOMAIN | $PAGER
	whois $domain@$wserver | $PAGER

done
exit $?
