#!/bin/sh

mkdir -p /tmp/network

if [ $# != 1 ]; then
	echo "usage: ifup <interface>"
	exit 1
fi

interface="$1"

if [ -f /tmp/network/$interface ]; then
	exit 0
fi

cmd=/env/network/$interface

if [ ! -e $cmd ]; then
	echo "$f: no such file"
	exit 1
fi

ip=
ipaddr=
netmask=
gateway=
serverip=
ethaddr=

. $cmd

if [ $? != 0 ]; then
	echo "failed to bring up $interface"
	exit 1
fi

if [ -f /env/network/${interface}-discover ]; then
	/env/network/${interface}-discover
	if [ $? != 0 ]; then
		echo "failed to discover eth0"
		exit 1
	fi
fi

if [ -n "$ethaddr" ]; then
	${interface}.ethaddr=$ethaddr
fi

if [ "$ip" = static ]; then
	${interface}.ipaddr=$ipaddr
	${interface}.netmask=$netmask
	${interface}.serverip=$serverip
	${interface}.gateway=$gateway
	ret=0
elif [ "$ip" = dhcp ]; then
	dhcp
	ret=$?
	if [ $ret = 0 -a -n "$serverip" ]; then
		${interface}.serverip=$serverip
	fi
fi

if [ $ret = 0 ]; then
	echo -o /tmp/network/$interface up
fi

exit $ret
