#!/bin/sh
########################################################################
# Begin $rc_base/init.d/network
#
# Description : Network Control Script
#
# Authors     : Gerard Beekmans - gerard@linuxfromscratch.org
#		Nathan Coulson - nathan@linuxfromscratch.org
#		Kevin P. Fleming - kpfleming@linuxfromscratch.org
#
# Version     : 00.00
#
# Notes       :
#
########################################################################

. /etc/sysconfig/rc
. ${rc_functions}
. /etc/sysconfig/network

case "${1}" in
	start)
		# Start all network interfaces
		for file in ${network_devices}/ifconfig.*
		do
			interface=${file##*/ifconfig.}

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

			IN_BOOT=1 ${network_devices}/ifup ${interface}
		done


		# inetd is a special case and is always started first if INetUtils is
		# installed.

		if [ -e /resources/indexes/libexec/inetd ]
		then
			/resources/indexes/libexec/inetd &
		fi

		# Packages that require initalisation can include init and early-init directories,
		# which should contain the init script(s). E.g. Apache would have init/apache which
		# would call apachectl, OpenSSH would have init/sshd which would start
		# sshd etc.
		# The package manager will collect all of these scripts together in
		# /resources/indexes/early-init/ and /resources/indexes/init/; all we need to do is
		# run each script in turn.

		# Run the late init scripts

		for pkg_init in `ls /system/indexes/init/`
		do
			source /system/indexes/init/$pkg_init
		done

		for pkg_init in `ls /resources/indexes/init/`
		do
			source /resources/indexes/init/$pkg_init
		done

		# Do user initialisation and exit
		source /system/user-init.sh

		;;

	stop)
		# Reverse list
		FILES=""
		for file in ${network_devices}/ifconfig.*
		do
			FILES="${file} ${FILES}"
		done

		# 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

			IN_BOOT=1 ${network_devices}/ifdown ${interface}
		done
		;;

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

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

# End /etc/rc.d/init.d/network
