# ! /bin/sh
# write by w
#
#
TOPDIR=/ubi/local/apps/network
TMPDIR=/run/network


[ -f $TOPDIR/network_functions ] && . $TOPDIR/network_functions


if [ ! $# ]; then
	echo "Please specify a configuration file...\n"
    exit 1
fi

if [ -f $1 ]; then
    . $1 
else
	echo "$1 file not exists...\n"
    exit 1
fi


if [ -z $DEVICE ]; then
	echo "DEV not exists...\n"
	exit 1
fi


load()
{
	if [ $ENABLE == true ]; then
		if [ -c "/dev/ttyACM0" ]; then
			echo "ttyACM0 is exist!"
			$TOPDIR/ndis_mobile -d /dev/ttyACM0 -s $TOPDIR/udhcpc.script -DL /ubi/local/apps/network/netlog -Dl 0 &
		elif [ -c "/dev/ttyUSB2" ]; then
			echo "ttyUSB2 is exist!"
			$TOPDIR/ndis_mobile -d /dev/ttyUSB2 -s $TOPDIR/udhcpc.script -Dl 0 &
		else
			echo "no wwan dev exist!"
		fi
	else
		ifconfig wwan0 down
	fi
	
}

unload()
{
	kill_process "udhcpc -i ${DEVICE}"
	kill_process "ndis_mobile"
}

monitor()
{
	sleep 60
	while true
	do
		sleep 60

		if [ $ENABLE == true ]; then
			ndis_num=`ps -ef | grep "ndis_mobile" | grep -v grep | wc -l`
			if [ $ndis_num -eq  0 ];  then
				echo "monitor!!!/r/n"
				kill_process "udhcpc -i ${DEVICE}"

				rm -rf $TMPDIR/ndis_status
				/etc/init.d/S84Reset4G.sh start
				sleep 5

				load
			fi

		fi
	done

}
case $2 in  
start)
	load
;;  
stop)  
	unload
;;  
restart) 
	unload
	load
;;
monitors)
	monitor
;;
*)
echo "Usage: /etc/init.d/if-lte.sh {start|stop|restart}"  
exit 1 ;;
esac



