#! /bin/sh
# network  
# This shell script fires up multiple network devices,
# found at /etc/sysconfig/config/network/*
# Use this if you don't like the classic rc.M style
#
# GNU GPL  (c) Eko M. Budi, 2004
#          (c) Vector Linux, 2004

PATH="/sbin:/usr/sbin:/bin:/usr/bin"

# DON'T DO ANYTHING IF classic networking is being used
if [ -x /etc/rc.d/rc.inet1 ]; then
  exit 0
fi

. /etc/rc.d/functions

start()
{
# Setup local network
echo -n "Starting network localhost ..."
ifconfig lo 127.0.0.1 127.255.255.255 netmask 255.0.0.0 &> /dev/null
evaluate_retval

# Scan the settings
for FDEVICE in /etc/sysconfig/config/network/*; do
   # Only if the file is executable
   if [ ! -x $FDEVICE ]; then
      continue
   fi

   DEVICE=`basename $FDEVICE`
  
   # Reset variables
   IPADDR=""	
   NETMASK=""
   DHCP="no"
   BOOT="no"
   PROBE=""
   DHCP_HOSTNAME=""
   
   # Read the settings
   . $FDEVICE
   
   # Sanity checking, simple one. 
   # If the setting is wrong, root is on her own ;-)
   if [ "$DHCP" = "yes" ]; then
     echo "Starting network ${DEVICE} by contacting a DHCP server..."
     if [ ! "$DHCP_HOSTNAME" = "" ]; then
        DHCP_HOSTNAME="-h $DHCP_HOSTNAME"
     fi
     dhcpcd -t 10 ${DHCP_HOSTNAME} -d $DEVICE &> /dev/null
     # If success, just continue to the next devices
     if [ $? = 0 ]; then
        print_status success
	continue
     fi
     print_status failure  
   fi

   # If no IPADDR or NETMASK, skip
   if [ -z "$IPADDR" -o -z "$NETMASK" ]; then
     continue
   fi
   
   # Test IPADDR, if invalid, skip
   IPMASK=`ipmask $NETMASK $IPADDR`
   if [ ! $? = 0 ]; then
     continue
   fi     
   BROADCAST=`echo $IPMASK | cut -d " " -f 1`
   NETWORK=`echo $IPMASK | cut -d " " -f 2`
 
   # Going up ..
   echo -n "Starting network ${DEVICE} as ${IPADDR}/${NETMASK}..."
   ifconfig ${DEVICE} ${IPADDR} broadcast ${BROADCAST} netmask ${NETMASK} &> /dev/null
   if [ $? != 0 ]; then
      print_status failure
      continue
   fi
   print_status success
   
   # if PROBE is defined, try it
   if [ "$PROBE" ]; then
        echo -n "Testing $DEVICE to ping $PROBE "
        for ii in 1; do
    	    ping -c 1 -w $ii $PROBE > /dev/null
    	    if [ $? = 0 ]; then
	       PROBE=""
	       break;
	    fi
	    echo -n "."
	    sleep 1
	done
        if [ "$PROBE" ]; then
          print_status failure
          ifconfig $DEVICE down
	else
          print_status success
	fi
    fi
done

## Set the gateway
# Read the gateway setting
for FGW in /etc/sysconfig/config/route/*; do
   if [ ! -x $FGW ]; then
      continue   
   fi

   BNAME=`basename $FGW`
   TARGET=`echo $BNAME | cut -d : -f 1` 
   NETMASK=""
   GATEWAY=""
   DEVICE=""
   METRIC=""

   # Source the setting
   . $FGW

   [ "$GATEWAY" ] && GATEWAY="gw $GATEWAY"
   [ "$METRIC" ] && METRIC="metric $METRIC"
   [ "$DEVICE" ] && DEVICE="dev $DEVICE"

   if [ "$TARGET" == "default" ]; then
      route | grep -e "^default" &> /dev/null
      if [ ! $? = 0 ]; then
         echo -n "Setting route default via $GATEWAY ..."
         route add default ${GATEWAY} netmask 0.0.0.0 $METRIC $DEVICE &> /dev/null
      fi
   elif [ "$TARGET" == "$BNAME" ]; then
      echo -n "Setting route to host $TARGET via $GATEWAY ..."
      route add -host $TARGET $GATEWAY $METRIC $DEVICE
   else
      NETMASK=`echo $BNAME | cut -d : -f 2`
      echo -n "Setting route to $TARGET/$NETMASK via $GATEWAY ..."
      route add -net $TARGET netmask $NETMASK $GATEWAY $METRIC $DEVICE
   fi
   evaluate_retval 
done
}

stop() {
# Stopping route
for FGW in /etc/sysconfig/config/route/*; do
   if [ -x $FGW ]; then
      BNAME=`basename $FGW`
      TARGET=`echo $BNAME | cut -d : -f 1`
      if [ "$TARGET" = "default" ]; then
         echo -n "Deleting route default ..."
         route del default &> /dev/null
      elif [ "$TARGET" = "$BNAME" ]; then
         echo -n "Deleting route to $TARGET ..."
         route del -host $TARGET &> /dev/null
      else
         NETMASK=`echo $BNAME | cut -d : -f 2`
         echo -n "Deleting route to $TARGET/$NETMASK ..."
         route del -net $TARGET netmask $NETMASK &> /dev/null
      fi
      evaluate_retval
   fi
done

# Stopping network
for FDEVICE in /etc/sysconfig/config/network/*; do
   if [ -x $FDEVICE ]; then
     DEVICE=`basename $FDEVICE`
     echo -n "Stopping network $DEVICE ..."
     ifconfig $DEVICE down &> /dev/null
     evaluate_retval
   fi
done

# Stopping local network
echo -n "Stopping localhost ..."
ifconfig lo down &> /dev/null
evaluate_retval
}

status() {
   ifconfig
}

case $1 in
start)
   start
   ;;
stop)
   stop
   ;;
restart)
   $0 stop
   sleep 3
   $0 start
   ;;
reload)
   echo -n "Reloading network ...."
   print_status success
   ;;
status)
   status 
   ;;
*)
   echo "Usage: $0 {start|stop|restart|reload|status}"
esac

