#!/bin/sh

export PATH="$PATH:/usr/lib/klibc/bin"

. /usr/share/initramfs-tools/scripts/functions

IP=$DIRECT_IP

# rewrite the configure_networking function
# to support the configuration of multiple network cards
configure_networking()
{
	ALL_DEVICE="$(ls -ld /sys/class/net/* | awk -F '/' '{print $NF}' | tr '\n' ' ')"

	for ROUNDTTT in 4 6 9 16 25 36 64 100; do

		net="$(ls -l /run/net-*.conf 2>/dev/null | grep -v '/run/net-lo.conf')"
		[ -n "${net}" ] && break

		case ${IP} in
			none|off)
				# Do nothing
				;;
			""|on|any)
				# Bring up device
				ipconfig -t ${ROUNDTTT} ${ALL_DEVICE}
				;;
			dhcp|bootp|rarp|both)
				ipconfig -t ${ROUNDTTT} -c "${IP}" -d ${ALL_DEVICE}
				;;
			*)
				ipconfig -t ${ROUNDTTT} -d "$IP"
				;;
		esac
	done

	. /run/net-*.conf

}

read_ip()
{
	for x in $(cat /proc/cmdline)
	do
		case $x in
			ip=*)
			IP="${x#ip=}"
			;;
		esac
	done
}

[ -n "$IP" ] || read_ip
configure_networking
