#!/bin/bash

########################################################################
# Begin network
#
# 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
#
# Version     : LFS 7.0
#
# Adapted by  : CacheGuard Technologies for CacheGuard-OS
#
########################################################################

### BEGIN INIT INFO
# Provides:            $network
# Required-Start:      $local_fs localnet swap
# Should-Start:        $syslog firewalld iptables nftables
# Required-Stop:       $local_fs localnet swap
# Should-Stop:         $syslog firewalld iptables nftables
# Default-Start:       3 4 5
# Default-Stop:        0 1 2 6
# Short-Description:   Starts and configures network interfaces.
# Description:         Starts and configures network interfaces.
# X-LFS-Provided-By:   LFS
### END INIT INFO

get-network-interfaces()
{
    local reverse=${1}

    local solo_ethernet bond_ethernet static_routes

    if test -z "${reverse}" ; then

	solo_ethernet=$(ls -1 /etc/sysconfig/ifconfig.eth* 2> /dev/null | sort --version-sort)
	bond_ethernet=$(ls -1 /etc/sysconfig/ifconfig.bond* 2> /dev/null | sort --version-sort)
	static_routes=$(ls -1 /etc/sysconfig/ifconfig.route* 2> /dev/null | sort --version-sort)

	echo ${solo_ethernet} ${bond_ethernet} ${static_routes}
    else
	solo_ethernet=$(ls -1 /etc/sysconfig/ifconfig.eth* 2> /dev/null | sort --version-sort --reverse)
	bond_ethernet=$(ls -1 /etc/sysconfig/ifconfig.bond* 2> /dev/null | sort --version-sort --reverse)
	static_routes=$(ls -1 /etc/sysconfig/ifconfig.route* 2> /dev/null | sort --version-sort --reverse)

	echo ${static_routes} ${bond_ethernet} ${solo_ethernet}
    fi
}

case "${1}" in
   start)
      files=$(get-network-interfaces)
      # Start all network interfaces

      for file in ${files}
      do
         interface=${file##*/ifconfig.}

         # Skip if $file is * (because nothing was found)
         if [ "${interface}" = "*" ]; then continue; fi

         /sbin/ifup ${interface}
      done
      mount --all --types nfs,cifs,nfs4
      ;;

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

    files=$(get-network-interfaces reverse)

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

        # Skip if $file is * (because nothing was found)
        if [ "${interface}" = "*" ]; then continue; fi

	if [ ${interface:0:3} == eth ] ; then
            # See if interface exists
            if [ ! -e /sys/class/net/$interface ]; then continue; fi
	fi
        /sbin/ifdown ${interface}
    done
    ;;

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

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

exit 0

# End network
