🏑 index : github.com/captn3m0/RT-AX53U.git

#!/bin/sh
# $1: output, $2: special IMSI.
# environment variable: unit - modem unit.
# echo "This is a script to capture the settings of SIM."


if [ -z "$unit" ] || [ "$unit" -eq "0" ]; then
	prefix="usb_modem_"
else
	prefix="usb_modem${unit}_"
fi

modem_model=`nvram get modem_model`
modem_type=`nvram get ${prefix}act_type`
modem_vid=`nvram get ${prefix}act_vid`
modem_autoapn=`nvram get modem_autoapn`
modem_imsi=
apps_local_space=`nvram get apps_local_space`
dataf="$apps_local_space/spn_asus.dat"


if [ "$modem_type" == "" -o  "$modem_type" == "ecm" -o "$modem_type" == "rndis" -o "$modem_type" == "asix" -o "$modem_type" == "ncm" ] && [ "$modem_model" == "2" -a "$modem_type" != "ncm" ]; then
	exit 0
fi

if [ "$1" = "set" ]; then
	modem_imsi=$2
else
	modem_imsi=`nvram get ${prefix}act_imsi |cut -c '1-6' 2>/dev/null`
fi
if [ -z "$modem_imsi" ]; then
	/usr/sbin/modem_status.sh imsi
	modem_imsi=`nvram get ${prefix}act_imsi |cut -c '1-6' 2>/dev/null`
	if [ -z "$modem_imsi" ]; then
		echo "Can't get IMI of SIM!"
		exit 2
	fi
fi

if [ "$1" = "set" ]; then
	content=`awk '/^'"$modem_imsi"',/ {print $0 "," NR; exit}' $dataf 2>/dev/null`
else
	total_line=`wc -l $dataf |awk '{print $1}' 2>/dev/null`
	nvram set ${prefix}auto_lines=$total_line
	nvram set ${prefix}auto_running=1

	modem_imsi_s=`echo $modem_imsi |cut -c 1-6 2>/dev/null`
	content=`awk '/^'"$modem_imsi_s"',/ {print $0 "," NR; exit}' $dataf 2>/dev/null`
	if [ -z "$content" ]; then
		modem_imsi_s=`echo $modem_imsi |cut -c 1-5 2>/dev/null`
		content=`awk '/^'"$modem_imsi_s"',/ {print $0 "," NR; exit}' $dataf 2>/dev/null`
	fi

	nvram set ${prefix}auto_running=$total_line
fi

if [ -z "$content" ]; then
	nvram set g3err_imsi=1
	echo "Can't get the APN mapping automatically!"
	exit 3
fi

compare=`echo "$content" |awk 'BEGIN{FS=","}{print $1}' 2>/dev/null`
modem_isp=`echo "$content" |awk 'BEGIN{FS=","}{print $2}' 2>/dev/null`
modem_spn=`echo "$content" |awk 'BEGIN{FS=","}{print $7}' 2>/dev/null`
[ -z "$modem_spn" ] && modem_spn="$modem_isp"
modem_apn=`echo "$content" |awk 'BEGIN{FS=","}{print $6}' 2>/dev/null`
modem_dial=`echo "$content" |awk 'BEGIN{FS=","}{print $3}' 2>/dev/null`
modem_user=`echo "$content" |awk 'BEGIN{FS=","}{print $4}' 2>/dev/null`
modem_pass=`echo "$content" |awk 'BEGIN{FS=","}{print $5}' 2>/dev/null`
line=`echo "$content" |awk 'BEGIN{FS=","}{print $8}' 2>/dev/null`

modem_country_num=`echo $modem_imsi |cut -c 1-3 2>/dev/null`
if [ -z "$modem_country_num" ]; then
	echo "Cant get the country number from IMSI."
elif [ $modem_country_num -eq 505 ]; then
	modem_country=AU
elif [ $modem_country_num -eq 232 ]; then
	modem_country=AT
elif [ $modem_country_num -eq 218 ]; then
	modem_country=BA
elif [ $modem_country_num -eq 724 ]; then
	modem_country=BR
elif [ $modem_country_num -eq 528 ]; then
	modem_country=BN
elif [ $modem_country_num -eq 284 ]; then
	modem_country=BG
elif [ $modem_country_num -eq 302 ]; then
	modem_country=CA
elif [ $modem_country_num -eq 460 ]; then
	modem_country=CN
elif [ $modem_country_num -eq 230 ]; then
	modem_country=CZ
elif [ $modem_country_num -eq 238 ]; then
	modem_country=DK
elif [ $modem_country_num -eq 370 ]; then
	modem_country=DO
elif [ $modem_country_num -eq 602 ]; then
	modem_country=EG
elif [ $modem_country_num -eq 244 ]; then
	modem_country=FI
elif [ $modem_country_num -eq 262 ]; then
	modem_country=DE
elif [ $modem_country_num -eq 454 ]; then
	modem_country=HK
elif [ $modem_country_num -eq 404 ]; then
	modem_country=IN
elif [ $modem_country_num -eq 510 ]; then
	modem_country=ID
elif [ $modem_country_num -eq 222 ]; then
	modem_country=IT
elif [ $modem_country_num -eq 440 ]; then
	modem_country=JP
elif [ $modem_country_num -eq 502 ]; then
	modem_country=MY
elif [ $modem_country_num -eq 204 ]; then
	modem_country=NL
elif [ $modem_country_num -eq 530 ]; then
	modem_country=NZ
elif [ $modem_country_num -eq 242 ]; then
	modem_country=NO
elif [ $modem_country_num -eq 515 ]; then
	modem_country=PH
elif [ $modem_country_num -eq 260 ]; then
	modem_country=PL
elif [ $modem_country_num -eq 268 ]; then
	modem_country=PT
elif [ $modem_country_num -eq 226 ]; then
	modem_country=RO
elif [ $modem_country_num -eq 250 ]; then
	modem_country=RU
elif [ $modem_country_num -eq 525 ]; then
	modem_country=SG
elif [ $modem_country_num -eq 231 ]; then
	modem_country=SK
elif [ $modem_country_num -eq 655 ]; then
	modem_country=ZA
elif [ $modem_country_num -eq 214 ]; then
	modem_country=ES
elif [ $modem_country_num -eq 240 ]; then
	modem_country=SE
elif [ $modem_country_num -eq 466 ]; then
	modem_country=TW
elif [ $modem_country_num -eq 520 ]; then
	modem_country=TH
elif [ $modem_country_num -eq 286 ]; then
	modem_country=TR
elif [ $modem_country_num -eq 255 ]; then
	modem_country=UA
elif [ $modem_country_num -eq 234 ]; then
	modem_country=UK
elif [ $modem_country_num -eq 310 ]; then
	modem_country=US
elif [ $modem_country_num -eq 452 ]; then
	modem_country=VN
elif [ $modem_country_num -eq 425 ]; then
	modem_country=IL
fi

if [ "$1" = "console" ]; then
	echo "   line: $line."
	echo "   imsi: $modem_imsi."
	echo "country: $modem_country."
	echo "    isp: $modem_isp."
	echo "    apn: $modem_apn."
	echo "    spn: $modem_spn."
	echo "   dial: $modem_dial."
	echo "   user: $modem_user."
	echo "   pass: $modem_pass."
elif [ "$1" = "set" ]; then
	nvram set ${prefix}auto_imsi="$modem_imsi"
	nvram set modem_country="$modem_country"
	modem_isp=`nvram get modem_roaming_isp`
	nvram set modem_isp="$modem_isp"
	nvram set modem_spn="$modem_spn"
	nvram set modem_apn="$modem_apn"
	nvram set modem_dialnum="$modem_dial"
	nvram set modem_user="$modem_user"
	nvram set modem_pass="$modem_pass"
else
	nvram set ${prefix}auto_imsi="$compare"
	nvram set ${prefix}auto_country="$modem_country"
	nvram set ${prefix}auto_isp="$modem_isp"
	nvram set ${prefix}auto_spn="$modem_spn"
	nvram set ${prefix}auto_apn="$modem_apn"
	nvram set ${prefix}auto_dialnum="$modem_dial"
	nvram set ${prefix}auto_user="$modem_user"
	nvram set ${prefix}auto_pass="$modem_pass"

	if [ "$modem_autoapn" = "1" ]; then
		nvram set modem_country="$modem_country"
		nvram set modem_isp="$modem_isp"
		nvram set modem_spn="$modem_spn"
		nvram set modem_apn="$modem_apn"
		nvram set modem_dialnum="$modem_dial"
		nvram set modem_user="$modem_user"
		nvram set modem_pass="$modem_pass"
	fi
fi