#!/bin/sh 
if [ ! -r vmlinuz -a ! -r zImage ]; 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.
RCM=etc/rc.d/rc.M                       # Where rc.M file is.
SMAIL=usr/lib/smail/config		# Smail configuration file
ELMRC=usr/lib/elm.rc			# ELM rc file
#
# defaults:
NETWORK=127.0.0.0
IPADDR=127.0.0.1
#
############################################################################
#			 Question and answer.
############################################################################
#
# 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;
}
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
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
echo $HOSTNAME >/etc/HOSTNAME

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 -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 -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 -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 -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 -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

 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

#
############################################################################
#			  The rc.inet1 file.
############################################################################
#
if [ -f $RC ]; then 
 mv $RC $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=$HOSTNAME

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

# 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}
/$ROUTE add default gw \${GATEWAY} metric 1

# End of rc.inet1
EOF
 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=$HOSTNAME

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

# 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}
#/$ROUTE add default gw \${GATEWAY} metric 1

# End of rc.inet1
EOF
 fi # write out the script
fi # only alter if it already exists
#
############################################################################
#			  The networks file.
############################################################################
#
if [ -f $ETCNETWORKS ]; then mv $ETCNETWORKS $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.
#
# Version:	@(#)/etc/networks	2.00	04/30/93
#
# Author:	Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org
#

loopback	127.0.0.0
localnet	$NETWORK

# End of networks.
EOF
#
############################################################################
#			  The rc.M file.
############################################################################
#
echo "Creating /$RCM..."
if [ -f $RCM ]; then mv $RCM $RCM.OLD;fi
/bin/cat <<EOF >$RCM
#! /bin/sh
#
# rc.M		This file is executed by init(8) when the system is being
#		initialized for one of the "multi user" run levels (i.e.
#		levels 1 through 6).  It usually does mounting of file
#		systems et al.
#
# Version:	@(#)/etc/rc.d/rc.M	2.02	02/26/93
#
# Author:	Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
#

  # Tell the viewers what's going to happen...
  echo "Going multiuser..."

  # Start update.
  /sbin/update &

  # Screen blanks after 15 minutes idle time.
  /bin/setterm -blank 15

  # Initialize the NET subsystem.
  if [ -x /etc/rc.d/rc.inet1 ];
  then
        /bin/hostname $HOSTNAME
        /bin/domainname $DOMAIN
        /bin/sh /etc/rc.d/rc.inet1
        /bin/sh /etc/rc.d/rc.inet2
  else
        /sbin/hostname_notcp $HOSTNAME
	/bin/domainname $DOMAIN
        echo
        echo "Since you don't have TCP/IP installed, syslogd will complain when it first"
        echo "starts. The warning can be ignored."
        echo
	/usr/sbin/syslogd
	/usr/sbin/klogd
	/usr/sbin/lpd
	/usr/sbin/crond 
  fi

  # This can be changed if your system keeps GMT.
  if [ -x /sbin/clock ]; then
        /sbin/clock -s
  fi

  # Remove stale locks (must be done after mount -a!)
  /bin/rm -f /usr/spool/locks/* /usr/spool/uucp/LCK..* /tmp/.X*lock 1> /dev/null 2> /dev/null

  # Remove stale hunt sockets so the game can start.
  if [ -r /tmp/hunt -o -r /tmp/hunt.stats ]; then
    echo "Removing your stale hunt sockets from /tmp..."
    /bin/rm -f /tmp/hunt*
  fi

  # Start the local setup procedure.
  /etc/rc.d/rc.local

  # All done.
EOF
#
############################################################################
#			   The hosts file.
############################################################################
#
echo "Creating /$HOSTS..."
if [ -f $HOSTS ];then mv $HOSTS $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...
#
# Version:	@(#)/etc/hosts		2.00	04/30/93
#
# Author:	Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
#

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

# End of hosts.
EOF
#
##########################################################################
# The Smail 3.1.28 configuration file 
##########################################################################
#
if [ -f $SMAIL ];then 
 mv $SMAIL $SMAIL.OLD
fi
echo "Creating /$SMAIL..."
/bin/cat <<EOF >$SMAIL
#
#	smail configuration for $HOSTNAME
# (see smail(5) man page for details and other options)
#
hostname=$HOSTNAME
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
#
############################################################################
# The ELM rc file
############################################################################
#
if [ -f $ELMRC ];then
 mv $ELMRC $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
#
############################################################################
#			The resolv.conf file.
############################################################################
#
if [ "$LOOPBACK" = "n" ]; 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 mv $RESOLV $RESOLV.OLD;fi
 echo "domain $DOMAIN" >$RESOLV
 echo "nameserver $NAMESERVER" >>$RESOLV
else
 echo "domain $DOMAIN" >$RESOLV
fi
#
############################################################################
#		     Change permissions and exit.
############################################################################
#
chown root.root $HOSTS $RESOLV $RC
chmod 644 $HOSTS $RESOLV
chmod 754 $RC
chmod 754 $RCM

cat << EOF

Your networking software has now been configured. 

EOF
