#!/bin/sh
source "$RUNES_INTERFACE_DIR/RUNES/functions.sh"
[ -f "$RUNES_CONFIG_PATH" ] && . $RUNES_CONFIG_PATH
# dists_routine="/path/to/distribution's/network/script"

interface=$RUNES_INTERFACE_DIR/$RUNES_RUNE_NAME

eval $(runes_cmdline param_ IPv4Network)
# gateway=
# dns=
# staticip=
# netmask_eth0=


# Resources
busybox=./bin/busybox


# Remove distribution's network wakeup scripts.
# RedHat system
rm -f /etc/rc.d/rc?.d/S[0-9][0-9]/network* 2>/dev/null
# Debian system
rm -f /etc/rc?.d/S[0-9][0-9]network* 2>/dev/null
# Specified by user
[ -n "$dists_routine" ] && rm -rf "$dists_routine"


# Wakeup loopback interface.
ifconfig lo 127.0.0.1 netmask 255.0.0.0 broadcast 127.255.255.255 up



# Default gateway
if [ -n "$param_gateway" ];then
	route add default gw "$param_gateway"
fi



# Name server
# inherit VIVER's resolv.conf
#echo -n "" > /etc/resolv.conf
if [ -n "$param_dns" ];then
	for dns in $(echo $param_dns | tr '.' ' ');do
		echo "nameserver $dns" >> /etc/resolv.conf
	done
fi


# Find network devices
netdev=$($busybox ifconfig -a | grep Ethernet | grep "HWaddr" | sed "s/[[:space:]].*//")
netdev="$(echo $netdev)"


# IP address
for link in $netdev; do
	if ifconfig $link | grep -q "inet addr";then
		gprintf "$link is already up"
		continue
	fi
	ip="$(eval echo \$param_staticip_$link)"
	if [ -n "$ip" ];then
		# Set static ip
		netmask="$(eval echo \$param_netmask_$link)"
		if [ -n "$netmask" ];then
			ifconfig $link $ip netmask $netmask up
		else
			ifconfig $link $ip up
		fi
	else
		# DHCP
		$busybox udhcpc -nfq -i $link -s ./dhcp-script
		dhcpcode=$?
		if [ $dhcpcode -ne 0 ];then
			# DHCP is failured
			# Zeroconf
			zcipcode=1
			$busybox zcip -f -q $link ./zcip-script
			# wait until zcip is finished
			for wait in $(seq 1 15) ;do
				if $busybox ifconfig $link | grep -q "inet addr";then
					zcipcode=0
					break
				else
					$busybox usleep $(expr $wait \* 100000)
				fi
			done
			if [ "$zcipcode" -ne 0 ];then
				# Zeroconf is failured
				gprintf_failure "Can't set IPv4 address for $link"
				gprintf_failure "Setting 192.168.0.10"
				ifconfig $link 192.168.0.10 up
			fi
		fi
	fi
done



# Collect network datas
ipDNS=""
for dns in $(cat /etc/resolv.conf | grep nameserver | sed -e "s/[ \t]*nameserver[ \t]*//");do
	if [ -z "$ipDNS" ];then
		ipDNS="$dns"
	else
		ipDNS="$ipDNS $dns"
	fi
done

# Print and save network datas
rm -rf $interface
mkdir $interface

gprintf_success "interfaces: $netdev"
echo "Interfaces=\"$netdev\"" > $interface/info
first_addr=""
for link in $netdev;do
	ipline=$($busybox ifconfig $link | grep "inet addr")
	ipAddr=$(echo $ipline | sed -r "s/.*addr:([0-9\.]*).*/\\1/")
	ipMask=$(echo $ipline | sed -r "s/.*Mask:([0-9\.]*).*/\\1/")
	ipBcast=$(echo $ipline | sed -r "s/.*Bcast:([0-9\.]*).*/\\1/")
	ipNetaddr=$($busybox ipcalc -n $ipAddr $ipMask | sed "s/NETWORK=//")
	[ -z "$first_addr" ] && first_addr="$ipAddr"
	gprintf_info "$link:"
	gprintf_info "    IP Address:         $ipAddr"
	gprintf_info "    Subnet Mask:        $ipMask"
	gprintf_info "    Broadcast Address:  $ipBcast"
	gprintf_info "    Network Address:    $ipNetaddr"
	echo "
IPAddress_$link=\"$ipAddr\"
NetworkAddress_$link=\"$ipNetaddr\"
NetMask_$link=\"$ipMask\"
BcastAddress_$link=\"$ipBcast\"
" >> $interface/info
done


ipGW=$($busybox route -n | grep "^0\.0\.0\.0" | sed -r "s/^0\.0\.0\.0[ \t]*([0-9\.]*).*/\\1/" | head -n 1)
if [ -z "$ipGW" -a -n "$first_addr" ];then
	route add default gw "$first_addr"
	ipGW="$first_addr"
else
	ipGW="$(echo $ipGW)"
fi



gprintf_info "Default Gateway:  $ipGW"
[ -n "$ipDNS" ] && gprintf_info "Name Servers:     $ipDNS"
echo "DefaultGateway=\"$ipGW\"" >> $interface/info
echo "DNS=\"$ipDNS\"" >> $interface/info


echo "#!/bin/sh" | cat - $interface/info ./interface.in > $interface/interface
chmod 755 $interface/interface


