#! /bin/sh

# /etc/rcS.d/10network
#
# Bring up the network and interfaces.

[ -f /etc/config/network ] && . /etc/config/network

# Setting the hostname
echo "Setting hostname...."
hostname $HOSTNAME

# Setting forwarding and defrag
if [ -d /proc/sys/net/ipv4 ]; then
    if [ "$FORWARD_IPV4" = "no" ]; then
        value=0
        message="disabled"
    else
        value=1
        message="enabled"
    fi
    echo "$value" > /proc/sys/net/ipv4/ip_forward
    echo "IPv4 packet forwarding is ${message}...."
    if [ "$DEFRAG_IPV4" = "yes" ]; then
        value=1
        message="enabled"
    else
        value=0
        message="disabled"
    fi
#    echo "$value" > /proc/sys/net/ipv4/ip_always_defrag
    echo "IPv4 automatic defragmentation is ${message}...."
fi

# Define the ifup function
ifup ()
{
    if [ "$BOOTPROTO" = "dhclient" ]; then
        if [ -f /sbin/dhclient ]; then
            # If we have pump we prefer it to get an ip number
            echo -n "Determining IP information for $DEVICE...."
    		if [ "$DEVICE" = "lo" ]; then
        		ifconfig ${DEVICE} ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST}
		        route add -host ${IPADDR} ${DEVICE}
    		else
        		ifconfig ${DEVICE} ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST}
            	if /sbin/${BOOTPROTO} ${DEVICE} ; then
               	 echo " done...."
           		else
           	     echo " failed...."
            	fi
			fi
		fi
    else
        if [ -z "$NETMASK" ]; then
            eval `/bin/ipcalc --netmask ${IPADDR}`
        fi
        if [ -z "$BROADCAST" ]; then
            eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`
        fi
        if [ -z "$NETWORK" ]; then
            eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}`
        fi
        ifconfig ${DEVICE} ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST}
        route add -host ${IPADDR} ${DEVICE}
        if [ "${GATEWAY}" != "" ]; then
            route add default gw ${GATEWAY} ${DEVICE}
        fi
    fi
}

# Bringing up the local network
if [ -f /etc/config/ifcfg-lo ]; then
    . /etc/config/ifcfg-lo
    echo "Bringing up interface ${DEVICE}...."
    ifup
fi

# Bring up ethernet interfaces
for i in /etc/config/ifcfg-eth*
do
    . $i
    echo "Bringing up interface ${DEVICE}...."
    ifup
done

touch /var/lock/subsys/network

exit 0
