#!/bin/sh
# whois

#set -x

# variables

TITLE="\
           whois: Network Information Center database (2945 bytes)\
"
MENU="\
          [s]end WHOIS commands | [p]rint the page | [q]uit | [h]elp\
"
NICHOST=whois.internic.net
PORT=whois

help()
{
cat /usr/doc/help/whois
}

refresh()
{
CMD=$*
cat > /tmp/whois.cnt <<END 
:T: $TITLE 
:B: $MENU 


          _           _
__      _| |__   ___ (_)___   _
\ \ /\ / / '_ \ / _ \| / __| (_)	Looks up records in the 
 \ V  V /| | | | (_) | \__ \  _         NIC database.
  \_/\_/ |_| |_|\___/|_|___/ (_)        -- Host:$NICHOST 	

   `date`

END

# prepare the muless page

if [ "$CMD" ] ; then
	echo -e "$CMD\r\n" | nc -vv $NICHOST $PORT 2>&1 >> /tmp/whois.cnt
fi

kill -10 $pid 2>/dev/null 
}

# passed by muless
pid=$2
set -- $1
key=$1
link=$3

# first run

if [ -z "$key" ] ; then
refresh 
muless -e $0 -s "\
               whois, Rustic(TM) Software Inc - 1999 M. Andreoli\
" /tmp/whois.cnt
exit
fi


case $key in
q) #quit
	echo "bye"
	rm /tmp/whois.cnt
	;;
h) #help
	help | muless -t "whois Help" -b "[q] quit help" 
	;;
s) #send whois command
	read -p "Enter whois command: " cmd
	refresh $cmd 
	;;
p) #print
	lpr /tmp/whois.cnt
	sleep 2
	;;	
esac


