#!/bin/sh
########################################################################
# Begin network
#
# chkconfig:	2345 07 90
# Description : Network Control Script
#
# Authors     : Gerard Beekmans - gerard@linuxfromscratch.org
#               Nathan Coulson - nathan@linuxfromscratch.org
#               Kevin P. Fleming - kpfleming@linuxfromscratch.org
#               DJ Lucas - dj@linuxfromscratch.org
# Update      : Bruce Dubbs - bdubbs@linuxfromscratch.org
# Update      : Jean-Marc pigeon
#
# Version     : LFS 7.0 / Osukiss 1.0
#
########################################################################

### BEGIN INIT INFO
# Provides:            $network
# Required-Start:      
# Default-Start:       2 3 4 5
# Default-Stop:        0 1 6
# Short-Description:   Starts and configures network interfaces.
# Description:         Starts and configures network interfaces.
# X-LFS-Provided-By:   LFS
### END INIT INFO
#===============================================================
. /lib/lsb/init-functions

#always proceed as called from init process
export IN_BOOT=1;

case "${1}" in
   start)
      log_info_msg "Starting network...."
      evaluate_retval
      for file in /etc/sysconfig/ifconfig.*
      do
        interface=${file##*/ifconfig.}

        # Skip if $file is * (because nothing was found)
        if [ "${interface}" = "*" ]
        then
          continue
          fi
	. ${file}
	if ! is_true "$ONBOOT"  ; then
          continue
          fi
        /sbin/ifup ${interface}
	if [ -z "${aliase}" ] ; then
	  /sbin/setroute ${1} ipv6 ${interface}
	  /sbin/setroute ${1} ipv4 ${interface}
	  fi
        done
      ;;

   stop)
      log_info_msg "Stopping network...."
      # Unmount any network mounted file systems
       umount --all --force --types nfs,cifs,nfs4 

      # Reverse list
      net_files=""
      for file in  /etc/sysconfig/ifconfig.*
        do
        net_files="${file} ${net_files}"
        done

      # Stop all network interfaces
      for file in ${net_files}
        do
        interface=${file##*/ifconfig.}

        # Skip if $file is * (because nothing was found)
        if [ "${interface}" = "*" ] ; then
          continue
          fi
	aliase=`echo ${interface}: | cut -d ':' -f2`
	if [ -z "${aliase}" ] ; then
	  /sbin/setroute ${1} ipv6 ${interface}
	  /sbin/setroute ${1} ipv4 ${interface}
	  fi
	if [ "$(ifconfig  ${1} 2>/dev/null | grep ' UP ' )" = "" ]; then
          /sbin/ifdown ${interface}
	  fi
        done
      ;;

   restart)
      ${0} stop
      sleep 1
      ${0} start
      ;;

   *)
      echo "Usage: ${0} {start|stop|restart}"
      exit 1
      ;;
esac
#===============================================================

exit 0

# End network
