#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

# wireless settings
[ -f /etc/conf.d/wireless ] && . /etc/conf.d/wireless
# ethernet bonding settings
[ -f /etc/conf.d/bonding ] && . /etc/conf.d/bonding
# bridge settings
[ -f /etc/conf.d/bridges ] && . /etc/conf.d/bridges
# dhcpcd settings
[ -f /etc/conf.d/dhcpcd ] && . /etc/conf.d/dhcpcd

# Special wrapper for hotplug ifup calls
#  (make sure hotplug doesn't bring up disabled interfaces)
hotplug_ifup()
{
	[ "$1" != "" ] || return 1
	for ifline in ${INTERFACES[@]}; do
		# if the interface is disabled then this will be skipped
		if [ "$ifline" = "$1" ]; then
			ifup $ifline
		fi
	done
}

ifup()
{
	if [ "$1" = "" ]; then
		echo "usage: $0 ifup <interface_name>"
		return 1
	fi
	# don't bring up an interface that's already up
	[ "`/sbin/ifconfig ${1} 2>/dev/null | grep UP`" ] && return 0
	eval iwcfg="\$wlan_${1}"
	if [ "$iwcfg" != "" ]; then
		sh -c "/usr/sbin/iwconfig $iwcfg"
		/bin/sleep 2
	fi
	eval ifcfg="\$${1}"
	if [ "$ifcfg" = "dhcp" ]; then
		# remove the .pid file if it exists
		rm -f /etc/dhcpc/dhcpcd-${1}.{pid,cache} >/dev/null 2>&1
		/sbin/dhcpcd $DHCPCD_ARGS ${1}
	else
		/sbin/ifconfig $ifcfg
	fi
	return $?
}

ifdown()
{
	if [ "$1" = "" ]; then
		echo "usage: $0 ifdown <interface_name>"
		return 1
	fi
	eval ifcfg="\$${1}"
	if [ "$ifcfg" = "dhcp" ]; then
		if [ -f /etc/dhcpc/dhcpcd-${1}.pid ]; then
			kill `cat /etc/dhcpc/dhcpcd-${1}.pid`
		else
			# No .pid file, just bring the interface itself down
			/sbin/ifconfig ${1} down
		fi
	else
		/sbin/ifconfig $ifcfg down
	fi
	return $?
}

iflist()
{
	for ifline in ${INTERFACES[@]}; do
		if [ "$ifline" = "${ifline#!}" ]; then
			echo -en " $ifline:\t"
		else
			echo -en "$ifline:\t"
		fi
		eval real_ifline=\$${ifline#!}
		echo $real_ifline
	done
}

rtup()
{
	if [ "$1" = "" ]; then
		echo "usage: $0 rtup <route_name>"
		return 1
	fi
	eval routecfg="\$${1}"
	/sbin/route add $routecfg
	return $?
}

rtdown()
{
	if [ "$1" = "" ]; then
		echo "usage: $0 rtdown <route_name>"
		return 1
	fi
	eval routecfg="\$${1}"
	/sbin/route del $routecfg
	return $?
}

rtlist()
{
	for rtline in ${ROUTES[@]}; do
		if [ "$rtline" = "${rtline#!}" ]; then
			echo -en " $rtline:\t"
		else
			echo -en "$rtline:\t"
		fi
		eval real_rtline=\$${rtline#!}
		echo $real_rtline
	done
}

bond_up()
{
	for ifline in ${BOND_INTERFACES[@]}; do
		if [ "$ifline" = "${ifline#!}" ]; then
			eval bondcfg="\$bond_${ifline}"
			/sbin/ifenslave $ifline $bondcfg || error=1
		fi
	done
}

bridge_up()
{
	for br in ${BRIDGE_INTERFACES[@]}; do
		if [ "$br" = "${br#!}" ]; then
			# if the bridge already exists, remove it
			if [ "`/sbin/ifconfig $br 2>/dev/null`" ]; then
				/sbin/ifconfig $br down
				/usr/sbin/brctl delbr $br
			fi
			/usr/sbin/brctl addbr $br
			eval brifs="\$bridge_${br}"
			for brif in $brifs; do
				if [ "$brif" = "${brif#!}" ]; then
					/usr/sbin/brctl addif $br $brif || error=1
				fi
			done
		fi
	done
}

bridge_down()
{
	for br in ${BRIDGE_INTERFACES[@]}; do
		if [ "$br" = "${br#!}" ]; then
			/usr/sbin/brctl delbr $br
		fi
	done
}


case "$1" in
	start)
		if ! ck_daemon network; then
			echo "Network is already running.  Try 'network restart'"
			exit
		fi

		# See if we're using network profiles
		if [ "$NET" ]; then
			# This env var is passed from the kernel boot line
			if [ "$NET" = "menu" ]; then
				/usr/bin/netcfg --menu --timeout 5
			else
				/usr/bin/netcfg $NET
			fi
		elif [ "$NET_PROFILES" ]; then
			if [ "$NET_PROFILES" = "menu" ]; then
				/usr/bin/netcfg --menu --timeout 5
			else
				for prof in ${NET_PROFILES[@]}; do
					if [ "$prof" = "${prof#!}" ]; then
						/usr/bin/netcfg -c $prof
					fi
				done
			fi
		fi

		stat_busy "Starting Network"
		error=0
		# bring up bridge interfaces
		bridge_up
		# bring up ethernet interfaces
		for ifline in ${INTERFACES[@]}; do
			if [ "$ifline" = "${ifline#!}" ]; then
				ifup $ifline || error=1
			fi
		done
		# bring up bond interfaces
		bond_up
		# bring up routes
		for rtline in "${ROUTES[@]}"; do
			if [ "$rtline" = "${rtline#!}" ]; then
				rtup $rtline || error=1
			fi
		done
		if [ $error -eq 0 ]; then
			add_daemon network
			stat_done
		else
			stat_fail
		fi
		;;
	stop)
		#if ck_daemon network; then
		#	echo "Network is not running.  Try 'network start'"
		#	exit
		#fi

		# shutdown any profiles started by netcfg (or from NET_PROFILES in rc.conf)
		/usr/bin/netcfg --stopall

		stat_busy "Stopping Network"
		rm_daemon network
		error=0
		for rtline in "${ROUTES[@]}"; do
			if [ "$rtline" = "${rtline#!}" ]; then
				rtdown $rtline || error=1
			fi
		done
		for ifline in ${INTERFACES[@]}; do
			if [ "$ifline" = "${ifline#!}" ]; then
				ifdown $ifline || error=1
			fi
		done
		# bring down bridge interfaces
		bridge_down
		if [ $error -eq 0 ]; then
			stat_done
		else
			stat_fail
		fi
		;;
	restart)
		$0 stop
		/bin/sleep 2
		$0 start
		;;
	hotplug_ifup|ifup|ifdown|iflist|rtup|rtdown|rtlist)
		$1 $2
		;;
	*)
		echo "usage: $0 {start|stop|restart}"  
		echo "       $0 {ifup|ifdown|iflist|rtup|rtdown|rtlist}"
esac

# vim: set ts=2 noet:
