#!/bin/sh
CFG=/etc/hwinfo.json
. /usr/share/libubox/jshn.sh


set_band_for_HL() {
	if [ "$1" == "e21x" ]; then
		uci set network.cellular.band1=$2
		uci set network.cellular.band2=$2

	elif [ "$1" == "e22x" ]; then
		uci set network.cellular.band=$2
	fi
}

set_service_type_for_HL() {
	if [ "$1" == "e21x" ]; then
		uci set network.cellular.service1=$2
		uci set network.cellular.service2=$2

	elif [ "$1" == "e22x" ]; then
		uci set network.cellular.service=$2
	fi
}

generate_gsm_cellular(){
	local ifname vid pid name protocol device
	local board=`cat /tmp/sysinfo/board_name`
	
	json_select module
		json_select $1
			json_get_vars ifname modulename protocol device ipaccess
			json_select usedatport
				json_get_var usedatport 1
			json_select ..	
		json_select ..
	json_select ..
	
	echo "ifname:$ifname, modulename:$modulename, protocol:$protocol device:$device ipaccess:$ipaccess usedatport:$usedatport"

	[ "`uci get network.cellular`" == "interface" ] || {
		if [ "$protocol" == "qmi" ]; then
			if [ "$board" == "e21x" ]; then
				uci -q batch <<-EOF
					set network.cellular='interface'
					set network.cellular.proto='$protocol'
					set network.cellular.device='$device'
					set network.cellular.module='$modulename'
					set network.cellular.ifname='$ifname'
					set network.cellular.simpriority=1
					set network.cellular.apn1=''
					set network.cellular.apn2=''
					set network.cellular.auth1='none'
					set network.cellular.auth2='none'
					set network.cellular.username1=''
					set network.cellular.username2=''
					set network.cellular.password1=''
					set network.cellular.password2=''
					set network.cellular.pincode1=''
					set network.cellular.pincode2=''
					set network.cellular.ccid1=''
					set network.cellular.ccid2=''
					set network.cellular.roam1=0
					set network.cellular.roam2=0
					set network.cellular.band1=0
					set network.cellular.band2=0
					set network.cellular.metric=7
					set network.cellular.atport='$usedatport'
					set network.cellular.board='$board'
					set network.cellular.service1='auto'
					set network.cellular.service2='auto'
					set network.cellular.retries='5'
					set network.cellular.toswitch='60'
					set network.cellular.forceswitch='0'
					set network.cellular.cid1=1
					set network.cellular.cid2=1
					set network.cellular.pdptype1='IPV4V6'
					set network.cellular.pdptype2='IPV4V6'
					set network.cellular.passthrough='0'
					set network.cellular.dhcpv6='1'
				EOF
			elif [ "$board" == "e22x" ]; then
				uci -q batch <<-EOF
					set network.cellular='interface'
					set network.cellular.proto='$protocol'
					set network.cellular.device='$device'
					set network.cellular.module='$modulename'
					set network.cellular.pdptype='IPV4V6'
					set network.cellular.auth='none'
					set network.cellular.ifname='$ifname'

					set network.cellular.apn=''
					set network.cellular.password=''
					set network.cellular.pincode=''
					set network.cellular.ccid=''
					set network.cellular.roam=0
					set network.cellular.band=0
					set network.cellular.metric=7
					set network.cellular.atport='$usedatport'
					set network.cellular.board='$board'
					set network.cellular.service='auto'
					set network.cellular.cid=1
					set network.cellular.passthrough='0'
					set network.cellular.dhcpv6='1'
				EOF
			fi
		elif [ "$protocol" == "cellular" ];then
			if [ "$board" == "e21x" ]; then
				uci -q batch <<-EOF
					delete network.cellular
					set network.cellular=interface
					set network.cellular.proto='$protocol'
					set network.cellular.ifname='$ifname'
					set network.cellular.device='$device'
					set network.cellular.module='$modulename'
					set network.cellular.ipaccess='$ipaccess'
					set network.cellular.simpriority=1
					set network.cellular.apn1=''
					set network.cellular.apn2=''
					set network.cellular.auth1='none'
					set network.cellular.auth2='none'
					set network.cellular.username1=''
					set network.cellular.username2=''
					set network.cellular.password1=''
					set network.cellular.password2=''
					set network.cellular.pincode1=''
					set network.cellular.pincode2=''
					set network.cellular.ccid1=''
					set network.cellular.ccid2=''
					set network.cellular.roam1=0
					set network.cellular.roam2=0
					set network.cellular.cid1=1
					set network.cellular.cid2=1
					set network.cellular.metric=7
					set network.cellular.force_link=1
					set network.cellular.mode=0
					set network.cellular.mtu=1420
					set network.cellular.roam=0
					set network.cellular.board='$board'
					set network.cellular.retries='5'
					set network.cellular.toswitch='60'
					set network.cellular.forceswitch='0'
					set network.cellular.pdptype1='IPV4V6'
					set network.cellular.pdptype2='IPV4V6'

				EOF
			elif [ "$board" == "e22x" ];then
				uci -q batch <<-EOF
					delete network.cellular
					set network.cellular=interface
					set network.cellular.proto='$protocol'
					set network.cellular.ifname='$ifname'
					set network.cellular.device='$device'
					set network.cellular.module='$modulename'
					set network.cellular.ipaccess='$ipaccess'
					set network.cellular.pdptype='IPV4V6'
					set network.cellular.apn=''
					set network.cellular.auth='none'
					set network.cellular.username=''
					set network.cellular.metric=7
					set network.cellular.force_link=1
					set network.cellular.mode=0
					set network.cellular.mtu=1420
					set network.cellular.toswitch=60
					set network.cellular.roam=0
					set network.cellular.cid=1
					set network.cellular.board='$board'
				EOF
			fi

			case $modulename in
				HL7518* | \
				HL7548* )
					sendat $device at+kusbcomp=1 1
					sendat $device at+cfun=1,1 0

					uci set network.cellular.maxports=3
					
					set_service_type_for_HL $board 5
					# uci set network.cellular.service=5
				;;

				HL8528* | \
				HL8518* | \
				HL8548*)
					sendat $device at+kusbcomp=2 1
					sendat $device at+cfun=1,1 0

					uci set network.cellular.maxports=5
					set_service_type_for_HL $board 4
				;;

				HL7588*)
					sendat $device at+kusbcomp=0 1
					sendat $device at+cfun=1,1 0

					uci set network.cellular.maxports=3
					set_service_type_for_HL $board 7
				;;

				HL7538*)
					uci set network.cellular.maxports=3
					set_service_type_for_HL $board 5
				;;

				HL7539*)
					uci set network.cellular.maxports=3
					set_service_type_for_HL $board 5
				;;

				HL7528*)
					sendat $device at+kusbcomp=0 1
					sendat $device 'at*psrdbs=1,108544' 1
					sendat $device "AT+CARRIER=0,SWK" 1
					sendat $device at+cfun=1,1 0

					uci set network.cellular.maxports=3
					set_service_type_for_HL $board 5
					set_band_for_HL $board 0
				;;

				HL7690* )
					sendat $device at+kusbcomp=0 1
					sendat $device at+cfun=1,1 0
					
					uci set network.cellular.maxports=3
					set_service_type_for_HL $board 5
				;;

				HL7692* )
					sendat $device at+kusbcomp=0 1
					sendat $device at+cfun=1,1 0

					uci set network.cellular.maxports=3
					set_service_type_for_HL $board 9
				;;

				HL7618*)	
					sendat $device at+kusbcomp=0 1
					sendat $device at+cfun=1,1 0

					uci set network.cellular.maxports=3
					set_service_type_for_HL $board 5
				;;

				HL7549* )
					sendat $device at+kusbcomp=0 1
					sendat $device at+cfun=1,1 0

					uci set network.cellular.maxports=3
					set_service_type_for_HL $board 5
				;;

				HL7648* | \
				HL7650*)
					sendat $device at+kusbcomp=0 1
					sendat $device at+cfun=1,1 0
					
					uci set network.cellular.maxports=3
					set_service_type_for_HL $board 7
				;;
			esac

		elif [ "$protocol" == "3g" ];then
			if [ "$board" == "e21x" ]; then
				uci -q batch <<-EOF
					set network.cellular='interface'
					set network.cellular.proto='$protocol'
					set network.cellular.device='$device'
					set network.cellular.module='$modulename'
					set network.cellular.pdptype='ipv4'
					set network.cellular.simpriority=1
					set network.cellular.apn1=''
					set network.cellular.apn2=''
					set network.cellular.auth1='none'
					set network.cellular.auth2='none'
					set network.cellular.username1=''
					set network.cellular.username2=''
					set network.cellular.password1=''
					set network.cellular.password2=''
					set network.cellular.pincode1=''
					set network.cellular.pincode2=''
					set network.cellular.ccid1=''
					set network.cellular.ccid2=''
					set network.cellular.roam1=0
					set network.cellular.roam2=0
					set network.cellular.band1=0
					set network.cellular.band2=0
					set network.cellular.cid1=1
					set network.cellular.cid2=1
					set network.cellular.metric=7
					set network.cellular.atport='$usedatport'
					set network.cellular.board='$board'
					set network.cellular.retries='5'
					set network.cellular.toswitch='60'
					set network.cellular.forceswitch='0'
				EOF
			elif [ "$board" == "e22x" ]; then
				uci -q batch <<-EOF
					set network.cellular='interface'
					set network.cellular.proto='$protocol'
					set network.cellular.device='$device'
					set network.cellular.module='$modulename'
					set network.cellular.pdptype='ipv4'
					set network.cellular.auth='none'

					set network.cellular.apn=''
					set network.cellular.password=''
					set network.cellular.pincode=''
					set network.cellular.ccid=''
					set network.cellular.roam=0
					set network.cellular.band=0
					set network.cellular.cid=1
					set network.cellular.metric=7
					set network.cellular.atport='$usedatport'
					set network.cellular.board='$board'
				EOF
			fi
			case $modulename in
				HL7802)
					sendat $device "at+kbndcfg=0,0000000000000F0F1B9F" 1
					sendat $device "at+kbndcfg=1,0000000000000B0F189F" 1
					sendat $device at+cfun=1,1 0
					set_service_type_for_HL $board cat-m1
				;;
				*)
					set_service_type_for_HL $board auto
				;;
			esac


		fi

		sleep 1
		#added at+cops=0 to set auto at factory reset
		sendat $device AT+COPS=0 1
	}
}


if [ -n "$1" -a "$1" == "1" ];then
	count=120
	while [ $count -gt 0 ]; do
		mount_root=`ps | grep mount_root | grep -v grep`
		if [ ! -z "$mount_root" ]; then
			break
		fi
		sleep 5
		count=`expr $count - 5`
	done

	while [ 1 ]; do
		mount_root=`ps | grep mount_root | grep -v grep`
		if [ -z "$mount_root" ]; then
			break
		fi
		sleep 5
	done
fi
json_init
#board_name=`cat /tmp/sysinfo/board_name`
#echo " board name $board_name"
json_load "$(cat ${CFG})"
json_select gsm
json_get_keys keys module
for key in $keys; do generate_gsm_cellular $key;done
uci commit
/etc/init.d/network restart
