#!/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=30
STOP=70

adsl_led() {
	local pppstatus
	local dslspeed
	# setup ppp (green) LED
	echo netdev >/sys/class/leds/ppp/trigger
	echo ppp0 >/sys/class/leds/ppp/device_name
	echo "link tx rx" >/sys/class/leds/ppp/mode
	# polling loop
	while true; do
		# when ppp is up we turn off adsl (amber) LED.) and leave ppp
		ifconfig ppp0 2>/dev/null|grep 'UP POINTOPOINT RUNNING' >/dev/null
		if [ $? -eq 0 ]; then
			echo 0 >/sys/class/leds/adsl/brightness
		else
			# otherwise we need to check the ADSL status
			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
				echo 1 >/sys/class/leds/adsl/brightness
			else
				echo 0 >/sys/class/leds/adsl/brightness
			fi
		fi
		# how often we poll
		sleep 1
	done
}

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

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

restart() {
	stop
	start
}

