#!/bin/sh
# 

. /usr/bin/scriptlib

WANTYPE=`rdcsman 0x00010003 u16`

sl_get_IP_NM_GW_in_ifconfig $IFNAME IF_IP IF_NM IF_GW
sl_get_DNS_in_resolv_conf IF_DNS1 IF_DNS2
sl_get_uptime CNTTIME

BACKUP=`rdcsman 0x001e000d u16`
VENDORID=`cat /var/usbserial | /bin/awk '{print $2}'`
# when Backup 3G enabled, local wan type is not 3G and pppd up the pppd_pid=ppp-3g.pid, now this is Backup 3G wan type
if [ $BACKUP -eq 1 ] && [ $WANTYPE -ne 16 ] && [ $WANTYPE -ne 17 ] ; then
	if [ "$VENDORID" != "482" ] ; then 
		PIDFILE_PPPD="/var/run/ppp-3g.pid"
		sl_get_ppp_PID_IFname $PIDFILE_PPPD PPP_PID PPP_IF
		if [ $PPPD_PID -eq $PPP_PID ] ; then
			wrcsman "0x801e0100 $CNTTIME && \
					0x801e0200 {$IF_IP} && \
					0x801e0300 {$IF_NM} && \
					0x801e0400 {$IF_GW} && \
					0x801e0500 {$IF_DNS1} && \
					0x801e0600 {$IF_DNS2} && \
					0x801e0700 0x07 && \
					0x801e1200 0x02 && \
					0x80010002 {$IF_IP} && \
					0x80010003 {$IF_NM} && \
					0x80010004 {$IF_GW} && \
					0x80010005 {$IF_DNS1} && \
					0x80010006 {$IF_DNS2} && \
					0x8001000B \"$IFNAME && \
					0x801E2200 \"$IFNAME && \
					0x801e1100 0x00"

			echo "nameserver $IF_DNS1" > /etc/resolv.conf
			echo "nameserver $IF_DNS2" >> /etc/resolv.conf

			if [ $WANTYPE -eq 0 ] || [ $WANTYPE -eq 32 ] ; then
				IF_WAN=`rdcsman 0x8000f361 str`
				wrcsman "0x80010300 \"$IF_WAN"
			else
				if [ $IFNAME == ppp0 ] ; then
					wrcsman "0x80010300 \"ppp1"
				else
					wrcsman "0x80010300 \"ppp0"
				fi
			fi
			exit 0
		fi
	else
		PIDFILE_PPPD="/var/run/ppp-iburst.pid"
		sl_get_ppp_PID_IFname $PIDFILE_PPPD PPP_PID PPP_IF
		if [ $PPPD_PID -eq $PPP_PID ] ; then
			wrcsman "0x80340100 $CNTTIME && \
					0x80340200 {$IF_IP} && \
					0x80340300 {$IF_NM} && \
					0x80340400 {$IF_GW} && \
					0x80340500 {$IF_DNS1} && \
					0x80340600 {$IF_DNS2} && \
					0x80340700 0x07 && \
					0x80010002 {$IF_IP} && \
					0x80010003 {$IF_NM} && \
					0x80010004 {$IF_GW} && \
					0x80010005 {$IF_DNS1} && \
					0x80010006 {$IF_DNS2} && \
					0x8001000B \"$IFNAME && \
					0x801E2200 \"$IFNAME && \
					0x801e1200 0x02"

			echo "nameserver $IF_DNS1" > /etc/resolv.conf
			echo "nameserver $IF_DNS2" >> /etc/resolv.conf

			if [ $WANTYPE -eq 0 ] || [ $WANTYPE -eq 32 ] ; then
				IF_WAN=`rdcsman 0x8000f361 str`
				wrcsman "0x80010300 \"$IF_WAN"
			else
				wrcsman "0x80010300 \"ppp0"
			fi
			exit 0
		fi
	fi
fi
	
if [ $WANTYPE -eq 64 ] ; then
#PPPoE interface up
	wrcsman "0x80040100 $CNTTIME && \
			 0x80040200 {$IF_IP} && \
			 0x80040300 {$IF_NM} && \
			 0x80040400 {$IF_GW} && \
			 0x80040500 {$IF_DNS1} && \
			 0x80040600 {$IF_DNS2} && \
			 0x80040700 0x02 && \
			 0x80010002 {$IF_IP} && \
			 0x80010003 {$IF_NM} && \
			 0x80010004 {$IF_GW} && \
			 0x80010005 {$IF_DNS1} && \
			 0x80010006 {$IF_DNS2} && \
			 0x8001000B \"$IFNAME && \
			 0x80010007 0x02"
	echo "nameserver $IF_DNS1" > /etc/resolv.conf
	echo "nameserver $IF_DNS2" >> /etc/resolv.conf

elif [ $WANTYPE -eq 65 ] ; then
#MPPPoE interface up	
	sl_get_MPPP_PID_session $PPPD_PID S_NO
		
	wrcsman "0x8004020$S_NO {$IF_IP} && \
		 	 0x8004030$S_NO {$IF_NM} && \
			 0x8004040$S_NO {$IF_GW} && \
			 0x8004050$S_NO {$DNS1} && \
			 0x8004060$S_NO {$DNS2} && \
			 0x8004070$S_NO 0x02 && \
			 0x80010002 {$IF_IP} && \
			 0x80010003 {$IF_NM} && \
			 0x80010004 {$IF_GW} && \
			 0x80010005 {$DNS1} && \
			 0x80010006 {$DNS2} && \
			 0x80010007 0x02"

	RT_DNS1=`rdcsman 0x8004050$S_NO ipv4` 
	if [ $RT_DNS1 != 0.0.0.0 ] ; then
		route add -host $RT_DNS1 dev $IFNAME 
	fi
		
	RT_DNS2=`rdcsman 0x8004060$S_NO ipv4` 
	if [ $RT_DNS2 != 0.0.0.0 ] ; then
		route add -host $RT_DNS2 dev $IFNAME 
	fi

	DEF_DNS1=`rdcsman 0x0004070$S_NO ipv4` 
	if [ $DEF_DNS1 != 0.0.0.0 ] ; then
		wrcsman "0x8004050$S_NO {$DEF_DNS1}"
		route add -host $DEF_DNS1 dev $IFNAME 
	fi

	DEF_DNS2=`rdcsman 0x0004080$S_NO ipv4` 
	if [ $DEF_DNS2 != 0.0.0.0 ] ; then
		wrcsman "0x8004060$S_NO {$DEF_DNS2}"
		route add -host $DEF_DNS2 dev $IFNAME 
	fi

	/usr/bin/uvm -m 740 -o /etc/ppp/ip-up.mpppoe /usr/uo/ip-up.mpppoe.uo
	/etc/ppp/ip-up.mpppoe

elif [ $WANTYPE -eq 96 ] ; then
#PPTP interface up
	wrcsman "0x80050100 $CNTTIME && \
		 	 0x80050200 {$IF_IP} && \
			 0x80050300 {$IF_NM} && \
			 0x80050400 {$IF_GW} && \
			 0x80050500 {$IF_DNS1} && \
			 0x80050600 {$IF_DNS2} && \
			 0x80050700 0x02 && \
			 0x80010002 {$IF_IP} && \
			 0x80010003 {$IF_NM} && \
			 0x80010004 {$IF_GW} && \
			 0x80010005 {$IF_DNS1} && \
			 0x80010006 {$IF_DNS2} && \
			 0x8001000B \"$IFNAME && \
			 0x80010007 0x02"
			
	echo "nameserver $IF_DNS1" > /etc/resolv.conf
	echo "nameserver $IF_DNS2" >> /etc/resolv.conf
  
elif [ $WANTYPE -eq 128 ] ; then
	
	wrcsman "0x80060100 $CNTTIME && \
		 	 0x80060200 {$IF_IP} && \
			 0x80060300 {$IF_NM} && \
			 0x80060400 {$IF_GW} && \
			 0x80060500 {$IF_DNS1} && \
			 0x80060600 {$IF_DNS2} && \
			 0x80060700 0x02 && \
		 	 0x80010002 {$IF_IP} && \
			 0x80010003 {$IF_NM} && \
			 0x80010004 {$IF_GW} && \
			 0x80010005 {$IF_DNS1} && \
			 0x80010006 {$IF_DNS2} && \
			 0x8001000B \"$IFNAME && \
			 0x80010007 0x02"
			
	echo "nameserver $IF_DNS1" > /etc/resolv.conf
	echo "nameserver $IF_DNS2" >> /etc/resolv.conf		

elif [ $WANTYPE -eq 16 ] ; then
	wrcsman "0x801e0100 $CNTTIME && \
			0x801e0200 {$IF_IP} && \
			0x801e0300 {$IF_NM} && \
			0x801e0400 {$IF_GW} && \
			0x801e0500 {$IF_DNS1} && \
			0x801e0600 {$IF_DNS2} && \
			0x801e0700 0x02 && \
		    0x80010002 {$IF_IP} && \
			0x80010003 {$IF_NM} && \
			0x80010004 {$IF_GW} && \
			0x80010005 {$IF_DNS1} && \
			0x80010006 {$IF_DNS2} && \
			0x8001000B \"$IFNAME && \
			0x80010007 0x02 && \
			0x801e1100 0x00"

	echo "nameserver $IF_DNS1" > /etc/resolv.conf
	echo "nameserver $IF_DNS2" >> /etc/resolv.conf
			 
elif [ $WANTYPE -eq 17 ] ; then
#iBurst interface up
	wrcsman "0x80340100 $CNTTIME && \
			 0x80340200 {$IF_IP} && \
			 0x80340300 {$IF_NM} && \
			 0x80340400 {$IF_GW} && \
			 0x80340500 {$IF_DNS1} && \
			 0x80340600 {$IF_DNS2} && \
			 0x80340700 0x02 && \
			 0x80010002 {$IF_IP} && \
			 0x80010003 {$IF_NM} && \
			 0x80010004 {$IF_GW} && \
			 0x80010005 {$IF_DNS1} && \
			 0x80010006 {$IF_DNS2} && \
			 0x8001000B \"$IFNAME && \
			 0x80010007 0x02"

	echo "nameserver $IF_DNS1" > /etc/resolv.conf
	echo "nameserver $IF_DNS2" >> /etc/resolv.conf

fi

