#!/bin/sh
# Runs the RUN_PROG
# LOOPS number of times
# at SLEEP_TIME second intervals.
#
# ottrey@ripe.net

whois_port=3333
loops=30
sleep_time=2

USAGE="$0 [-p $whois_port] [-l $loops] [-s $sleep_time] [-c] \n"
USAGE="$USAGE where -p = whois_port                          \n"
USAGE="$USAGE       -l = loops                               \n"
USAGE="$USAGE       -s = sleep_time                          \n"
USAGE="$USAGE       -c = clear                               \n"

# Aaaaargh... this isn't working portably.
#set -- `getopt p:l:s:ch $*`
for i in $*
do
     case $i in
     -p)       whois_port=$2; shift 2;;
     -l)       loops=$2; shift 2;;
     -s)       sleep_time=$2; shift 2;;
     -c)       will_clear=$i; shift;;
     -h)       printf "$USAGE"; exit;;
     --)       shift; break;;
     esac
done

echo whois_port=$whois_port
echo loops=$loops
echo sleep_time=$sleep_time
echo will_clear=$will_clear

while [ $loops != 0 ];
do
  if [ $will_clear ]; then clear; else echo ""; fi
  query=`tail -1 /ncc/db2/log/qry/qry2log`
  inv_options=`echo $query  | grep 'i=' | sed 's/.*i=/ -i /g' | sed 's/[]:].*//g'`
  object_types=`echo $query | grep 'T=' | sed 's/.*T=/ -T /g' | sed 's/[]:].*//g'`
#  if [ $inv_options ]; then printf '\7'; fi
  search_term=`echo $query | sed 's/.*\[.*\]//g'`
  whois_query=`echo "$inv_options $object_types $search_term"`
  echo "whois_query =$whois_query"
#  echo $search_term | SQWQWT/test_SQWQWT -v
#  telnetin "whois `echo $search_term`"
#  /space/dbdev/ottrey/whois_client/whois -h rowan -p $whois_port `echo $whois_query`
  /home/ottrey/bin/whoisj -h rowan -p $whois_port `echo $whois_query`
#  whois -h sequoia " -r `echo $whois_query`"
  loops=$(($loops-1))
  echo $loops
  sleep $sleep_time
done
