#!/bin/sh -e
#
#    network-up: calculate the network transmit rate
#    Copyright (C) 2008 Canonical Ltd.
#
#    Authors: Dustin Kirkland <kirkland@canonical.com>
#
#    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, version 3 of the License.
#
#    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, see <http://www.gnu.org/licenses/>.

cache="$HOME/.screen-profiles/network-up"

interface=`route -n | tail -n 1 | sed "s/^.* //"`
unit="kB/s"

t1=`stat -c %Y "$cache"` 2>/dev/null || t1=0
t2=`date +%s`

if [ $t2 -le $t1 ]; then
	rate=0
else
	x1=`cat "$cache"` 2>/dev/null || tx1=0
	x2=`ifconfig "$interface" | grep "TX bytes" | sed "s/^.*TX bytes://" | sed "s/ .*$//"`
	echo "$x2" > "$cache"
	rate=`echo "$t1" "$t2" "$x1" "$x2" | awk '{printf "%.0f", ($4 - $3) / ($2 - $1) / 1024 }'`
	if [ "$rate" -gt 1024 ]; then
		rate=`echo "$rate" | awk '{printf "%.1f", $1/1024}'`
		unit="MB/s"
	fi
fi
printf "^\005{=b mw}$rate\005{-}\005{= mw}$unit\005{-} "
