#!/bin/sh 
# This checks IP address syntax.
# usage: syntax_check ADDRESS #-OF-EXPECTED-SEGMENTS (up to 4)
# example: syntax_check 123.22.43.1 4
# returns: 0=found correct  1=too many fields  2=non numeric field found
syntax_check() {
  RET_CODE=0 
  SCRATCH=$1
  SCRATCH=`echo $SCRATCH | tr "." "/"`
  INDEX=$2
  while [ ! "$INDEX" = "0" ]; do
    # OK, so I'm a LISP-head :^)
    FIELD=`basename $SCRATCH`
    SCRATCH=`dirname $SCRATCH`
    if expr $FIELD + 1 1> /dev/null 2> /dev/null; then
      GOOD=y
    else
      RET_CODE=2; # non-numeric field
    fi
    INDEX=`expr $INDEX - 1`
  done
  if [ ! "$SCRATCH" = "." ]; then
    RET_CODE=1; # too many arguments
  fi
  if [ "$3" = "WARN" -a ! "$RET_CODE" = "0" ]; then
    cat << EOF

The address you have entered seems to be non-standard. We were expecting $2
groups of numbers seperated by dots, like: 127.0.0.1
Are you absolutely sure you want to use the address $1?

EOF
    echo -n "Use funny address $1 ([y]es, [n]o)? "
    read USE_FUNNY;
    echo
    if [ "$USE_FUNNY" = "y" ]; then
      RET_CODE = 0;
    fi
  else
    if [ "$3" = "ECHO" ]; then
      echo $RET_CODE;
    fi
  fi
  return $RET_CODE;
}

if [ ! -d lost+found -a ! -d vmlinuz -a ! -d proc ]; then # cheap, but it works :^)
 cd /
fi;

############################################################################
#
#  Disclaimer:
#
#  WARNING! USE THIS SCRIPT AT YOUR OWN RISK, I HAVE NO IDEA WHAT IT
#  WILL DO IF RUN ON YOUR COMPUTER! I ACCEPT NO RESPONSIBILITY!
#
#  A script to help get you on the net. Original idea from the MCC
#  install.net.  This script asks you for your hostname, domainname, IP
#  address, network address, gateway, netmask, broadcast address, and
#  your DNS server.  It then proceeds to set up the networking files
#  using the answers you gave it.  Make sure that the files and programs 
#  point to the right directory.  This is set up as documented in the 
#  NET-2-HOWTO. Please email me about any problems with, or errors in, 
#  the script.
#
#							Jim Robinson
#							jimr@simons-rock.edu
############################################################################
#
# IMPORTANT!!! NO LEADING '/' in the paths below, or this script will not
# function from the bootdisk.
IFCONFIG=sbin/ifconfig			# Where ifconfig program is.
ROUTE=sbin/route			# Where route program is.
RC=etc/rc.d/rc.inet1			# Where rc.inet1 file is.
RESOLV=etc/resolv.conf			# Where resolv.conf file is.
HOSTS=etc/hosts			 	# Where hosts file is.
ETCNETWORKS=etc/networks		# Where networks file is.
SMAIL=var/lib/smail/config		# Smail configuration file
ELMRC=var/lib/elm/elm.rc		# ELM rc file
#
# defaults:
NETWORK=127.0.0.0
IPADDR=127.0.0.1
#
############################################################################
#			 Question and answer.
############################################################################
#
cat << EOF

NETWORK CONFIGURATION

Now we will attempt to configure your mail and TCP/IP. This process probably
won't work on all possible network configurations, but should give you a good
start. You will be able to reconfigure your system at any time by typing:
  
  netconfig

EOF
HOSTNAME=""
while [ "$HOSTNAME" = "" ]; do
cat << EOF
First, we'll need the name you'd like to give your host. Only the base
hostname is needed right now. (not the domain)

EOF
 echo -n "Enter hostname: "
 read HOSTNAME
done

while [ "$DOMAIN" = "" ]; do
cat << EOF

Now, we need the domain name. Do not supply a leading '.'

EOF
 echo -n "Enter domain name for $HOSTNAME: "
 read DOMAIN
done
echo

cat << EOF
If you only plan to use TCP/IP through loopback, then your IP address will
be 127.0.0.1 and we can skip a lot of the following questions.

EOF

LOOPBACK=unknown
while [ ! "$LOOPBACK" = "y" -a ! "$LOOPBACK" = "n" ]; do
 echo -n "Do you plan to ONLY use loopback ([y]es, [n]o)? "
 read LOOPBACK;
 echo
done

if [ -r etc/rc.d/rc.inet1 -a "$LOOPBACK" = "n" ]; then

 while [ 0 ]; do
  echo "Enter your IP address for the local machine. Example: 111.112.113.114"
  echo -n "Enter IP address for $HOSTNAME (aaa.bbb.ccc.ddd): "
  read IPADDR
  if [ "$IPADDR" = "" ]; then
    continue;
  fi
  syntax_check $IPADDR 4 WARN
  if [ $? = 0 ]; then
    break;
  fi
 done
 echo
 
# while [ 0 ]; do
#  echo "Enter your network address. This will usually be your IP address"
#  echo "with the last number replaced by 0, such as 111.112.113.0"
#  echo -n "Enter network address (aaa.bbb.ccc.ddd): "
#  read NETWORK
#  if [ "$NETWORK" = "" ]; then
#    continue;
#  fi
#  syntax_check $NETWORK 4 WARN
#  if [ $? = 0 ]; then
#    break;
#  fi
# done
# echo

 while [ 0 ]; do
  echo "Enter your gateway address, such as 111.112.113.1"
  echo "If you don't have a gateway, you can edit /etc/rc.d/rc.inet1 later,"
  echo "or you can probably get away with entering your own IP address here."
  echo -n "Enter gateway address (aaa.bbb.ccc.ddd): "
  read GATEWAY
  if [ "$GATEWAY" = "" ]; then
    continue;
  fi
  syntax_check $GATEWAY 4 WARN
  if [ $? = 0 ]; then
    break;
  fi
 done
 echo

 while [ 0 ]; do
  echo "Enter your netmask. This will generally look something"
  echo "like this: 255.255.255.0"
  echo -n "Enter netmask (aaa.bbb.ccc.ddd): "
  read NETMASK
  if [ "$NETMASK" = "" ]; then
    continue;
  fi
  syntax_check $NETMASK 4 WARN
  if [ $? = 0 ]; then
    break;
  fi
 done
 echo

# while [ "$BROADCAST" = "" ]; do
#  echo "Your broadcast address will usually be your IP address"
#  echo "with 255 replacing the last value, such as: 111.112.113.255"
#  echo -n "Enter broadcast address (aaa.bbb.ccc.ddd): "
#  read BROADCAST
#  if [ "$BROADCAST" = "" ]; then
#    continue;
#  fi
#  syntax_check $BROADCAST 4 WARN
#  if [ $? = 0 ]; then
#    break;
#  fi
# done
# echo

BROADCAST=`ipmask $NETMASK $IPADDR | cut -f 1 -d ' '`
NETWORK=`ipmask $NETMASK $IPADDR | cut -f 2 -d ' '`

 echo "Setting up TCP/IP..."
else
 if [ ! -r etc/rc.d/rc.inet1 ]; then
 cat << EOF

You do not seem to have TCP/IP installed, so all I can really set up for
you is your hostname/domainname. This won't mean much since you're not on
the network, but it will let you have the hostname you prefer shown at the
login prompt.

EOF
 fi
fi

echo "Creating /etc/HOSTNAME..."
echo $HOSTNAME.$DOMAIN > etc/HOSTNAME

#
############################################################################
#			  The rc.inet1 file.
############################################################################
#
if [ -f $RC ]; then 
 cp $RC tmp/`basename $RC`.OLD
 echo "Creating /$RC..."
if [ "$LOOPBACK" = "n" ]; then # we are using an ethernet card
 /bin/cat <<EOF >$RC
#! /bin/sh
#
# rc.inet1	This shell script boots up the base INET system.
#
# Version:	@(#)/etc/rc.d/rc.inet1	1.01	05/27/93
#

HOSTNAME=\`cat /etc/HOSTNAME\`

# Attach the loopback device.
/$IFCONFIG lo 127.0.0.1
/$ROUTE add -net 127.0.0.0

# IF YOU HAVE AN ETHERNET CONNECTION, use these lines below to configure the 
# eth0 interface. If you're only using loopback or SLIP, don't include the
# rest of the lines in this file.

# Edit for your setup.
IPADDR="$IPADDR"	# REPLACE with YOUR IP address!
NETMASK="$NETMASK"	# REPLACE with YOUR netmask!
NETWORK="$NETWORK"	# REPLACE with YOUR network address!
BROADCAST="$BROADCAST"	# REPLACE with YOUR broadcast address, if you
			# have one. If not, leave blank and edit below.
GATEWAY="$GATEWAY"	# REPLACE with YOUR gateway address!

# Uncomment the line below to initialize the ethernet device.
/$IFCONFIG eth0 \${IPADDR} broadcast \${BROADCAST} netmask \${NETMASK}

# Uncomment these to set up your IP routing table.
/$ROUTE add -net \${NETWORK} netmask \${NETMASK}
/$ROUTE add default gw \${GATEWAY} metric 1

# End of rc.inet1
EOF
  chmod 755 $RC
 else # we are only using loopback
  /bin/cat <<EOF >$RC
#! /bin/sh
#
# rc.inet1	This shell script boots up the base INET system.
#
# Version:	@(#)/etc/rc.d/rc.inet1	1.01	05/27/93
#

HOSTNAME=\`cat /etc/HOSTNAME\`

# Attach the loopback device.
/$IFCONFIG lo 127.0.0.1
/$ROUTE add -net 127.0.0.0

# IF YOU HAVE AN ETHERNET CONNECTION, use these lines below to configure the 
# eth0 interface. If you're only using loopback or SLIP, don't include the
# rest of the lines in this file.

# Edit for your setup.
#IPADDR="$IPADDR"	# REPLACE with YOUR IP address!
#NETMASK="$NETMASK"	# REPLACE with YOUR netmask!
#NETWORK="$NETWORK"	# REPLACE with YOUR network address!
#BROADCAST="$BROADCAST"	# REPLACE with YOUR broadcast address, if you
			# have one. If not, leave blank and edit below.
#GATEWAY="$GATEWAY"	# REPLACE with YOUR gateway address!

# Uncomment ONLY ONE of the three lines below. If one doesn't work, try again.
# /$IFCONFIG eth0 \${IPADDR} netmask \${NETMASK} broadcast \${BROADCAST}
#/$IFCONFIG eth0 \${IPADDR} broadcast \${BROADCAST} netmask \${NETMASK}
# /$IFCONFIG  eth0 \${IPADDR} netmask \${NETMASK} 

# Uncomment these to set up your IP routing table.
#/$ROUTE -n add \${NETWORK} netmask \${NETMASK}
#/$ROUTE add default gw \${GATEWAY} metric 1

# End of rc.inet1
EOF
  chmod 755 $RC
 fi # write out the script
fi # only alter if it already exists
#
############################################################################
#			  The networks file.
############################################################################
#
if [ -f $ETCNETWORKS ]; then cp $ETCNETWORKS tmp/`basename $ETCNETWORKS`.OLD;fi
echo "Creating /$ETCNETWORKS..."
/bin/cat <<EOF >$ETCNETWORKS
#
# networks	This file describes a number of netname-to-address
#		mappings for the TCP/IP subsystem.  It is mostly
#		used at boot time, when no name servers are running.
#

loopback	127.0.0.0
localnet	$NETWORK

# End of networks.
EOF
chmod 644 $ETCNETWORKS
#
############################################################################
#			   The hosts file.
############################################################################
#
echo "Creating /$HOSTS..."
if [ -f $HOSTS ];then cp $HOSTS tmp/`basename $HOSTS`.OLD;fi
/bin/cat <<EOF >$HOSTS
#
# hosts		This file describes a number of hostname-to-address
#		mappings for the TCP/IP subsystem.  It is mostly
#		used at boot time, when no name servers are running.
#		On small systems, this file can be used instead of a
#		"named" name server.  Just add the names, addresses
#		and any aliases to this file...
#
# By the way, Arnt Gulbrandsen <agulbra@nvg.unit.no> says that 127.0.0.1
# should NEVER be named with the name of the machine.  It causes problems
# for some (stupid) programs, irc and reputedly talk. :^)
#

# For loopbacking.
127.0.0.1	localhost
$IPADDR 	$HOSTNAME.$DOMAIN $HOSTNAME

# End of hosts.
EOF
chmod 644 $HOSTS
#
##########################################################################
# The Smail 3.1.28 configuration file 
##########################################################################
#
#mkdir -p `dirname $SMAIL`
#if [ -f $SMAIL ];then 
# cp $SMAIL tmp/`basename $SMAIL`.OLD
#fi
#echo "Creating /$SMAIL..."
#/bin/cat <<EOF >$SMAIL
##
##	smail configuration for $HOSTNAME
## (see smail(5) man page for details and other options)
##
#-smtp_debug
#hostname=$HOSTNAME.$DOMAIN
#visible_domain=$DOMAIN
#more_hostnames=$HOSTNAME.$DOMAIN
#postmaster=postmaster
#smtp_accept_max=10
#EOF
#echo 'smtp_banner="$primary_name Linux Smail$version #$compile_num ready at $date"' >> $SMAIL
#echo 'received_field="Received: \' >> $SMAIL
#echo '	${if def:sender_host \' >> $SMAIL
#echo '		{from $sender_host by $primary_name \' >> $SMAIL
#echo '		${if def:sender_proto: with $sender_proto}\' >> $SMAIL
#echo '		\n\t(Linux Smail$version #$compile_num) }\' >> $SMAIL
#echo '	else{by $primary_name ${if def:sender_proto:with $sender_proto }\' >> $SMAIL
#echo '		(Linux Smail$version #$compile_num)\n\t}}\' >> $SMAIL
#echo '	id $message_id; $spool_date"' >> $SMAIL
#chmod 644 $SMAIL
##
############################################################################
# The ELM rc file
############################################################################
#
mkdir -p `dirname $ELMRC`
if [ -f $ELMRC ];then
 cp $ELMRC tmp/`basename $ELMRC`.OLD
fi
echo "Creating /$ELMRC..."
/bin/cat <<EOF >$ELMRC
#------------------------ global elm.rc file ------------------
#
# this expects any global aliases in /usr/lib/aliases.text
#
# you probably also want to set the visible_name parameter in 
# /usr/lib/smail/config if you use smail3.1.28
#
# this is the unqualified hostname
#
hostname = $HOSTNAME
#
# this is the local domain
#
hostdomain = .$DOMAIN
#
# this is the fully qualified hostname
#
hostfullname = $HOSTNAME.$DOMAIN
EOF
chmod 644 $ELMRC
#
############################################################################
#			The resolv.conf file.
############################################################################
#
if [ "$LOOPBACK" = "n" ]; then
 echo
 echo -n "Will you be accessing a nameserver ([y]es, [n]o)? "
 read NAMESV;
 if [ "$NAMESV" = "y" -o "$NAMESV" = "Y" ]; then
  cat << EOF
Here is your current IP address, full hostname, and base hostname:
$IPADDR       $HOSTNAME.$DOMAIN    $HOSTNAME

Please give the IP address of the name server to use. 
You can add more Domain Name Servers by editing /$RESOLV.

EOF
  while [ "$NAMESERVER" = "" ]; do
   echo -n "Name Server for domain $DOMAIN (aaa.bbb.ccc.ddd): "
   read NAMESERVER
  done

  if [ -f $RESOLV ]; then cp $RESOLV tmp/`basename $RESOLV`.OLD;fi
  echo "domain $DOMAIN" >$RESOLV
  echo "nameserver $NAMESERVER" >>$RESOLV
 else
  echo "domain $DOMAIN" >$RESOLV
 fi
fi
if [ "$LOOPBACK" = "n" ]; then chmod 644 $RESOLV ;fi
#
############################################################################
#		     Change permissions and exit.
############################################################################
#
chown root.root $HOSTS $RESOLV $RC
chmod 644 $HOSTS $RESOLV
chmod 754 $RC

cat << EOF

Your networking software has now been configured. 

EOF
