#!/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_color() {
  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 > /tmp/tmpmsg

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
    dialog --title "WARNING" --yesno "`cat /tmp/tmpmsg`" 9 72
    if [ $? = 0 ]; then
      RET_CODE = 0;
    fi
    rm -r /tmp/tmpmsg
  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;

# 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 > /tmp/tmpmsg

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
dialog --title "NETWORK CONFIGURATION" --msgbox "`cat /tmp/tmpmsg`" 12 70
HOSTNAME=""
while [ "$HOSTNAME" = "" ]; do
cat << EOF > /tmp/tmpmsg
First, we'll need the name you'd like to give your host. Only
the base hostname is needed right now. (not the domain)

Enter hostname:
EOF
 dialog --title "ENTER HOSTNAME" --inputbox "`cat /tmp/tmpmsg`" 11 70 \
 2> /tmp/SeThost
 if [ $? = 1 -o $? = 255 ]; then
  rm -f /tmp/SeThost /tmp/tmpmsg
  exit
 fi
 HOSTNAME="`cat /tmp/SeThost`"
 rm -f /tmp/SeThost /tmp/tmpmsg
done

while [ "$DOMAIN" = "" ]; do
cat << EOF > /tmp/tmpmsg
Now, we need the domain name. Do not supply a leading '.'

Enter domain name for $HOSTNAME: 
EOF
 dialog --title "ENTER DOMAINNAME" --inputbox "`cat /tmp/tmpmsg`" \
10 70 2> /tmp/SeTdom
 if [ $? = 1 -o $? = 255 ]; then
  rm -f /tmp/SeTdom /tmp/tmpmsg
  exit
 fi
 DOMAIN="`cat /tmp/SeTdom`"
 rm -f /tmp/SeTdom /tmp/tmpmsg
done
echo $HOSTNAME.$DOMAIN > etc/HOSTNAME

cat << EOF > /tmp/tmpmsg
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.

Do you plan to ONLY use loopback? 
EOF
dialog --title "LOOPBACK ONLY?" --yesno "`cat /tmp/tmpmsg`" 9 70
if [ $? = 0 ]; then
 LOOPBACK="y"
else
 LOOPBACK="n"
fi

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

 while [ 0 ]; do
  cat << EOF > /tmp/tmpmsg
Enter your IP address for the local machine. Example: 
111.112.113.114
Enter IP address for $HOSTNAME (aaa.bbb.ccc.ddd): 
EOF
  dialog --title "ENTER LOCAL IP ADDRESS" --inputbox "`cat /tmp/tmpmsg`" \
10 68 2> /tmp/SeTlip
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/SeTlip /tmp/tmpmsg
   exit
  fi
  IPADDR="`cat /tmp/SeTlip`"
  rm -f /tmp/SeTlip /tmp/tmpmsg
  if [ "$IPADDR" = "" ]; then
    continue;
  fi
  syntax_check_color $IPADDR 4 WARN
  if [ $? = 0 ]; then
    break;
  fi
 done
 
# while [ 0 ]; do
#  cat << EOF > /tmp/tmpmsg
#Enter your network address. This will usually be your
#IP address with the last number replaced by 0, such as
#111.112.113.0
#Enter network address (aaa.bbb.ccc.ddd):
#EOF
#  dialog --title "ENTER NETWORK ADDRESS" --inputbox \
#"`cat /tmp/tmpmsg`" 11 65 2> /tmp/SeTneta
#  if [ $? = 1 -o $? = 255 ]; then
#   rm -f /tmp/SeTneta /tmp/tmpmsg
#   exit
#  fi
#  NETWORK="`cat /tmp/SeTneta`"
#  rm -f /tmp/SeTneta /tmp/tmpmsg
#  if [ "$NETWORK" = "" ]; then
#    continue;
#  fi
#  syntax_check_color $NETWORK 4 WARN
#  if [ $? = 0 ]; then
#    break;
#  fi
# done

 while [ 0 ]; do
  cat << EOF > /tmp/tmpmsg
Enter your gateway address, such as 111.112.113.1

If you don't have a gateway on your network, you can enter
your own IP address.

Enter gateway address (aaa.bbb.ccc.ddd):
EOF
  dialog --title "ENTER GATEWAY ADDRESS" --inputbox "`cat /tmp/tmpmsg`" \
13 65 2> /tmp/SeTgate
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/SeTgate /tmp/tmpmsg
   exit
  fi
  GATEWAY="`cat /tmp/SeTgate`"
  rm -f /tmp/SeTgate /tmp/tmpmsg
  if [ "$GATEWAY" = "" ]; then
    continue;
  fi
  syntax_check_color $GATEWAY 4 WARN
  if [ $? = 0 ]; then
    break;
  fi
 done

 while [ 0 ]; do
  cat << EOF > /tmp/tmpmsg
Enter your netmask. This will generally look something
like this: 255.255.255.0
Enter netmask (aaa.bbb.ccc.ddd):
EOF
  dialog --title "ENTER NETMASK" --inputbox "`cat /tmp/tmpmsg`" \
10 65 2> /tmp/SeTnmask
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/SeTnmask /tmp/tmpmsg
   exit
  fi
  NETMASK="`cat /tmp/SeTnmask`"
  rm -f /tmp/SeTnmask /tmp/tmpmsg
  if [ "$NETMASK" = "" ]; then
    continue;
  fi
  syntax_check_color $NETMASK 4 WARN
  if [ $? = 0 ]; then
    break;
  fi
 done

# while [ "$BROADCAST" = "" ]; do
#  cat << EOF > /tmp/tmpmsg
#Your broadcast address will usually be your IP address
#with 255 replacing the last value, such as: 111.112.113.255
#Enter broadcast address (aaa.bbb.ccc.ddd): 
#EOF
#  dialog --title "ENTER BROADCAST ADDRESS" --inputbox \
#"`cat /tmp/tmpmsg`" 10 70 2> /tmp/SeTbcast
#  if [ $? = 1 -o $? = 255 ]; then
#   rm -f /tmp/tmpmsg /tmp/SeTbcast
#  fi
#  BROADCAST="`cat /tmp/SeTbcast`"
#  rm -f /tmp/tmpmsg /tmp/SeTbcast
#  if [ "$BROADCAST" = "" ]; then
#    continue;
#  fi
#  syntax_check_color $BROADCAST 4 WARN
#  if [ $? = 0 ]; then
#    break;
#  fi
# done

# Set broadcast/network addresses automatially:

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

else
 if [ ! -r etc/rc.d/rc.inet1 ]; then
  cat << EOF > /tmp/tmpmsg

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
  dialog --title "SKIPPING MOST OF THE CONFIG PROCESS" \
--infobox "`cat /tmp/tmpmsg`" 10 70
 fi
fi

#
############################################################################
#			  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 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 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 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
 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
 dialog --title "USE A NAMESERVER?" --yesno "Will you be accessing a \
nameserver?" 5 50 
 if [ $? = 0 ]; then
  while [ "$NAMESERVER" = "" ]; do
   cat << EOF > /tmp/tmpmsg
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.

Name Server for domain $DOMAIN (aaa.bbb.ccc.ddd): 
EOF
   dialog --title "SELECT NAMESERVER" --inputbox \
"`cat /tmp/tmpmsg`" 14 72 2> /tmp/SeTns
   if [ $? = 1 -o $? = 255 ]; then
    rm -f /tmp/tmpmsg /tmp/SeTns 
   fi
   NAMESERVER="`cat /tmp/SeTns`"
   rm -f /tmp/tmpmsg /tmp/SeTns 
  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.
############################################################################
#

cat << EOF > /tmp/tmpmsg

Your networking software has now been configured. 

EOF
dialog --title "NETWORK SETUP COMPLETE" --msgbox \
"`cat /tmp/tmpmsg`" 7 65
rm -f /tmp/tmpmsg
