#!/bin/sh
exec_option=$1
> /etc/resolv.conf

if [ ! -f /usr/local/data/configuration/Network/Wan.cfg ];then
	echo 'Hi, guy, i need config file'
	exit 1
fi

ifconfig lo up
if [ ! -f /usr/local/data/tmp/prdt_info.cfg ];then
	echo 'System is boot now'
	exit 0
fi

. /usr/local/data/configuration/Network/Wan.cfg
Model_Name=`cat /usr/local/data/tmp/prdt_info.cfg | grep Model_Name | cut -d '=' -f 2`
WifiEnable_tmp=$(echo $WifiEnable | tr [A-Z] [a-z])
ifconfig wlan0 &>/dev/null && IS_SUPPORTWIFI_TMP=yes || IS_SUPPORTWIFI_TMP=no
HostName=""
ifconfig eth0:1 down

HostName="$dhcpOption12"
[ -n "$dhcpOption60" ] && Model_Name="$dhcpOption60"

if [[ "$NETWORKTYPE" -le "1" ]]
then
	echo 1 >/proc/sys/net/ipv6/conf/all/disable_ipv6
else
	echo 0 >/proc/sys/net/ipv6/conf/all/disable_ipv6
fi

fflush_dev()
{
	if [[ "$NETWORKTYPE" -gt "1" ]];then
		ip -6 address flush dev eth0 scope global 
		[[ "$IS_SUPPORTWIFI_TMP" = "yes" ]] && ip -6 address flush dev wlan0 scope global
	else
		ip -6 address flush dev eth0
		[[ "$IS_SUPPORTWIFI_TMP" = "yes" ]] && ip -6 address flush dev wlan0
	fi
}

start_pppoe()
{
	killall pppd2
	echo "$USERNAME * $PASSWORD *" >/etc/ppp/chap-secrets
	echo "$USERNAME * $PASSWORD *" >/etc/ppp/pap-secrets
	pppd2 pty '/usr/sbin/pppoe -I eth0' user $USERNAME usepeerdns persist maxfail 10 holdoff 30 defaultroute &
	exit 0
}

start_ipv4()
{
	if [[ "$CONNECTMODE" == DHCP ]]
	then
		if [[ "$IS_SUPPORTWIFI_TMP" = "yes" ]] && [[ "$WifiEnable_tmp" = "yes" ]]
		then
			DHCP_PARAM="wlan0"
		else
			DHCP_PARAM="eth0"
		fi
		if [[ "$StaticDNSEnable" = "Yes" ]]
		then
			DHCP_PARAM="$DHCP_PARAM -R"
		fi
		if [ -z "$HostName" ];then
			dhcpcd $DHCP_PARAM -i "$Model_Name" &
		else
			dhcpcd $DHCP_PARAM -i "$Model_Name" -h "$HostName" &
		fi
		if [ "$?" != "0" ]
		then
			exit 1
		fi
		sleep 3
	elif [[ "$CONNECTMODE" == STATIC ]]
	then
		if [[ "$IS_SUPPORTWIFI_TMP" =  "yes" ]] && [[ "$WifiEnable_tmp" = "yes" ]]
		then
			ip addr flush dev eth0
			ifconfig wlan0 $IPADDRESS netmask $NETMASK up
			route add default gw $GATEWAY dev wlan0
		else
			ip addr flush dev wlan0
			ifconfig eth0 $IPADDRESS netmask $NETMASK up
			route add default gw $GATEWAY dev eth0
		fi
		[ -n "$DNS1" ] && echo "nameserver  $DNS1" >> /etc/resolv.conf
		[ -n "$DNS2" ] && echo "nameserver  $DNS2" >> /etc/resolv.conf

	elif [[ "$NETWORKTYPE" -ne "2" ]]
	then
		echo "only support STATIC DHCP connect type for current version!"
	fi
}

start_ipv6()
{
	if [[ "$CONNECT6MODE" == DHCP ]] && [[ "$NETWORKTYPE" -ne "1" ]]
	then
		if [[ "$IS_SUPPORTWIFI_TMP" = "yes" ]] && [[ "$WifiEnable_tmp" = "yes" ]]
		then
			odhcp6c wlan0 &
		else
			odhcp6c eth0  &
		fi
		if [ "$?" != "0" ]
		then
			exit 1
		fi
		sleep 3
	elif [[ "$CONNECT6MODE" == STATIC ]] && [[ "$NETWORKTYPE" -ne "1" ]] 
	then
		if [[ "$IS_SUPPORTWIFI_TMP" =  "yes" ]] && [[ "$WifiEnable_tmp" = "yes" ]]
		then
			ip addr flush dev eth0
			ifconfig wlan0 add $IPADDRESS6/$NETPREFIX up
			route -A inet6 add default gw $GATEWAY6 dev wlan0
		else
			ip addr flush dev wlan0
			ifconfig eth0 add $IPADDRESS6/$NETPREFIX up
			route -A inet6 add default gw $GATEWAY6 dev eth0
		fi
		[ -n "$DNS61" ] && echo "nameserver  $DNS61" >> /etc/resolv.conf
		[ -n "$DNS62" ] && echo "nameserver  $DNS62" >> /etc/resolv.conf

	elif [[ "$NETWORKTYPE" -ne "1" ]]
	then
		echo "only support STATIC DHCP connect type for current version!"
	fi
}

if [ X"$exec_option" = X"start" ]
then
	ifconfig eth0 0.0.0.0
	if [[ "$IS_SUPPORTWIFI_TMP" = "yes" ]] && [[ "$WifiEnable_tmp" = "yes" ]];then
		ifconfig wlan0 0.0.0.0
		wifi_cmd recap
	elif [[ "$IS_SUPPORTWIFI_TMP" = "yes" ]];then
		wifi_cmd disap
		ifconfig wlan0 0.0.0.0
	fi

	fflush_dev
	[ X"$CONNECTMODE" = X"PPPOE" ] && start_pppoe
	[ "$NETWORKTYPE" -ne "2" ] && start_ipv4
	[ "$NETWORKTYPE" -ne "1" ] && start_ipv6
elif [ X"$exec_option" = X"restart" ]
then
	killall -s 9 odhcp6c dhcpcd
	ifconfig eth0 0.0.0.0

	[[ "$IS_SUPPORTWIFI_TMP" = "yes" ]] && ifconfig wlan0 0.0.0.0

	fflush_dev
	[ "$CONNECTMODE" = "PPPOE" ] && start_pppoe
	[ "$NETWORKTYPE" -ne "2" ] && start_ipv4
	[ "$NETWORKTYPE" -ne "1" ] && start_ipv6
elif [[ X"$exec_option" = X"stop" ]]
then
	killall -9 dhcpcd odhcp6c pppd2
	[[ "$IS_SUPPORTWIFI_TMP" = "yes" ]] && [[ "$WifiEnable_tmp" = "no" ]] && wifi_cmd disap
else
	echo 'usage networking start|stop'
fi

exit 0
