#!/bin/sh
#
# Quicky installation script that seems to be the right thing for
# most BSD based Un*xes.  Pass 1 arg: the location of the support files
# (i.e., the directory containing this file).
#
# install-finger /src/local/finger/support

if [ "$1" = "-client" ]; then
  makeclient=true
  shift
fi

if [ "$1" ]; then 
  SUPPORTDIR=$1
else
  SUPPORTDIR=.
fi

host=`/bin/hostname`

BINDIR=`grep '#define BinDir' ../config.h | awk '{ print $3; }'`
ETCDIR=`grep '#define EtcDir' ../config.h | awk '{ print $3; }'`
FINGERDIR=`grep '#define FingerDir' ../config.h | awk '{ print $3; }'`

if [ `pwd` != /etc ]; then
  rm -f psaux temp rc.local services inetd.conf
fi

if [ "$makeclient" ]; then
  cp hostconfig ${FINGERDIR}/hostconfig
fi

# Make a new inetd.conf which contains the right stuff.
echo "Creating a file that can replace /etc/inetd.conf..."
cat /etc/inetd.conf | grep -v fingerd >temp
cat ${SUPPORTDIR}/inetd.conf-add | sed -e "s@/usr/local/etc@$ETCDIR@g" >temp1
cat temp temp1 >inetd.conf
rm temp temp1

# Make a new services which contains the right stuff.
echo "Creating a file that can replace /etc/services..."
cat /etc/services | grep -v cfinger >temp
cat temp ${SUPPORTDIR}/services-add >services

echo "Checking out the processes on this machine..."
ps -aux | grep -v grep >psaux

echo "Finding process ID of inetd..."
INETDPID=`grep inetd psaux | awk '{ print $2 };'`

if [ "$makeclient" = "" ]; then
  echo "Finding out if you are running yellow pages..."
  RUNNINGYP=`grep yp psaux`

  if [ "$RUNNINGYP" != "" ]; then
    echo "You are.  Checking to see if this is a Yellow Pages server..."
    ISSERVER=`grep ypserv psaux`
  fi
fi

rm -f psaux

if [ "$ISSERVER" != "" ]; then
  rm -f clients
  if [ -d /export/root ]; then
     echo "Building a list of possible finger clients..."
     thisdir=`pwd`
     (cd /export/root;
      for i in *; do
        if [ -d $i ]; then echo "client	$i" >>$thisdir/clients; fi
      done
     )
     echo "\"`pwd`/clients\" is a possible list of clients for"
     echo "\"${FINGERDIR}/hostconfig\"."
  fi
fi
  
echo "\"`pwd`/inetd.conf\" is a replacement for \"/etc/inetd.conf\""
echo "\"`pwd`/services\" is a replacement for \"/etc/services\""
echo ""
echo "You should install the inetd.conf and services files in /etc,"
echo "making sure to save the old ones first."
echo ""
if [ "$makeclient" = "" ]; then
  echo "Would you like me to do that now (y/n)?"
  read reply
else
  reply=y
fi
if [ "$reply" = "y" -o "$reply" = "yes" ]; then
  if [ -f /etc/inetd.conf-orig ]; then
     echo "Not copying /etc/inetd.conf to /etc/inetd.conf-orig because"
     echo "that file already exists."
  else
     echo "Copying /etc/inetd.conf to /etc/inetd.conf-orig."
     cp /etc/inetd.conf /etc/inetd.conf-orig
  fi
  cp inetd.conf /etc/inetd.conf

  if [ -f /etc/services-orig ]; then
     echo "Not copying /etc/services to /etc/services-orig because that"
     echo "file already exists."
  else
     echo "Copying /etc/services to /etc/services-orig."
     cp /etc/services /etc/services-orig
  fi
  cp services /etc/services
fi

if [ "$RUNNINGYP" != "" ]; then
  if [ "$ISSERVER" != "" ]; then
     echo "This machine ($host) is a YP server for a domain.  You should do"
     echo "\`cd /var/yp; make services' to propagate the changes to"
     echo "${host}'s clients.  Would you like me to do that now (y/n)?"
     read reply
     if [ "$reply" = "y" -o "$reply" = "yes" ]; then
        cd /var/yp; make services
     fi
     echo "Since this machine is the YP server, you probably want to"
     echo "make it the GNU Finger server as well.  I can create the"
     echo "${FINGERDIR}/hostconfig file, as well as update"
     echo "the /etc/rc.local file to make it start the finger server."
     echo "Would your like me to do that now (y/n)?"
     read reply
     if [ "$reply" = "y" -o "$reply" = "yes" ]; then
        is_finger_server=yes
        echo "server	$host" >${FINGERDIR}/hostconfig
	echo "server	$host" >${SUPPORTDIR}/hostconfig
	if [ -f /etc/rc.local-orig ]; then
	  echo "Not copying /etc/rc.local to /etc/rc.local-orig because that"
	  echo "file already exists."
	else
	  echo "Saving /etc/rc.local in /etc/rc.local-orig."
	  cp /etc/rc.local /etc/rc.local-orig
	fi
	cat ${SUPPORTDIR}/rc.local-add | sed -e "s@/usr/local/bin@$BINDIR@g" >temp
	gawk -f ${SUPPORTDIR}/rc-local.awk /etc/rc.local >rc.local
	rm -f temp
	cp rc.local /etc/rc.local
     fi
  fi
  echo "This machine ($host) is a YP client.  The only thing that you"
  echo "really need to do now is to restart the inet daemon on this"
  echo "machine."
else
  if [ "$makeclient" = "" ]; then
    echo "Do you wish to make this machine be the GNU Finger server for"
    echo "the local site?  I can create the ${FINGERDIR}/hostconfig file,"
    echo "as well as update the /etc/rc.local file to make it start the"
    echo "finger server when the machine starts up."
    echo "Would you like me to do that now (y/n)?"
    read reply
    if [ "$reply" = "y" -o "$reply" = "yes" ]; then
       is_finger_server=yes
       echo "server	$host" >${FINGERDIR}/hostconfig
       if [ -f /etc/rc.local-orig ]; then
         echo "Not copying /etc/rc.local to /etc/rc.local-orig because that"
	 echo "file already exists."
       else
          echo "Saving /etc/rc.local in /etc/rc.local-orig."
	  cp /etc/rc.local /etc/rc.local-orig
       fi
       cat ${SUPPORTDIR}rc.local-add | sed -e "s@/usr/local/bin@$BINDIR@g" >temp
       gawk -f ${SUPPORTDIR}/rc-local.awk /etc/rc.local >rc.local
       rm -f temp
       cp rc.local /etc/rc.local
    fi
  fi
fi

if [ "$makeclient" = "" ]; then
  echo "It is a reasonable thing to restart the inet daemon on this machine now."
  echo "All you have to type is, \`kill -HUP $INETDPID'.  Would you like me"
  echo "to do that now (y/n)?"
  read reply
else
  if [ -f ${SUPPORTDIR}/hostconfig -a ! -f ${FINGERDIR}/hostconfig ]; then
    cp ${SUPPORTDIR}/hostconfig ${FINGERDIR}/hostconfig
  fi
  reply=y
fi
if [ "$reply" = "y" -o "$reply" = "yes" ]; then
  kill -HUP "$INETDPID"
fi

echo "Done with this much of the install.  You need to create the ttylocs"
echo "file for ${FINGERDIR}/ttylocs, a file of terminal to location"
echo "mappings.  An example of such a file is in the file \"`pwd`/ttylocs\"."

if [ "$is_finger_server" ]; then 
  echo
  echo "Since this host is the Finger server, you will need to tell it"
  echo "which hosts are clients.  You do that by putting the names of"
  echo "the hosts in the file \"${FINGERDIR}/hostconfig\"."
  echo
fi
exit 0
