#!/bin/sh

LAN_RX_PACKET_PREV=0
WAN_RX_PACKET_PREV=0
echo -n "Boot system  at: " >> /mnt/flashfs/configs/current/network_restart_times
date >> /mnt/flashfs/configs/current/network_restart_times
while [ 1 ]; do
	LAN_LINK=$(mii-tool lan0 | grep ok)
	WAN_LINK=$(mii-tool wan0 | grep ok)
	if [ "$LAN_LINK" != "" ] ||  [ "$WAN_LINK" != "" ]; then
		LAN_RX_PACKET=$(ifconfig lan0 | grep "RX packets" | /bin/cut -f 2 -d ':' | tr -d ' errors')
		WAN_RX_PACKET=$(ifconfig wan0 | grep "RX packets" | /bin/cut -f 2 -d ':' | tr -d ' errors')
		if [ "$LAN_RX_PACKET" = "$LAN_RX_PACKET_PREV" ] || [ "$WAN_RX_PACKET" = "$WAN_RX_PACKET_PREV" ]; then
			ARP_RESULT_LAN=""
			ARP_RESULT_WAN=""
			if [ "$LAN_RX_PACKET" = "$LAN_RX_PACKET_PREV" ]; then
				if [ "$LAN_LINK" != "" ]; then
					LAN_IP_ARP=$(cat /proc/net/arp | grep lan0 | /bin/cut -f 1 -d ' ' |  head -n 1)
					if [ "$LAN_IP_ARP" != "" ]; then
						ARP_RESULT_LAN=$(arping -I lan0 -c 1 $LAN_IP_ARP | grep Unicast)
					fi
					if [ "$ARP_RESULT_LAN" = "" ] ; then
						LAN_IP_ARP=$(cat /proc/net/arp | grep lan0 | /bin/cut -f 1 -d ' ' |  tail -n 1)
						if [ "$LAN_IP_ARP" != "" ]; then
							ARP_RESULT_LAN=$(arping -I lan0 -c 1 $LAN_IP_ARP | grep Unicast)
						fi
					fi
				fi
			else
				ARP_RESULT_LAN="LANOK"
			fi
			if [ "$WAN_RX_PACKET" = "$WAN_RX_PACKET_PREV" ]; then
				if [ "$WAN_LINK" != "" ]; then
					WAN_IP_ARP=$(cat /proc/net/arp | grep wan0 | /bin/cut -f 1 -d ' ' |  head -n 1)
					if [ "$WAN_IP_ARP" != "" ]; then
						ARP_RESULT_WAN=$(arping -I wan0 -c 1 $WAN_IP_ARP | grep Unicast)
					fi
					if [ "$ARP_RESULT_WAN" = "" ] ; then
						WAN_IP_ARP=$(cat /proc/net/arp | grep wan0 | /bin/cut -f 1 -d ' ' |  tail -n 1)
						if [ "$WAN_IP_ARP" != "" ] ; then
							ARP_RESULT_WAN=$(arping -I wan0 -c 1 $WAN_IP_ARP | grep Unicast)
						fi
					fi
					if [ "$ARP_RESULT_WAN" = "" ]; then
						DEFAULT_ROUTE=$(ip route | grep default | /bin/cut -f 3 -d ' ')
						if [ "$DEFAULT_ROUTE" != "" ]; then
							ARP_RESULT_WAN=$(arping -I wan0 -c 1 $DEFAULT_ROUTE | grep Unicast)
						fi
					fi
				fi
			else
				ARP_RESULT_WAN="WANOK"
			fi
			if [ "$ARP_RESULT_LAN" != "" ]; then
				LAN_OK=1
			else
				if [ "$LAN_LINK" != "" ]; then
					LAN_OK=0
				else
					LAN_OK=1
				fi
			fi
			if [ "$ARP_RESULT_WAN" != "" ]; then
				WAN_OK=1
			else
				if [ "$WAN_LINK" != "" ]; then
					WAN_OK=0
				else
					WAN_OK=1
				fi
			fi
			if [ "$LAN_OK" = "0" ] && [ "$WAN_OK" = "0" ] ; then
				/etc/init.d/epnetwork restart
				echo -n "Restarted network  at: " >> /mnt/flashfs/configs/current/network_restart_times
				date >> /mnt/flashfs/configs/current/network_restart_times
			elif [ "$LAN_OK" = "0" ] ; then
				/etc/init.d/epnetwork restart_lan0
				echo -n "Restarted lan0  at: " >> /mnt/flashfs/configs/current/network_restart_times
				date >> /mnt/flashfs/configs/current/network_restart_times
			elif [ "$WAN_OK" = "0" ] ; then
				/etc/init.d/epnetwork restart_wan0
				echo -n "Restarted wan0  at: " >> /mnt/flashfs/configs/current/network_restart_times
				date >> /mnt/flashfs/configs/current/network_restart_times
			fi
			if [ "$LAN_OK" = "0" ] || [ "$WAN_OK" = "0" ] ; then
				lines_count=$(wc -l /mnt/flashfs/configs/current/network_restart_times | /bin/cut -f 1 -d ' ')
				if [ "$lines_count" -gt "10000" ]; then
					/usr/bin/tail -n +9900 </mnt/flashfs/configs/current/network_restart_times >/mnt/flashfs/configs/current/network_restart_times_temp
					/bin/mv -f /mnt/flashfs/configs/current/network_restart_times_temp /mnt/flashfs/configs/current/network_restart_times
				fi
#update MAC addresses on lan0/wan0 in case of any corruption
				(`echo export $(envtool | grep ethaddr)`>/dev/null;ifconfig lan0 hw ether $ethaddr)
				(`echo export $(envtool | grep eth1addr)`>/dev/null;ifconfig wan0 hw ether $eth1addr)
			fi
			LAN_RX_PACKET_PREV=$(ifconfig lan0 | grep "RX packets" | /bin/cut -f 2 -d ':' | tr -d ' errors')
			WAN_RX_PACKET_PREV=$(ifconfig wan0 | grep "RX packets" | /bin/cut -f 2 -d ':' | tr -d ' errors')
		else
			LAN_RX_PACKET_PREV=$LAN_RX_PACKET
			WAN_RX_PACKET_PREV=$WAN_RX_PACKET
		fi
	fi
	sleep 60

done
