# BeOS Networking Environment startup script.
#
#	Don't edit this: edit /etc/network.conf instead.
#	(or /home/config/boot/UserBootScript, perhaps)
#

#
# Start BONE
#

# Prune files made while in the process of updaing the system config.
rm -f /etc/network.conf.new /etc/resolv.conf.new /etc/inetd.conf.new

# Read the configuration file.
if [ -r /etc/network.conf ]; then
	. /etc/network.conf
fi

# Make BONE initialize itself, and stay loaded
ifconfig loop0 >/dev/null 2>&1

#
# Interface section (IF_xxx)
#

# Disable old interfaces.
if [ x$1 = xrestart ]; then
	if [ -n "$IF_DISABLE" ]; then
		for i in $IF_DISABLE; do
			dhconfig $i off >/dev/null 2>&1
			ifconfig $i down >/dev/null 2>&1
		done
	fi
fi

# Set the dhcp hostname from /etc/hostname
read HOSTNAME < /etc/hostname
if [ -n "$HOSTNAME" ]; then
	dhconfig set hostname $HOSTNAME >/dev/null 2>&1
fi

# Set up interfaces.
if [ -n "$IF_LIST" ]; then
	for i in $IF_LIST; do
		unset type dev addr mask flags gw
		eval type=\$IF_${i}_TYPE
		eval dev=\$IF_${i}_DEV
		eval addr=\$IF_${i}_IP_ADDR
		eval mask=\$IF_${i}_IP_MASK
		eval flags=\$IF_${i}_FLAGS
		eval gw=\$IF_${i}_IP_GW

		# Skip interfaces without a listed device
		if [ -z "$dev" ]; then
			continue;
		fi
	
		# if the interface uses a driver and the driver's gone, bring the interface down.
		if [ $dev != ${dev#/dev/net} -a ! -e "$dev" ]; then
			dhconfig $dev off >/dev/null 2>&1
			ifconfig $dev down >/dev/null 2>&1
			continue;
		fi

		case x$type in
		xoff)		dhconfig $dev off >/dev/null 2>&1
					ifconfig $dev down >/dev/null 2>&1
					;;
		xdhcp)		ifconfig $dev up $flags >/dev/null 2>&1
					dhconfig $dev on >/dev/null 2>&1
					;;
		xstatic)	dhconfig $dev off >/dev/null 2>&1
					ifconfig $dev $addr $mask up $flags >/dev/null 2>&1
					if [ -n "$gw" ]; then
						route del default
						route add default gw $gw dev $dev >/dev/null 2>&1
					fi
					;;
		*)			if [ -n "$addr" ]; then
						dhconfig $dev off >/dev/null 2>&1
						ifconfig $dev $addr $mask up $flags >/dev/null 2>&1
						if [ -n "$gw" ]; then
							route del default
							route add default gw $gw dev $dev >/dev/null 2>&1
						fi
					else
						ifconfig $dev up $flags >/dev/null 2>&1
						dhconfig $dev on >/dev/null 2>&1
					fi
					;;
		esac
	done
fi

#
# Routing section (ROUTE_xxx)
#

# Set up routing.
if [ -n "$ROUTE_LIST" ]; then
	for i in $ROUTE_LIST; do
		eval args=\"\$ROUTE_$i\"
		route $args >/dev/null 2>&1
	done
fi


# 
# Network Daemon startup (/etc/net.d)
#

start() {
	if [ -x /etc/net.d/$1 ]; then
		echo exec $1
		/etc/net.d/$1 &
	else
		echo shell $1
		sh /etc/net.d/$1 &
	fi
} 

# start daemons.

if [ x$1 != xrestart ]; then
	for i in `cd /etc/net.d; echo *`; do
		start $i >/dev/null 2>&1
	done
fi

