#!/bin/sh
### netsetup
### $Id: netsetup,v 1.3 1993/05/29 18:56:37 mdw Exp mdw $
### Matt Welsh <mdw@tc.cornell.edu>
### 
### This script creates net.conf and fixes a few other files for
### TCP/IP configuration. 

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

# This is the netsetup directory
NET_DIR="/etc"
. $NET_DIR/netf.sh

SLIPONLY=
LOOPBACKONLY=
IPADDR=
NETMASK=
NETWORK=
GATEWAY=
DEVICE=

echo "netsetup $Id: netsetup,v 1.3 1993/05/29 18:56:37 mdw Exp mdw $"

ORIG_HOSTNAME=`hostname`
echo ""
echo "Please give me your hostname, without the domain name (for example,"
echo "if your hostname is loomer.vpizza.com, only give me 'loomer')."
HOSTNAME=`Prompt "Enter your hostname" "$ORIG_HOSTNAME"`

echo ""
echo "Please give me your domain name. For example, for loomer.vpizza.com,"
echo "this is \'vpizza.com\'. (Machines not actually on the network can"
echo "use anything for this, or leave it blank.)"
DOMAIN=`Prompt "Enter your domain name" ""`

if [ "$DOMAIN" != "" ]; then
  FULLHOST="$HOSTNAME.$DOMAIN"
  echo ""
  echo "In that case, your fully qualified hostname is $FULLHOST."
  echo "If this is not correct, please enter your full hostname below."
  FULLHOST=`Prompt "Enter your full hostname" "$FULLHOST"`
else
  FULLHOST=$HOSTNAME
fi

# Go ahead and get config info
echo ""
echo "Are you using a SLIP connection with a dynamic IP address (that is,"
ans=`Prompt "your IP address changes every time you dial in)? (y/n)" "n"`
case $ans in
  y*) SLIPONLY=yes
      ;;
  Y*) SLIPONLY=yes
      ;;
esac

if [ "$SLIPONLY" = "" ]; then
  echo ""
  ans=`Prompt "Are you using loopback only? (y/n)" "n"`
  case $ans in
    y*) LOOPBACKONLY=yes
        ;;
    Y*) LOOPBACKONLY=yes
        ;;
  esac
fi

if [ "$SLIPONLY" = "" -a "$LOOPBACKONLY" = "" ]; then
# For IP only
  echo ""
  IPADDR=`Prompt "Please enter your IP address" ""`

  echo ""
  echo "Please enter your ethernet device name (without the /dev)."
  echo "If you don't have an ETHERNET device (i.e., you're using SLIP),"
  echo "leave this blank."
  DEVICE=`Prompt "Enter your ethernet device name" "eth0"`

else
  IPADDR="127.0.0.1"
fi

if [ "$LOOPBACKONLY" = "" ]; then
  # May need this for SLIP
  echo ""
  echo "Please enter your subnet mask. This specifies how many bits are"
  echo "used to determine the subnetwork number in your address. For Class"
  echo "B networks, this is 255.255.0.0, and for Class C networks, it is"
  echo "255.255.255.0. Most networks are Class C. If you're not sure,"
  echo "leave it blank or try 255.255.255.0."
  NETMASK=`Prompt "Enter your subnet mask" ""`

  if [ "$NETMASK" != "" ]; then
    echo ""
    echo "Enter your network address: this is your IP address masked with the"
    echo "netmask, above (I'm too lazy to figure it out myself). For example,"
    echo "if your netmask is 255.255.255.0, and your IP address is"
    echo "128.250.153.21, your net address would be 128.250.153.0. You can"
    echo "leave this blank if you use SLIP or don't want to define it."
    NETWORK=`Prompt "Enter your network address" ""`
  fi

  echo ""
  echo "Please enter the numeric address of your gateway. This is the"
  echo "machine which routes packets for your subnet; your network admins"
  echo "should be able to provide this information. SLIP users may need"
  echo "to leave this blank (or set it to the address of your SLIP server)."
  GATEWAY=`Prompt "Enter your gateway's IP address" ""`

  echo ""
  echo "Enter the IP address of your nameserver (the machine which you"
  echo "lookup hostnames from). If you run named on your own machine,"
  echo "or don't have a nameserver, use your own IP address."
  NAMESERVER=`Prompt "Enter IP address of name server" "$IPADDR"`
fi

echo ""
echo -n "Creating $CONF_FILE..."
mv -f $CONF_FILE $CONF_FILE.orig

echo "hostname ${HOSTNAME:?}" > $CONF_FILE
if [ "$DOMAIN" != "" ]; then
  echo "domain ${DOMAIN:?}" >> $CONF_FILE
fi
echo "fullhost ${FULLHOST:?}" >> $CONF_FILE

if [ "$IPADDR" != "" ]; then
  echo "ipaddr ${IPADDR:?}" >> $CONF_FILE
fi
if [ "$DEVICE" != "" ]; then
  echo "device ${DEVICE:?}" >> $CONF_FILE
fi
if [ "$NETMASK" != "" ]; then
  echo "netmask ${NETMASK:?}" >> $CONF_FILE
fi
if [ "$NETWORK" != "" ]; then
  echo "network ${NETWORK:?}" >> $CONF_FILE
fi
if [ "$GATEWAY" != "" ]; then
  echo "gateway ${GATEWAY:?}" >> $CONF_FILE
fi
if [ "$NAMESERVER" != "" ]; then
  echo "nameserver ${NAMESERVER:?}" >> $CONF_FILE
fi
echo "done."

echo -n "Fixing $NET_DIR/hosts..."
fixhost
echo "done."

echo -n "Creating $NET_DIR/resolv.conf..."
rm -f $NET_DIR/resolv.conf
touch $NET_DIR/resolv.conf
if [ "$DOMAIN" != "" ]; then
  echo "domain $DOMAIN" > $NET_DIR/resolv.conf
fi
if [ "$NAMESERVER" != "" ]; then
  echo "nameserver $NAMESERVER" >> $NET_DIR/resolv.conf
else
  echo "nameserver 127.0.0.1" >> $NET_DIR/resolv.conf
fi
echo "done."

echo -n "Creating $NET_DIR/networks..."
rm -f $NET_DIR/networks
echo "loopback 127.0.0.0" > $NET_DIR/networks
if [ "$DOMAIN" != "" -a "$NETWORK" != "" ]; then
  echo "$DOMAIN $NETWORK" >> $NET_DIR/networks
fi
echo "done."

