#!/bin/sh

cli="/usr/bin/CLISettings"
operations="ip router mask mode vlan tftp"
ip="ENETCFG_IPADDR"
router="ENETCFG_ROUTER"
mask="ENETCFG_SUBNET"
mode="ENETCFG_DHCP_MODE"
vlan="ENETCFG_VLAN"
tftp0="ENETCFG_NOE_TFTP0_ADDR"
tftp1="ENETCFG_NOE_TFTP1_ADDR"
tftpport="ENETCFG_NOE_TFTP_PORT"
result=0

Usage()
{
	echo "IP configuration : Set and Flash IP parameters";
	echo "Usage:"
	echo " 		ipconfig [ip|router|mask|mode|vlan|tftp]"
	echo "		ipconfig [set ip|router|mask|mode|vlan <value>]"
	echo "		ipconfig [set tftp <tftp1> <tftp2> <tftpport>]"
	echo "Changes takes effect after reboot"
}

list()
{
        echo "ip `$cli get CURRENT $ip`"
        echo "mask `$cli get CURRENT $mask`"
        echo "router `$cli get CURRENT $router`"
	echo "mode `$cli get CURRENT $mode`"
	echo "vlan `$cli get CURRENT $vlan`"
	echo "tftp1 `$cli get CURRENT $tftp0`"
	echo "tftp2 `$cli get CURRENT $tftp1`"
	echo "tftpport `$cli get CURRENT $tftpport`"
}

exit_ko()
{
	echo "ipconfig KO"
	exit 1
}

exit_ok()
{
	echo "ipconfig OK"
	exit 0
}

[ -x $cli ] || { echo "Cannot run $cli"; exit_ko;}
[ $# -ne 0 ] || {  list ; exit_ok;}


if [ "$1" == "set" ]
then
	found=$operations found=${found#*${found%%$2*}} found=${found%${found##*$2}}
	[ "$found" == "$2" ] || { echo "Unsupported Operation"; Usage; exit_ko;}

	if [ "$2" != "tftp" ];then
		[ $# -eq 3 ] || { echo "No Input Value"; Usage; exit_ko;}
	fi
	
	if [ "$2" == "vlan" ]
	then
		if [ "$3" == "no" ]
		then
			 eval "$cli set LOCAL ENETCFG_VLAN_ENABLE false"
			 result=$?
		else
			vlan_num=`expr $3 + 0`
                        eval "$cli set LOCAL \$$2 $vlan_num"
			result=$?
			[ $result == 0 ] && eval "$cli set LOCAL ENETCFG_VLAN_ENABLE true"
			result=$?
		fi
	elif [ "$2" == "tftp" ];then
		case $# in
			3)
			    if [ a"$3" != a"0.0.0.0" ];then
			    	eval "$cli set LOCAL $tftp0 $3"
			    	result=$?
			    fi
			;;
			4)
			    if [ a"$3" != a"0.0.0.0" ];then
			    	eval "$cli set LOCAL $tftp0 $3"
			    	result=$?
			    fi
			    [ $result -ne 0 ] && exit_ko
			    if [ a"$4" != a"0.0.0.0" ];then
			    	eval "$cli set LOCAL $tftp1 $4"
			    	result=$?
			    fi
			;;
			5)
			    if [ a"$3" != a"0.0.0.0" ];then
			    	eval "$cli set LOCAL $tftp0 $3"
			    	result=$?
			    fi
			    [ $result -ne 0 ] && exit_ko
			    if [ a"$4" != a"0.0.0.0" ];then
			    	eval "$cli set LOCAL $tftp1 $4"
			    	result=$?
			    fi
			    [ $result -ne 0 ] && exit_ko
			    eval "$cli set LOCAL $tftpport $5"
			    result=$?
			;;
			*)
			    echo "Wrong Parameters $@"
			    Usage
			    exit_ko
			    ;;
		esac
				
	else
		eval "$cli set LOCAL \$$2 $3"
		result=$?
	fi

else
	found=$operations found=${found#*${found%%$1*}} found=${found%${found##*$1}}
	if [ "$found" == "$1" ] 
	then
		if [[ a"$1" == a"tftp" ]];then
		    eval "$cli get CURRENT $tftp0"
		    [ $? -ne 0 ] && exit_ko
		    eval "$cli get CURRENT $tftp1"
		    [ $? -ne 0 ] && exit_ko
		    eval "$cli get CURRENT $tftpport"
		    result=$?
		else
		    eval "$cli get CURRENT \$$1"
		    result=$?
		fi
	else
		echo "Unsupported Operation"
		Usage
		exit_ko
	fi	
fi

if [ $result -eq 0 ] 
then exit_ok
else exit_ko
fi

