#!/bin/sh

#
# This script will check the XENV keys to determine if there's any
# pre-defined ethernet configuration (e.g. l.autoeth0, l.autoeth1). 
# If the configuration exists, the script will setup the ethernet
# accordingly. 
# 
# The valid form of the key is (X=0 or 1)
#	l.autoethX = dhcp
# for DHCP configuration, and
#	l.autoethX = ip_addr:netmask:gateway:dns1:dns2
# for static IP configuration.
#
# On DHCP, the script will also monitor the link status and starting/
# stopping dhcp client if needed.
#

PATH=/bin:/sbin:/usr/bin:/usr/sbin

ZXENV_DEV=/dev/zxenv
ZXENV_TMP=/tmp/zxenv
AUTOETH0_KEY=l.autoeth0
AUTOETH1_KEY=l.autoeth1

# We need zxenv device to expose in-memory zxenv 
ZXENV_MAJ=`cat /proc/devices | grep zxenv | head -c 4`
if [ "$ZXENV_MAJ" = "" ]; then
	exit 1
fi

# Check the existence of some other required utilities
SETXENV2=`which setxenv2_mipsel`
AWK=`which awk`
if [ "$SETXENV2" = "" -o "$AWK" = "" ]; then
	exit 1
fi

# Create device node if needed
if [ ! -c $ZXENV_DEV ]; then
	rm -f $ZXENV_DEV
	mknod -m 444 $ZXENV_DEV c $ZXENV_MAJ 0
fi

# Dump the zxenv content
ERR=
dd if=$ZXENV_DEV of=$ZXENV_TMP bs=16k count=1 > /dev/null 2>&1 || ERR="y"
if [ ! "$ERR" = "" ]; then
	rm -f $ZXENV_TMP
	exit 1
fi

# Get and parse the ethernet configuration
ETH0_CFGSTR=`setxenv2_mipsel -f $ZXENV_TMP -k $AUTOETH0_KEY` || ETH0_ERR="y"  
if [ "$ETH0_ERR" = "" ]; then                                                 
	ETH0_CFG=`echo $ETH0_CFGSTR | awk '{ print $4 }'`                     
	ETH0_IP=`echo $ETH0_CFG | awk -F \: '{ print $1 }'`                   
	ETH0_NM=`echo $ETH0_CFG | awk -F \: '{ print $2 }'`                   
	ETH0_GW=`echo $ETH0_CFG | awk -F \: '{ print $3 }'`                   
	ETH0_DNS1=`echo $ETH0_CFG | awk -F \: '{ print $4 }'`                 
	ETH0_DNS2=`echo $ETH0_CFG | awk -F \: '{ print $5 }'`                 
fi                                                                            
ETH0_CFGSTR=`setxenv2_mipsel -f $ZXENV_TMP -k $AUTOETH1_KEY` || ETH1_ERR="y"  
if [ "$ETH1_ERR" = "" ]; then                                                 
	ETH1_CFG=`echo $ETH1_CFGSTR | awk '{ print $4 }'`                     
	ETH1_IP=`echo $ETH1_CFG | awk -F \: '{ print $1 }'`                   
	ETH1_NM=`echo $ETH1_CFG | awk -F \: '{ print $2 }'`                   
	ETH1_GW=`echo $ETH1_CFG | awk -F \: '{ print $3 }'`                   
	ETH1_DNS1=`echo $ETH1_CFG | awk -F \: '{ print $4 }'`                   
	ETH1_DNS2=`echo $ETH1_CFG | awk -F \: '{ print $5 }'`                 
fi                                                                          

RESOLV_CONF=/etc/resolv.conf

if [ ! "$ETH0_IP" = "" ]; then
	# Ethernet 0 configuration exists
	ifconfig eth0 up
	if [ "$ETH0_IP" = "dhcp" ]; then
		# Start in background to monitor the status
		( \
			ETH0_STAT= ; \
			while [ 1 ]; \
			do \
				ETH0_STAT_NEW=`cat /sys/class/net/eth0/carrier` ; \
				if [ ! "$ETH0_STAT" = "$ETH0_STAT_NEW" ]; then \
					if [ "$ETH0_STAT_NEW" = "1" ]; then \
						udhcpc -i eth0 > /dev/null 2>&1 ; \
					else \
						PID=`ps | grep "udhcpc -i eth0" | awk '{ print $1 }'` ; \
						if [ ! "$PID" = "" ]; then \
							kill -9 $PID > /dev/null 2>&1 ; \
						fi ; \
					fi ; \
					ETH0_STAT=$ETH0_STAT_NEW ; \
				fi ; \
				sleep 5 ; \
			done; \
		) &
	else
		# Try set up static configuration
		if [ ! "$ETH0_NM" = "" ]; then
			ifconfig eth0 $ETH0_IP netmask $ETH0_NM up
			if [ ! "$ETH0_GW" = "" ]; then
				route add default gw $ETH0_GW metric 1
			fi
			touch $RESOLV_CONF
			if [ ! "$ETH0_DNS1" = "" ]; then
				echo "nameserver " $ETH0_DNS1 >> $RESOLV_CONF
			fi
			if [ ! "$ETH0_DNS2" = "" ]; then
				echo "nameserver " $ETH0_DNS2 >> $RESOLV_CONF
			fi
		fi
	fi
fi

if [ ! "$ETH1_IP" = "" ]; then
	# Ethernet 1 configuration exists
	ifconfig eth1 up
	if [ "$ETH1_IP" = "dhcp" ]; then
		# Start in background to monitor the status
		( \
			ETH1_STAT= ; \
			while [ 1 ]; \
			do \
				ETH1_STAT_NEW=`cat /sys/class/net/eth1/carrier` ; \
				if [ ! "$ETH1_STAT" = "$ETH1_STAT_NEW" ]; then \
					if [ "$ETH1_STAT_NEW" = "1" ]; then \
						udhcpc -i eth1 > /dev/null 2>&1 ; \
					else \
						PID=`ps | grep "udhcpc -i eth1" | awk '{ print $1 }'` ; \
						if [ ! "$PID" = "" ]; then \
							echo Killing $PID ... ; \
							kill -9 $PID > /dev/null 2>&1 ; \
						fi ; \
					fi ; \
					ETH1_STAT=$ETH1_STAT_NEW ; \
				fi ; \
				sleep 5 ; \
			done; \
		) &
	else
		# Try set up static configuration
		if [ ! "$ETH1_NM" = "" ]; then
			ifconfig eth1 $ETH1_IP netmask $ETH1_NM up
			if [ ! "$ETH1_GW" = "" ]; then
				route add default gw $ETH1_GW metric 1
			fi
			touch $RESOLV_CONF
			if [ ! "$ETH1_DNS1" = "" ]; then
				echo "nameserver " $ETH1_DNS1 >> $RESOLV_CONF
			fi
			if [ ! "$ETH1_DNS2" = "" ]; then
				echo "nameserver " $ETH1_DNS2 >> $RESOLV_CONF
			fi
		fi
	fi
fi

rm -f $ZXENV_TMP

exit 0

