#! /bin/sh

# This file started as /etc/rc.d/init.d/snmpserver - Start/Stop the snmpd
# need link file /usr/sbin/snmpd  /usr/local/sbin/snmpd
#

# Comment out the following exit line to enable this script.
# Before doing so, you need to edit the /etc/snmpd.conf or /usr/local/net-snmp/share/snmp
# /snmpd.conf file.
#exit 0

KILLWAIT=3
CGI_BIN_PATH="/var/www/html/web_pages/cgi-bin"
#setControl("iptype", iptypeV + "," + ip_addrV + "," + subnetmaskV + "," + gatewayV + "," + dnsipV);
#/var/www/html/web_pages/cgi-bin/setSNMPcfg.cgi iptype,1,192.168.107.235,255.255.255.0,192.168.107.254,192.168.100.237
NAMESERVERCONF="/etc/resolv.conf"
#get nameserver
NAMESERVER=$(cat $NAMESERVERCONF| awk '{print $2}')

case "$1" in
	
	dhcp)
	if [ "$#" -ne "1" ]; 
	then 
		echo $#
		echo "Usage: $0 dhcp. FAILED"
	else
		${CGI_BIN_PATH}/setSNMPcfg.cgi name="iptype"\&\?params="1,,,,"\&
		echo "need reboot system."
	fi
	;;

	static)
	#configip -static ipaddr netmask gateway
	if [ "$#" -ne "4" ]; 
	then 
		echo "Usage: $0 static ipaddr netmask gateway. FAILED"
	else
		${CGI_BIN_PATH}/setSNMPcfg.cgi name="iptype"\&\?params="0,$2,$3,$4,$NAMESERVER"\&
		#ifconfig eth0 $2 netmask $3 up
		#route add default gw $4
		#ifconfig lo up
		echo "Settings have been updated."
	fi
	;;	
	
	dns)
	if [ "$#" -ne "2" ]; 
	then
		echo "Usage: $0 dns nameserver. FAILED"
	else
		echo "nameserver $2" > $NAMESERVERCONF
		echo "Done"
	fi
		
	;;	
	
	mac)                                                      
	if [ "$#" -ne "2" ];                                      
	then                                                                             
			 echo "Usage: $0 mac 00:00:00:00:00:00. FAILED"   
	else                                                               
			echo -n "Are you sure to change the MAC address?[yes|no]: "
			read -r confirm                                            
			echo "$confirm" | grep -i "yes"                            
			#if [ "$confirm"X = "yes"X ];                                                       
			if [ "$?" = "0" ];                                                                                    
			then                                                       
					fw_setenv ethaddr $2                               
					if [ "$?" = "0" ];                                 
					then                                               
							echo "Settings have been updated."         
					else                                               
							echo "FAILED."                             
					fi                                                 
			else                                                       
					echo "Settings haven't been updated."              
					exit 0                                    
			fi                                                
	fi                                                                      
	;;
	
	ipv6)
	if [ "$#" -ne "3" ]; 
	then
		echo "Usage: $0 ipv6 add/del ip6address. FAILED"
	else
		case "$2" in
			add)
				busybox-imx28-ipv6 ifconfig eth0 add $3
			;;
			
			del)
				busybox-imx28-ipv6 ifconfig eth0 del $3
			;;
			
			*)
				echo "FAILED"
				exit 0
			;;
		esac
		if [ "$?" = "0" ];                                 
		then                                               
				echo "Done."         
		else                                               
				echo "FAILED."                             
		fi 
	fi
	;;
	
	-q)
		busybox-imx28-ipv6 ifconfig
	;;
	
    *)
	echo "Usage: $0 static ipaddr netmask gateway"
	echo "	  $0 dhcp"
	echo "	  $0 dns nameserver"
	echo "	  $0 mac 00:00:00:00:00:00"
	echo "	  $0 ipv6 add/del ip6address"
	exit 1
	;;

esac

exit 0

