#!/bin/sh
exec_option=$1
cmdopt="-d edge0 -u 1000 -g 1000"

if [ -f /usr/local/data/configuration/Network/N2N.cfg ]
then
	. /usr/local/data/configuration/Network/N2N.cfg
	if [[ X"$exec_option" = X"start" ]] || [[ X"$exec_option" = X"restart" ]]
	then
		ln -s /usr/local/app/tools/edge /usr/sbin
		pid=`pidof edge`
		if [ X"$pid" != X"" ]
        	then
    	    		kill -9 $pid
        	fi
		x_tmp=$(echo $ENABLE | tr [A-Z] [a-z])
  		if [[ "$x_tmp" = "yes" ]]
		then
			if [ X"$LOCALIP" != X"" ]
			then
				cmdopt="$cmdopt -a $LOCALIP"
			fi	
			if [ X"$USERNAME" != X"" ]
			then
				cmdopt="$cmdopt -c $USERNAME"
			fi
			if [ X"$PASSWORD" != X"" ]
			then
				cmdopt="$cmdopt -k $PASSWORD"
			fi
			if [ X"$MAC" != X"" ]
			then
				cmdopt="$cmdopt -m $MAC"
			fi
			if [ X"$MTU" != X"" ]
			then
				cmdopt="$cmdopt -M $MTU"
			fi
			if [ X"$SUBNETMASK" != X"" ]
			then
				cmdopt="$cmdopt -s $SUBNETMASK"
			fi
			if [ X"$SERVERIP" != X"" ]
			then
				cmdopt="$cmdopt -l $SERVERIP"
			fi
			if [ X"$PORT" != X"" ]
			then
				cmdopt="$cmdopt:$PORT"
			fi
			edge $cmdopt &
			if [ "$?" != "0" ]
			then
	    			exit 1
			fi
		fi
	elif [[ X"$exec_option" = X"stop" ]]
	then
		pid=`pidof edge`
		if [ X"$pid" != X"" ]
        	then
    	   		kill -9 $pid
        	fi
	fi
else
	echo 'please config n2n first'
fi
