# ! /bin/sh
# write by w

TOPDIR=/ubi/local/apps/network
TMPDIR=/run/network


[ -f $TOPDIR/network_functions ] && . $TOPDIR/network_functions



if [ ! $# ]; then
	echo "Please specify a configuration file...\n"
    exit 1
fi

if [ -f $1 ]; then
    . $1 
else
	echo "$1 file not exists...\n"
    exit 1
fi


if [ -z $DEVICE ]; then
	echo "DEV not exists...\n"
	exit 1
fi


static_ip_addr()
{
	if ( [ -z $IPADDR ] || [ -z $NETMASK ] ); then
		echo "Parameter error...\n"
		exit 1
	fi
	ifconfig $DEVICE $IPADDR netmask $NETMASK 
 
	[ -f $TMPDIR/networks ] && . $TMPDIR/networks

	if [ "eth0:2" == $DEVICE ]; then
		return 0
	fi

	if [ ${charger_priority_first} == $DEVICE ] ; then
		route add default gw $GATEWAY dev $DEVICE metric 1
	elif [ ${charger_priority_second} == $DEVICE ] ; then
		route add default gw $GATEWAY dev $DEVICE metric 2
	elif [ ${charger_priority_third} == $DEVICE ] ; then
		route add default gw $GATEWAY dev $DEVICE metric 3
	else
		route add default gw $GATEWAY dev $DEVICE metric 4
	fi 

	return 0
}

dhcp_ip_client()
{
	ifconfig $DEVICE 0.0.0.0
	udhcpc -i $DEVICE -s $TOPDIR/udhcpc.script -b
	return 0
}



case $2 in  
start)
	kill_process "udhcpc -i $DEVICE"
	kill_process "udhcpd"

	if [ $ENABLE == "false" ]; then
		ifconfig $DEVICE 0.0.0.0
		exit 1
	fi
	if [ "${BOOTPROTO}" = "dhcp" ]; then
		dhcp_ip_client
	else
		static_ip_addr
	fi
;;  
stop)  
;;  
restart)  
;;
*) 
echo "Usage: /etc/init.d/if-up.sh {start|stop|restart}"  
exit 1 ;;
esac



