#!/bin/sh
# Basic TCP/IP
# Here we configure static routes
# and start dhcp daemon in single-shot mode to configure ifaces
#
. /boot.conf
test "$LOG_START" && echo "<*> Executing '$0 $*'"

test "$LOG_STAGE" && echo "* Configuring loopback: 127.0.0.1"
ip a a 127.0.0.1/8 dev lo \
|| echo "! ip a a 127.0.0.1 dev lo: failure"
ip l set dev lo up \
|| echo "! ip l set dev lo up: failure"

i=0
while test "${NETDEV[$i]}"; do
    # Use this to tweak NIC (set needed duplex mode etc)
    if test "${IF_SETUP[$i]}"; then
	test "$LOG_STAGE" && echo "* Setting up ${NETDEV[$i]}: ${IF_SETUP[$i]}"
	${IF_SETUP[$i]} \
	|| echo "! ${IF_SETUP[$i]}: failure"
	#echo -n "DEBUG:"; read junk
    fi
    # DHCP?
    if test "${IPADDR[$i]}" = "DHCP"; then
	reply="R"
	while test "$reply" = "R" -o "$reply" = "r"; do
    	    test "$LOG_STAGE" && echo "* Attempting to configure ${NETDEV[$i]} by contacting a DHCP server..."
    	    # This runs DHCP client to renew IP
	    reply=""
	    udhcpc \
		--quit --now \
		--interface=${NETDEV[$i]} --hostname=null --foreground --pidfile=/dev/null \
		--script=/etc/rc.d/cfg_ip_dhcp_handler \
	    || { echo -n "*** DHCP failed (err:$?)! Press <Enter> to continue or <R> to retry..."; read reply; }
	done
    # Static addr?
    elif test "${IPADDR[$i]}" -a "${IPADDR[$i]}" != "127.0.0.1"; then
	test "$LOG_STAGE" && echo "* Configuring ${NETDEV[$i]}: ${IPADDR[$i]}"
	ifconfig ${NETDEV[$i]} ${IPADDR[$i]} ${IFCONFIG_COMMANDLINE[$i]} \
	|| echo "! ifconfig ${NETDEV[$i]} failure"
    fi
    # Next!
    let i+=1
done  

exit 0
