#!/bin/bash

# olpc-connections v0.3
# Copyright (C) 2008 Giannis Galanis <echo gxlxnis@lxptop.org | sed s/x/a/g>

# 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.

init () {
#btime=`awk '/btime/ {print $2}' /proc/stat`
#echo " "`date -d "Jan 1 1970 00:00 UTC + $btime seconds" +%H:%M:%S`" : boot"
#echo " "`date +%H:%M:%S`" : script started"

echo
space="                    "
echo -n "            "
NETDEVICES=`ls /sys/class/net | grep -v '^lo$'`
for ND in "$NETDEVICES"; do
    let sp=15-${#ND};let sp1=$sp/2;let sp2=$sp-$sp1;
    echo -n "${space:0:$sp1}$ND${space:0:$sp2} | "
done;
echo "      dns       |salut|gabbl|     jabber      | xos"
}

stats () {
dns=`awk '/^name/ {print $2}' /etc/resolv.conf|awk 'NR==1'`
salut=`sugar-telepathies|awk '/salut/ {print $2}'`
gabble=`sugar-telepathies|awk '/gabble/ {print $2}'`
jabber=`netstat -ntpT 2> /dev/null|grep telepathy|grep ESTAB|awk '{print $5}'|sed s/:.*$//`
xos=`sugar-xos|wc -l`
[ "$salut" == "0" ] && salut=" on  "
[ "$salut" == "1" ] && salut=" ... "
[ "$salut" == "2" ] && salut=" off "
[ "$salut" == "" ] && salut="     "
[ "$gabble" == "0" ] && gabble=" on  "
[ "$gabble" == "1" ] && gabble=" ... "
[ "$gabble" == "2" ] && gabble=" off "
[ "$gabble" == "" ] && gabble="     "

for ND in "$NETDEVICES"; do
    ipaddr=`ifconfig $ND|grep "inet addr"|sed s/^.*r://|awk '{print $1}'`
    let sp=15-${#ipaddr};let sp1=$sp/2;let sp2=$sp-$sp1;echo -n "${space:0:$sp1}$ipaddr${space:0:$sp2} | "
done
let sp=15-${#dns};let sp1=$sp/2;let sp2=$sp-$sp1;echo -n "${space:0:$sp1}$dns${space:0:$sp2} |"
echo -n "$salut|"
echo -n "$gabble| "
let sp=15-${#jabber};let sp1=$sp/2;let sp2=$sp-$sp1;echo -n "${space:0:$sp1}$jabber${space:0:$sp2} | "
echo -e "$xos"
}

log () {
stats1=`stats`
echo -n " "`date +%H:%M:%S`
echo " | $stats1"

while true
do
	sleep 1
	stats2=`stats`
	[ ! "$stats2" = "$stats1" ] && echo -n " "`date +%H:%M:%S` && echo " | $stats2"
	stats1="$stats2"
done 
}

init
log
