#!/bin/bash
#
# This is a very simple installation script. It may not work on 
# all systems. Please use it with care and caution. More detailed 
# instructions are available in the INSTALL file. 
#
# --Bill
#

HELLO="BW Whois Installation Script version 3.1 (27-July-2002)"
echo $HELLO

# check to see that I'm running as root
WHOAMI=`whoami`
if [ $WHOAMI != "root" ] ; then 
  echo "You must run this program as root. You are logged in as $WHOAMI."
  exit
fi

# check to see that /etc exists. If not, this is not unix and I don't 
# know what to do about it. 
if [ ! -d /etc ] ; then 
   echo "You don't have an [/etc] directory. You will have to install"
   echo "manually. Please see the INSTALL file."
   exit
fi

# make sure the configuration directory exists
CONFPATH=/etc/whois
if [ ! -d $CONFPATH ] ; then
  echo "Creating directory [$CONFPATH] . . . "
  mkdir $CONFPATH
fi

# used below for copying files
function copyfile
{
echo "Installing [$DEST]."

# check for existing destination file
if [ -f $DEST ] ; then
  echo
  echo    "The file [$DEST] already exists. "
  echo -n "Shall I erase it? (Y/N) "
  read YN
  if [ $YN == "Y" -o $YN == "y" ] ; then 
    rm $DEST
    echo "[$DEST] erased."
  fi
fi

if [ ! -f $DEST ] ; then 

  # do the copy
  echo "Copying [$SOURCE] to [$DEST]."
  cp $SOURCE $DEST  

  # set the mode
  if [ ! -z $MODE ] ; then 
    chmod $MODE $DEST 
  fi

  # is there a conflict?
  if [ ! -z $CHECK -a -f $CHECK ] ; then 
    echo
    echo    "The file [$CHECK] may conflict with the file [$DEST]. "
    echo -n "Shall I erase [$CHECK]? (Y/N) "
    read YN
    if [ $YN == "Y" -o $YN == "y" ] ; then 
      rm $CHECK
      echo "[$CHECK] erased."
    fi
  fi

fi

}

SOURCE=whois
DEST=/usr/local/bin/whois
CHECK=/usr/bin/whois
MODE=0755
copyfile

SOURCE=whois.1
DEST=/usr/local/man/man1/whois.1
CHECK=/usr/man/man1/whois.1
MODE=0644
copyfile

SOURCE=whois.conf
DEST=${CONFPATH}/whois.conf
CHECK=""
MODE=0644
copyfile

SOURCE=tld.conf
DEST=${CONFPATH}/tld.conf
CHECK=""
MODE=0644
copyfile

SOURCE=sd.conf
DEST=${CONFPATH}/sd.conf
CHECK=""
MODE=0644
copyfile

SITE_PERL_LIST="
  /usr/local/lib/site_perl
  /usr/local/lib/perl5/site_perl
  /usr/lib/site_perl
  /usr/lib/perl5/site_perl
"

for SITE_PERL in $SITE_PERL_LIST
  do if [ -d $SITE_PERL ] ; then 
    PM_DEST=${SITE_PERL}/bwInclude.pm
    SOURCE=bwInclude.pm
    DEST=$PM_DEST
    CHECK=""
    MODE=0644
    copyfile
    break
  fi
done

echo
echo "The following are the files that were to have been "
echo "installed. Please check this listing to make sure "
echo "The date stamps and file modes are correct. "
echo "===================================================="

ls -l  \
  /usr/local/bin/whois \
  /usr/local/man/man1/whois.1 \
  ${CONFPATH}/whois.conf \
  ${CONFPATH}/tld.conf \
  ${CONFPATH}/sd.conf \
  $PM_DEST

echo "===================================================="

echo "Done."

