#!/bin/sh
# netset
# Set second ethernet card, and enable gateway service
#
# GNU GPL (c) Eko M. Budi, 2004
#         (c) Vector Linux. 2004
#
# 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

if [ ! $DISPLAY = "" ]; then
DCMD="Xdialog --wrap" 
CMD="rxvt -e "
else
DCMD="dialog"
CMD=""
fi

TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
 mkdir -p $TMP
 chmod 700 $TMP
fi
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/tempmsg

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
    $DCMD --title "WARNING" --yesno "`cat $TMP/tempmsg`" 9 72
    if [ $? = 0 ]; then
      RET_CODE = 0;
    fi
    rm -r $TMP/tempmsg
  else
    if [ "$3" = "ECHO" ]; then
      echo $RET_CODE;
    fi
  fi
  return $RET_CODE;
}

if [ ! -d proc -a ! -d bin -a ! -d tmp ]; 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.inet2			# 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.
DHCP=no                                 # Use DHCP, yes/no.
#
# defaults:
NETWORK=192.168.0.0
IPADDR=192.168.0.1
NETMASK=255.255.255.0
#
############################################################################
#			 Question and answer.
############################################################################
#
cat << EOF > $TMP/tempmsg

Now we will attempt to configure your internal network. This 
process is for setting up a gateway and a second ethernet card to an 
internal network.If you want to set up general networking options to
connect to the outside world such as the internet or this machine
is part of an internal network and not the gateway then use the 
NETCONFIG program. You will be able to reconfigure your gateway
system at any time by typing:
  
netset

EOF
$DCMD --title "INTERNAL NETWORK CONFIGURATION" --msgbox "`cat $TMP/tempmsg`" 14 72
while [ 0 ]; do
cat << EOF > $TMP/tempmsg


Enter your IP address for the local machine. Example: 
111.112.113.114
Enter the IP address for this machine: 
EOF
  $DCMD --title "ENTER IP ADDRESS FOR THIS MACHINE" --inputbox \
"`cat $TMP/tempmsg`" \
14 68 $IPADDR 2> $TMP/SeTlip
  if [ $? = 1 -o $? = 255 ]; then
   rm -f $TMP/SeTlip $TMP/tempmsg
   exit
  fi
  IPADDR="`cat $TMP/SeTlip`"
  rm -f $TMP/SeTlip $TMP/tempmsg
  if [ "$IPADDR" = "" ]; then
   continue;
  fi
  syntax_check_color $IPADDR 4 WARN
  if [ $? = 0 ]; then
   echo $IPADDR > $TMP/SeTIP
   break;
  fi
 done

 while [ 0 ]; do
  if [ -r $TMP/SeTnetmask ]; then
   NETMASK=`cat $TMP/SeTnetmask`
  fi
  cat << EOF > $TMP/tempmsg
Enter your netmask. This will generally look something
like this: 255.255.255.0
Enter netmask (aaa.bbb.ccc.ddd):
EOF
  $DCMD --title "ENTER NETMASK FOR LOCAL NETWORK" --inputbox \
"`cat $TMP/tempmsg`" \
10 65 $NETMASK 2> $TMP/SeTnmask
  if [ $? = 1 -o $? = 255 ]; then
   rm -f $TMP/SeTnmask $TMP/tempmsg
   exit
  fi
  NETMASK="`cat $TMP/SeTnmask`"
  rm -f $TMP/SeTnmask $TMP/tempmsg
  if [ "$NETMASK" = "" ]; then
   continue;
  fi
  syntax_check_color $NETMASK 4 WARN
  if [ $? = 0 ]; then
   echo $NETMASK > $TMP/SeTnetmask
   break;
  fi
 done
 
# Set broadcast/network addresses automatically:

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


#
############################################################################
#			  The rc.inet2 file.
############################################################################
#
# echo "Creating /$RC..."
/bin/cat << ENDFILE > $RC
#! /bin/sh
#
# rc.inet2	This shell script boots up the internal network.
#
# Version:	@(#)/etc/rc.d/rc.inet2	2.00	05/06/2001
#


# Use these lines below to configure the 
# eth1 interface.

# Edit these values to set up a static IP address:
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!


  # Set up the ethernet card:
  echo "Configuring eth1 as \${IPADDR}..."
  /$IFCONFIG eth1 \${IPADDR} broadcast \${BROADCAST} netmask \${NETMASK}

  # If that didn't succeed, give the system administrator some hints:
  if [ ! \$? = 0 ]; then
    cat << EOF
Your ethernet card was not initialized properly.  Here are some reasons why this
may have happened, and the solutions:
1. Your kernel does not contain support for your card.  Including all the 
   network drivers in a Linux kernel can make it too large to even boot, and
   sometimes including extra drivers can cause system hangs.  To support your
   ethernet, either edit /etc/rc.d/rc.modules to load the support at boottime,
   or compile and install a kernel that contains support.

EOF
fi

# End of rc.inet2
ENDFILE
chmod 755 $RC

## Silently Enable firewall service
if [ -f /etc/rc.d/init.d/fireall ]; then
   chmod a+x /etc/rc.d/init.d/firewall
fi

$DCMD --title " INTERNAL NETWORK SETUP COMPLETE" --msgbox \
"Your internal networking has now been configured.\n
The necessary firewall service has also been activated.\n
You may reboot the computer to start it as a gateway" 8 62
rm -f $TMP/tempmsg

