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

dotconfig=/wr/etc/dot-config
int_file=/etc/network/interfaces
log_output=/dev/kmsg
management_port=eth0
me=`basename "$0"`

#
# This function is looping forever until DHCP is reachable 
#
wait_dhcp_forever()
{
	while  : ; do
		udhcpc  -n -q  -i $management_port
		if [ "$?" -eq "0" ]; then
			break
		fi
		echo "$me: waiting DHCP service (press ENTER to abandon) ...."
		# wait for input for 5s, timeout if no input.
		read -t 5
		# any input will result in the returned value to be zero
		if [ "$?" -eq "0" ]; then
			# report to log that something went wrong
			echo "WARNING: Obtaining IP from DHCP service was" \
			     "abandoned by the user."\
			     | tee $log_output
			break
		fi
	done
}

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

if grep -q '/ nfs' /proc/mounts; then
	echo "Running via NFS: leaving $management_port config alone" | tee $log_output
	exit 0
fi

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

# kill all previous instances of udhcpc
killall udhcpc &> /dev/null
# put management port 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 $management_port &> /dev/null
ip addr flush dev $management_port
ip link set $management_port down
# wait after down to make udhcpc to work properly
sleep 1

if [ "$CONFIG_ETH0_STATIC" = "y" ] || [ "$CONFIG_ETH0_DHCP_ONCE" = "y" ]; 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 $management_port 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_HOSTNAME_STATIC" = "y" ]; then
    if [ -z "$CONFIG_HOSTNAME_STRING" ]; then
		echo "empty CONFIG_HOSTNAME_STRING! use wrs" | tee $log_output
		CONFIG_HOSTNAME_STRING="wrs"
    fi
    /bin/hostname "$CONFIG_HOSTNAME_STRING"
    echo "$CONFIG_HOSTNAME_STRING"  | tee $log_output > /etc/hostname
elif [ "$CONFIG_HOSTNAME_DHCP" = "y" ]; then
    DHCP_OPT_EXTRA="-O hostname -s /wr/bin/dhcp_extra_opt.sh"
fi

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

if [ "$CONFIG_ETH0_STATIC" = "y" ]; then
    # ifup to use static parameters from /etc/netwrok/interfaces
    echo "Using static IP" | tee $log_output
    ifup $management_port
    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" | tee $log_output
# redirect output from udhcpc into syslog and output about the first lease to
# the kernel log (syslog is not started at this point)
wait_dhcp_forever
udhcpc -S -b -i $management_port $DHCP_OPT_EXTRA | tee $log_output
