#!/bin/sh
file=/work/config.json
jsoner=/usr/sbin/jsoner

apnEnable=`$jsoner -g $file 4g.apn`
enable=`$jsoner -g $file 4g.enable`
apn=`$jsoner -g $file 4g.apnName`
usr=`$jsoner -g $file 4g.user`
passwd=`$jsoner -g $file 4g.passwd`
auth=`$jsoner -g $file 4g.authType`
auth1=$auth
echo "4gEnable : $enable"
echo "apnEnable: $apnEnable"
echo "apn      : $apn"
echo "usr      : $usr"
echo "passwd   : $passwd"
echo "auth     : $authType"

#************************************************

if [ $enable != "true" ]
then 
    echo "4g is not enable. exit"
    exit
fi

if [ $auth -eq -1 ]
then 
     echo "auth is auto mode"
     auth=""
	 auth1=0
fi

#HuaWei E909
initHwWeiModule()
{
echo -e "ATE0\n\r" > /dev/ttyUSB0 
if [ $apnEnable != "true" ]
then
    echo "use common mode, init hw."
	echo -e "AT^CURC=0\n\r" > /dev/ttyUSB0
    echo  "net cmd: AT^NDISDUP=1,1" 
    echo -e "AT^NDISDUP=1,1\n\r" > /dev/ttyUSB0
else
    echo "use apn mode;"
	echo -e "AT^CURC=0\n\r" > /dev/ttyUSB0
	echo -e "AT+CFUN=0\r\n" > /dev/ttyUSB0
	sleep 1
	echo -e "AT^AUTHDATA=0,$auth1,,\"$passwd\",\"$usr\"\r\n" > /dev/ttyUSB0
	sleep 1
	echo -e "AT+CGDCONT=0,\"IP\",\"$apn\"\r\n" > /dev/ttyUSB0
	sleep 1
	echo -e "AT+CFUN=1\r\n" > /dev/ttyUSB0
	sleep 1 
	echo -e "AT^NDISDUP=1,1,\"$apn\",\"$usr\",\"$passwd\",$auth\n\r" > /dev/ttyUSB0
    echo  "net cmd: AT^NDISDUP=1,1,\"$apn\",\"$usr\",\"$passwd\",$auth" 
fi

}

initEC20xx()
{
#if [ $apnEnable != "true" ]
#then
    echo "use common mode, init ec20x."
	echo -e "AT+QCFG=\"nat\",1\r\n" > /dev/ttyUSB2
    echo  "net cmd: AT+QNETDEVCTL=2,1,1" 
    echo -e "AT+QNETDEVCTL=2,1,1\r\n" > /dev/ttyUSB2
#else
#    echo "use apn mode;"
#fi

}

counter=0
while true
do
	if [ -c /dev/ttyUSB1 ]
	then
		echo "4g module is connected!"
		break;
	else 
		echo "4g module is not connected."
	fi
	counter=`expr $counter + 1`
	if [ $counter -gt 5 ]
	then
		echo "check times: $counter break!"
		exit
	fi
	sleep 1;
done

grep -rn "15c1" /proc/tty/driver/usbserial > /dev/null
if [ $? -eq 0 ]
then 
echo "init hw module"
initHwWeiModule
else 
echo "init ec module"
initEC20xx
fi

ifconfig usb0 up
udhcpc   -i usb0 

