#!/bin/sh
### fixhost
### $Id: fixhost,v 1.2 1993/05/29 18:39:46 mdw Exp $
### Matt Welsh <mdw@tc.cornell.edu>
###
### Simply gets info from CONF_FILE and puts it into /etc/hosts in 
### the right format. 

# Some security
PATH=/bin:/usr/bin:/etc
IFS="	 "

# This is the netsetup directory
NET_DIR="/etc"
TMPFILE=/tmp/hosts.$$
# Fix this to /etc/hosts...
HOSTS=hosts
. $NET_DIR/netf.sh

if [ ! -f $CONF_FILE ]; then
  echo "fixhost: cannot read $CONF_FILE."
  exit 1
fi

HOSTNAME=`getnetconf hostname`
FULLHOST=`getnetconf fullhost`
IPADDR=`getnetconf ipaddr`
LOOPBACK=

if [ "$IPADDR" = "127.0.0.1" ]; then
  LOOPBACK="loopback"
fi

ORIG=`grep "^${IPADDR:?}\W" $HOSTS`

if [ "$ORIG" = "" ]; then
  echo "$IPADDR ${HOSTNAME:?} $FULLHOST" >> $HOSTS
else
  # It's in there[tm]
  cat $HOSTS | sed -e "s/$ORIG/$IPADDR ${HOSTNAME:?} $FULLHOST $LOOPBACK/g" > $TMPFILE
  mv $TMPFILE $HOSTS
fi
