#!/bin/bash

currCDstate=0
currTRstate=0
/usr/local/bin/LED.py CD off
/usr/local/bin/LED.py TR off

while true; do

	if [ `netstat -pan |egrep -e 'ESTAB.*tcpser' | wc -l` -gt 0 ]; then
		if [ $currCDstate -eq 0 ]; then
			/usr/local/bin/LED.py CD on
			currCDstate=1
		fi
	else
		if [ $currCDstate -eq 1 ]; then
			/usr/local/bin/LED.py CD off
			currCDstate=0
		fi
	fi

	if fuser -s /dev/ttyAMA0; then
		if [ $currTRstate -eq 0 ]; then
			/usr/local/bin/LED.py TR on
			currTRstate=1
		fi
	else
		if [ $currTRstate -eq 1 ]; then
			/usr/local/bin/LED.py TR off
			currTRstate=0
		fi
	fi

	sleep 2
done

