#! /bin/sh
# /etc/rc.d/rc.inet1
# This script is used to bring up the various network interfaces.
#
# @(#)/etc/rc.d/rc.inet1 10.0  Sun Jun  6 23:42:32 PDT 2004  (pjv)

############################
# READ NETWORK CONFIG FILE #
############################

# Get the configuration information from /etc/rc.d/rc.inet1.conf:
. /etc/rc.d/rc.inet1.conf

###########
# LOGGING #
###########

# If possible, log events in /var/log/messages:
if [ /var/run/syslogd.pid -a -x /usr/bin/logger ]; then
  LOGGER=/usr/bin/logger
else # output to stdout/stderr:
  LOGGER=/bin/cat
fi

######################
# LOOPBACK FUNCTIONS #
######################

# Function to bring up the loopback interface.  If loopback is
# already up, do nothing.
lo_up() {
  if grep lo: /proc/net/dev 1> /dev/null ; then
    if ! /sbin/ifconfig | grep "^lo" 1> /dev/null ; then
      echo "/etc/rc.d/rc.inet1:  /sbin/ifconfig lo 127.0.0.1" | $LOGGER
      /sbin/ifconfig lo 127.0.0.1
      echo "/etc/rc.d/rc.inet1:  /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo" | $LOGGER
      /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo
    fi
  fi
}

# Function to take down the loopback interface:
lo_down() {
  if grep lo: /proc/net/dev 1> /dev/null ; then
    echo "/etc/rc.d/rc.inet1:  /sbin/ifconfig lo down" | $LOGGER
    /sbin/ifconfig lo down
  fi
}

######################
# ETHERNET FUNCTIONS #
######################

# Function to bring up an Ethernet interface.  If the interface is
# already up or does not yet exist (perhaps because the kernel driver
# is not loaded yet), do nothing.
eth_up() {
  # If the interface isn't in the kernel yet (but there's an alias for it in
  # modules.conf), then it should be loaded first:
  if ! grep eth${1}: /proc/net/dev 1> /dev/null ; then # no interface yet
    if /sbin/modprobe -c | grep -w "alias eth${1}" | grep -vw "alias eth${1} off" > /dev/null ; then
      echo "/etc/rc.d/rc.inet1:  /sbin/modprobe eth${1}" | $LOGGER
      /sbin/modprobe eth${1}
    fi
  fi
  if grep eth${1}: /proc/net/dev 1> /dev/null ; then # interface exists
    if ! /sbin/ifconfig | grep -w "eth${1}" 1>/dev/null || \
      ! /sbin/ifconfig eth${1} | grep "inet addr" 1> /dev/null ; then # interface not up or not configured
      if [ -x /etc/rc.d/rc.wireless ]; then
        . /etc/rc.d/rc.wireless eth${1} # Initialize any wireless parameters
      fi
      if [ "${USE_DHCP[$1]}" = "yes" ]; then # use DHCP to bring interface up
        if [ ! "${DHCP_HOSTNAME[$1]}" = "" ]; then
          echo "/etc/rc.d/rc.inet1:  /sbin/dhcpcd -d -t 10 -h ${DHCP_HOSTNAME[$1]} eth${1}" | $LOGGER
          /sbin/dhcpcd -d -t 10 -h ${DHCP_HOSTNAME[$1]} eth${1}
        else
          echo "/etc/rc.d/rc.inet1:  /sbin/dhcpcd -d -t 10 eth${1}" | $LOGGER
          /sbin/dhcpcd -d -t 10 eth${1}
        fi
      else # bring up interface using a static IP address
        if [ ! "${IPADDR[$1]}" = "" ]; then # skip unconfigured interfaces
          # Determine broadcast address from the IP address and netmask:
          BROADCAST=`/bin/ipmask ${NETMASK[$1]} ${IPADDR[$1]} | cut -f 1 -d ' '`
          # Set up the ethernet card:
          echo "/etc/rc.d/rc.inet1:  /sbin/ifconfig eth${1} ${IPADDR[$1]} broadcast ${BROADCAST} netmask ${NETMASK[$1]}" | $LOGGER
          /sbin/ifconfig eth${1} ${IPADDR[$1]} broadcast ${BROADCAST} netmask ${NETMASK[$1]}
        else
          if [ "$DEBUG_ETH_UP" = "yes" ]; then
            echo "/etc/rc.d/rc.inet1:  eth${1} interface is not configured in /etc/rc.d/rc.inet1.conf" | $LOGGER
          fi
        fi
      fi
    else
      if [ "$DEBUG_ETH_UP" = "yes" ]; then
        echo "/etc/rc.d/rc.inet1:  eth${1} is already up, skipping" | $LOGGER
      fi
    fi 
  else
    if [ "$DEBUG_ETH_UP" = "yes" ]; then
      echo "/etc/rc.d/rc.inet1:  eth${1} interface does not exist (yet)" | $LOGGER
    fi
  fi
}

# Function to take down an Ethernet interface:
eth_down() {
  if grep eth${1}: /proc/net/dev 1> /dev/null ; then
    if [ "${USE_DHCP[$1]}" = "yes" ]; then
      echo "/etc/rc.d/rc.inet1:  /sbin/dhcpcd -k -d eth${1}" | $LOGGER
      /sbin/dhcpcd -k -d eth${1} || /sbin/ifconfig eth${1} down
      sleep 1
    else
      echo "/etc/rc.d/rc.inet1:  /sbin/ifconfig eth${1} down" | $LOGGER
      /sbin/ifconfig eth${1} down
    fi
  fi
}

#####################
# GATEWAY FUNCTIONS #
#####################

# Function to bring up the gateway if there is not yet a default route:
gateway_up() {
  if ! /sbin/route -n | grep "^0.0.0.0" 1> /dev/null ; then
    if [ ! "$GATEWAY" = "" ]; then
      echo "/etc/rc.d/rc.inet1:  /sbin/route add default gw ${GATEWAY} metric 1" | $LOGGER
      /sbin/route add default gw ${GATEWAY} metric 1 2>&1 | $LOGGER
    fi
  fi
}

# Function to take down an existing default gateway:
gateway_down() {
  if /sbin/route -n | grep "^0.0.0.0" 1> /dev/null ; then
    echo "/etc/rc.d/rc.inet1:  /sbin/route del default" | $LOGGER
    /sbin/route del default
  fi
}

############
### MAIN ###
############

case "$1" in
'start') # "start" brings up all available interfaces:
  lo_up
  eth_up 0
  eth_up 1
  eth_up 2
  eth_up 3
  gateway_up
  ;;
'stop') # "stop" takes down all existing interfaces:
  gateway_down
  eth_down 3
  eth_down 2
  eth_down 1
  eth_down 0
  lo_down
  ;;
eth?_start) # "eth?_start" will start the specified interface
  INUM=`echo $1 | /bin/cut -c 4`
  eth_up $INUM
  ;;
eth?_stop) # "eth?_stop" will stop the specified interface
  INUM=`echo $1 | /bin/cut -c 4`
  eth_down $INUM
  ;;
eth?_restart) # "eth?_restart" will take the specified interface down and up again
  INUM=`echo $1 | /bin/cut -c 4`
  eth_down $INUM
  eth_up $INUM
  ;;
*) # The default is to bring up all interfaces:
  lo_up
  eth_up 0
  eth_up 1
  eth_up 2
  eth_up 3
  gateway_up
esac

# End of /etc/rc.d/rc.inet1
