#!/bin/sh
#
# Start the network....
#

dotconfig=/wr/etc/dot-config
int_file=/etc/network/interfaces

# no matter what we do keep lo up
ifup lo &> /dev/null

if grep -q '/ nfs' /proc/mounts; then
	echo "Running via NFS: leaving eth0 config alone"
	exit 0
fi

# read dot-config
if [ -f $dotconfig ]; then
      . $dotconfig
fi

# kill all previous instances of udhcpc
killall udhcpc &> /dev/null
# put eth0 down in case it was up before, but it is not so simple
# ifdown to change /var/run/ifstate
# flush to aviod ifup complains
# down to take link down (after reboot ifdown does not put link down)
ifdown eth0 &> /dev/null
ip addr flush dev eth0
ip link set eth0 down
# wait after down to make udhcpc to work properly
sleep 1

if [ "$CONFIG_ETH0_STATIC" ] || [ "$CONFIG_ETH0_DHCP_ONCE" ]; then
    echo "Write IP address"
    echo "# File generated from dot-config, do not edit!" > $int_file
    echo "# Configure Loopback" >> $int_file
    echo "auto lo" >> $int_file
    echo "iface lo inet loopback" >> $int_file
    echo "" >> $int_file

    echo "iface eth0 inet static" >> $int_file
    if [ "$CONFIG_ETH0_IP" ]; then
	echo "    address $CONFIG_ETH0_IP" >> $int_file
    fi
    if [ "$CONFIG_ETH0_MASK" ]; then
	echo "    netmask $CONFIG_ETH0_MASK" >> $int_file
    fi
    if [ "$CONFIG_ETH0_NETWORK" ]; then
	echo "    network $CONFIG_ETH0_NETWORK" >> $int_file
    fi
    if [ "$CONFIG_ETH0_BROADCAST" ]; then
	echo "    broadcast $CONFIG_ETH0_BROADCAST" >> $int_file
    fi
    if [ "$CONFIG_ETH0_GATEWAY" ]; then
	echo "    gateway $CONFIG_ETH0_GATEWAY" >> $int_file
    fi

fi

if [ "$CONFIG_ETH0_DHCP_ONCE" ]; then
    echo "Try DHCP to get IP"
    # try dhcp, if fail use static IP
    udhcpc -i eth0 -n
    if [ $? -ne 0 ]; then
	echo "Failed to obtain IP address via DHCP, set static IP"
	CONFIG_ETH0_STATIC="y"
    else
	exit
    fi
fi

if [ "$CONFIG_ETH0_STATIC" ]; then
    # ifup to use static parameters from /etc/netwrok/interfaces
    echo "Using static IP"
    ifup eth0
    exit
fi

# Try to get IP via dhcp if failed run dhcp client forever in background.
# If no information how to get IP address is available use this option.
echo "Using DHCP to get IP"
udhcpc -b -i eth0
