#!/bin/sh
#Copyright 1999 John Stracke <francis@thibault.org>
#Released under the GNU General Public License.

#Script to do a whois lookup and go to the right registrar's server.
# Syntax is as with fwhois: name[@host].

#Uncomment the line to pick the right whois program.  Note that setting
# FWHOIS implies the program named uses fwhois syntax; setting WHOIS
# implies the program uses whois syntax.

FWHOIS=fwhois
#WHOIS=whois

if
  [ "$FWHOIS" ]
then
  WHOIS_SIMPLE="$FWHOIS"
else
  if
    [ "$WHOIS" ]
  then
    WHOIS_SIMPLE="$WHOIS"
  else
    echo Neither WHOIS nor FWHOIS has been set.
    exit 1
  fi
fi

_whois() {
  if
    [ "$2" ]
  then
    if
      [ "$FWHOIS" ]
    then
      $FWHOIS "$1"@"$2"
    else
      $WHOIS -h $2 $1
    fi
  else
    $WHOIS_SIMPLE $1
  fi
}

if
  echo $1 | grep '@' >/dev/null
then
  _whois `echo $1 | cut -f1 -d@` `echo $1 | cut -f2 -d@`
else
  if
    echo $1 | grep '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$' >/dev/null
  then
    _whois $1 whois.arin.net
  else
    tmpfile=/tmp/whois-wrapper-tmp-$$
    _whois $1 >$tmpfile
    if
      grep 'Whois Server: ' $tmpfile >/dev/null
    then
      redirect=`grep 'Whois Server: ' $tmpfile | cut -f2 -d: | cut -f2 -d' '`
      _whois "$1" "$redirect"
    else
      cat $tmpfile
    fi

    rm $tmpfile
  fi
fi
