#!/bin/sh

PPP_IFNAME="ppp0"
WAN_IFNAME="brwan"
LAN_IFNAME="br0"
INTERVAL=300

touch /tmp/router-analytics/ra_support_InternetDisconnect_event
#add for ra check whether support InternetDisconnect event
set_internetDis()
{
	internet=`/bin/config get internetDisconnDuration_sec`
	reason=`echo $internet|awk '{print $1}'`
	discon=`echo $internet|awk '{print $2}'`
	connect=`echo $internet|awk '{print $3}'`
	if [ "x$reason" =  "x" -o "x$discon" =  "x" -o "x$connect" =  "x" ];then
		reason=0
		discon=0
		connect=0
	fi
	case "$1" in
		"1")
			/bin/config set internetDisconnDuration_sec="$2 $discon $connect"
		;;
		"2")
			/bin/config set internetDisconnDuration_sec="$reason $2 $connect"
		;;
		"3")
			/bin/config set internetDisconnDuration_sec="$reason $discon $2"
		;;
		"4")
			if [ "$discon" = "0" ];then
				/bin/config set internetDisconnDuration_sec="$reason $2 $connect"
			fi
		;;
	esac
	/bin/config commit
}

gw_detect()
{
	if [ "x$(cat /tmp/orbi_type)" = "xBase" ];then
		if [ "$(/bin/config get ap_mode)" = "1" ];then
			ifname=$LAN_IFNAME
			ip=`route -n |grep $ifname | grep 0.0.0.0 |awk '{print $2}' |grep -v 0.0.0.0`
		elif [ "$(/bin/config get wan_proto)" = "pppoe" -o "$(/bin/config get wan_proto)" = "pptp" -o "$(/bin/config get wan_proto)" = "l2tp" ]; then
			ifname=$PPP_IFNAME
			ip=`route -n |grep $ifname | grep 0.0.0.0 |awk '{print $1}' |grep -v 0.0.0.0`
		else
			ifname=$WAN_IFNAME
			ip=`route -n |grep $ifname | grep 0.0.0.0 |awk '{print $2}' |grep -v 0.0.0.0`
		fi
	else
		ifname=$LAN_IFNAME
		ip=`route -n |grep $ifname | grep 0.0.0.0 |awk '{print $2}' |grep -v 0.0.0.0`
	fi
	if [ "x$ip" = "x" ];then
		old_gwdiconnection_sec=`/bin/config get gwDisconnDuration_sec`
		let old_gwdiconnection_sec=old_gwdiconnection_sec+$INTERVAL
		/bin/config set gwDisconnDuration_sec=$old_gwdiconnection_sec

		old_gwdiconnection=`/bin/config get gwDisconnDuration`
		let old_gwdiconnection=old_gwdiconnection+5
		/bin/config set gwDisconnDuration=$old_gwdiconnection
		return
	fi
	ping -c 2 $ip > /tmp/ping_gw_result 2> /dev/null
	sleep 5
	result=`cat /tmp/ping_gw_result`

	if [ "x$result" = "x" ];then
		old_gwdiconnection_sec=`/bin/config get gwDisconnDuration_sec`
		let old_gwdiconnection_sec=old_gwdiconnection_sec+$INTERVAL
		/bin/config set gwDisconnDuration_sec=$old_gwdiconnection_sec

		old_gwdiconnection=`/bin/config get gwDisconnDuration`
		let old_gwdiconnection=old_gwdiconnection+5
		/bin/config set gwDisconnDuration=$old_gwdiconnection
		if [ "$discon_reason" != "4" ];then
			discon_reason=0
		fi
	else
		if [ "x$(echo $result |grep "100% packet loss")" != "x" ]; then
			old_gwdiconnection_sec=`/bin/config get gwDisconnDuration_sec`
			let old_gwdiconnection_sec=old_gwdiconnection_sec+$INTERVAL
			/bin/config set gwDisconnDuration_sec=$old_gwdiconnection_sec

			old_gwdiconnection=`/bin/config get gwDisconnDuration`
			let old_gwdiconnection=old_gwdiconnection+5
			/bin/config set gwDisconnDuration=$old_gwdiconnection
			if [ "$discon_reason" != "4" ];then
				discon_reason=0
			fi
		fi
	fi
}

net_status=0
discon_timestamp=`date +%s`
uptime1=`cat /proc/uptime | awk '{print $1}' | awk -F "." '{print $1}'`
connect_timestamp=0
discon_reason=4
#1 wan unplug
#2 cannot resolv
#3 ip cannot ping
ping_netgear()
{
    ping -c 2 www.netgear.com > /tmp/ping_netgear_result 2> /dev/null

    result=`cat /tmp/ping_netgear_result`

    if [ "x$result" = "x" ];then
		connect_timestamp=0
		if [ "$discon_timestamp" = 0 ] ;then
			discon_timestamp=`date +%s`
			set_internetDis 2 $discon_timestamp
		fi
		if [ "$discon_reason" != "4" ];then
			discon_reason=2
		fi
		net_status=0

		old_disconnetion=`/bin/config get internetDisconnDuration`
		let old_disconnetion=old_disconnetion+5
		/bin/config set internetDisconnDuration=$old_disconnetion
    else
		if [ "x$(echo $result |grep "100% packet loss")" != "x" ]; then
			connect_timestamp=0
			if [ "$discon_timestamp" = 0 ] ;then
				discon_timestamp=`date +%s`
				set_internetDis 2 $discon_timestamp
			fi
			if [ "$discon_reason" != "4" ];then
				discon_reason=3
			fi
			net_status=0

			old_disconnetion=`/bin/config get internetDisconnDuration`
			let old_disconnetion=old_disconnetion+5
			/bin/config set internetDisconnDuration=$old_disconnetion
		else
			if [ "$discon_reason" = "4" ];then
				discon_timestamp=`date +%s`
				uptime2=`cat /proc/uptime | awk '{print $1}' | awk -F "." '{print $1}'`
				discon_timestamp=`expr $discon_timestamp - $uptime2 + $uptime1`
				set_internetDis 4 $discon_timestamp
			fi
			if [ "$connect_timestamp" = 0 ] ;then
				connect_timestamp=`date +%s`
				set_internetDis 3 $connect_timestamp
				set_internetDis 1 $discon_reason
				killall -SIGUSR1 aws-iot
			fi
			discon_timestamp=0
			discon_reason=0

			if [ "$net_status" = "0" ]; then
				echo "trigger internet disconnect event"
			fi
			net_status=1
		fi
    fi
}

internet_detect()
{
	wanproto_status=`/bin/config get wan_proto` 2> /dev/null
	wandod_status=`/bin/config get wan_endis_dod` 2> /dev/null
    
    if [ "$wanproto_status" = "pppoe" -o "$wanproto_status" = "pptp" ] && [ "$apmode_status" = "0" ]; then
		# If connetion mode is dial-on-demand, then get connection status from file instead of pinging Internet.
        if [ "$wandod_status" != "1" ];then
			ping_netgear           
        fi
    else
        ping_netgear
    fi
}

while true
do
	gw_detect
	if [ "x$(cat /tmp/orbi_type)" != "xBase" ];then
		internet_detect
	fi
	sleep $INTERVAL
done



