#!/bin/sh /etc/rc.common
# Copyright (C) 2010  Glen Pitt-Pladdy
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#
# See: http://www.pitt-pladdy.com/blog/_20100123-184109_0000_OpenWrt_with_native_IPv6_on_DG834_v2_using_AAISP_/


START=50
STOP=50

connect_monitor() {
	local dslstatus
	dslstatus=1
	local dslspeed
	# polling loop
	while true; do
		# check ADSL staus
		dslspeed=`grep 'Connection Rate:' /proc/avalanche/avsar_modem_stats`
		if [ `echo "$dslspeed" | cut -s -f 3` -gt 0 \
			-a `echo "$dslspeed" | cut -s -f 5` -gt 0 ]; then
			# ADSL up
			if [ $dslstatus -eq 0 ]; then
				# it was down
				touch /tmp/connect-monitor-dsl_up
				dslstatus=1
			fi
		else
			# ADSL down
			dslstatus=0
		fi
		# how often we poll
		sleep 5
	done
}

start() {
	if [ -f /var/run/local-connect-monitor.pid ]; then
		echo 'Not starting - local-connect-monitor.pid exists. Already running?'
	else
		connect_monitor &
		pid=$!
		echo $pid >>/var/run/local-connect-monitor.pid
	fi
}

stop() {
	kill `cat /var/run/local-connect-monitor.pid`
	rm /var/run/local-connect-monitor.pid
}

restart() {
	stop
	start
}

