#!/bin/sh

###############################################################################
#
# /etc/init.d/network - System control script for the network.
# Also accessed via a symbolic link named /usr/local/sbin/rcnetwork.
#
# Copyright (c) 2006, all rights reserved
# License- GPL
# Author-  Andre G Ancelin
# Company- Adtec Digital, Inc.
# Date-    2006-01-20
# Modified 2006-05-11 MPG, added route handling
#
###############################################################################

readonly HOSTNAME_FILE=/etc/HOSTNAME

readonly IFCONFIG="/sbin/ifconfig"
readonly ROUTE="/sbin/route"
readonly ROUTE_SET="/usr/local/sbin/router"
readonly ROUTES="/etc/sysconfig/network/routes"

readonly LEASES="/var/local/lib/dhcp/dhcp.leases"
readonly DHCLIENT="/usr/local/sbin/dhclient"
readonly DHSCRIPT="/usr/local/sbin/dhclient-script"
readonly DHCONF="/etc/dhclient.conf"

declare -i readonly NUM_ETHER_IFS=2

# Include the common script for all service scripts.
source /etc/init.d/rcservices

# Bring up a network interface.
interface_up ()
{
  declare readonly FILE=/etc/sysconfig/network/ifcfg-$1
  if [ -e $FILE ]; then
    unset IPADDR NETMASK
    source $FILE
    if [ -n "$IPADDR" -a -n "$NETMASK" ]; then
      # if IPADDR is zero then networking should not be turned on for that port.
      if [ "$IPADDR" != "0.0.0.0" ]; then
	      # test out the existence of BOOTPROTO and set to static as default if not set.
        [ -z "${BOOTPROTO:-}" ] && BOOTPROTO=static 
        if [ $BOOTPROTO == "dhcp" ]; then
           # update the dhcp configuration file with the hostname.
	       sed -i "s/\(send host-name \).*/\1\"${HOSTNAME}\";/" $DHCONF
           $DHCLIENT -q -nw -cf $DHCONF -lf $LEASES $1
        else
          echo "Configuring ${1}: IPADDR=$IPADDR NETMASK=$NETMASK"
          $IFCONFIG $1 $IPADDR netmask $NETMASK
        fi
      else
        echo "Network turned off, skipping ${1} configuration"        
      fi	
    else
      echo "No definitions for \"IPADDR\" and/or \"NETMASK\", skipping ${1} configuration"
    fi
  fi
}

# Bring down a network interface.
interface_down ()
{
  declare readonly FILE=/etc/sysconfig/network/ifcfg-$1
  if [ -e $FILE ]; then
    echo "$FILE exists:"
    echo "Shutting down $1 network interface"
    pkill $(basename ${DHCLIENT})
    $IFCONFIG $1 down
  fi
}

setup_route ()
{
  # Setup the routes, if not dhcp
  if [ "$BOOTPROTO" == "static" ]; then
    if [ -e $ROUTE_SET -a -e $ROUTES ] ; then
       # each eth interface could have its own gateway
       declare readonly FILE=/etc/sysconfig/network/ifcfg-$1
       if [ -e $FILE ]; then
          source $FILE
          if [ -n "$GATEWAY" ] ; then
             if [ "$1" == "eth0" ] ; then          
               $ROUTE_SET $GATEWAY
             fi
             # remove gateway from $FILE
             sed -r "s/^GATEWAY=\"([0-9]+\.){3}[0-9]+\"$//g" $FILE > ${FILE}.bak
             mv ${FILE}.bak $FILE
          else
             $ROUTE_SET "startup"
          fi          
       else
          $ROUTE_SET "startup"
       fi
    fi
  fi

}

#------------------------------------------------------------------------------
# Start the service.
do_start ()
{
  echo "Bringing up all network interfaces"
  
  # Set the hostname for this unit.
  hostname -F $HOSTNAME_FILE
  
  # Bring up the local loopback interface.  
  interface_up lo
  
  # Bring up the Ethernet interfaces.
  declare -i I
  for ((I=0; I<${NUM_ETHER_IFS}; I++)); do
    interface_up eth$I
    route add -net 169.254.0.0 netmask 255.255.0.0 dev eth$I metric 99
    route add default dev eth$I metric 99
    setup_route eth$I
  done;

  # Display the status of the network.
  do_status
}

#------------------------------------------------------------------------------
# Stop the service.
do_stop ()
{
  echo "Bringing down all network interfaces"
  
  # Bring down the local loopback interface.  
  interface_down lo
  
  # Bring down the Ethernet interfaces.
  declare -i I
  for ((I=0; I<${NUM_ETHER_IFS}; I++)); do
    interface_down eth$I
  done;  
}

#------------------------------------------------------------------------------
# Status of the service.
do_status ()
{
  # Dump the ifconfig status.
  $IFCONFIG
}

#------------------------------------------------------------------------------
# Usage for this service.
do_usage ()
{
  echo "Usage: $1 {start|stop|restart|tryrestart|status}"
  exit $FAILED
}

#------------------------------------------------------------------------------
# Unsupported functions.
unset -f do_reload do_forcereload

# Do the service.
dispatch_service $1
