#!/bin/bash

# olpc-netstatus-0.6
# Copyright (c) 2007, 2008 OLPC
# Author: 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.

source /usr/share/olpc-utils/olpc-utils-functions

function info {
echo
echo "Model     : "`get_ofw_file /model`
echo "Serial    : "`get_ofw_file /serial-number`
echo "MAC       : "`ifconfig eth0|awk '/^eth0/ {print $5}'`
echo
if [ -e /boot/olpc_build ]; then
    echo "Build     : "`cat /boot/olpc_build`
elif [ -e /boot/olpc_version ]; then
    echo "Build     : "`cat /boot/olpc_version`
fi
if builtin type -p lsb_release 1>/dev/null; then
    echo "Base OS Info:"
    lsb_release -a
    echo ''
elif [ -e /etc/issue ]; then
    echo "Base OS Info:"
    cat /etc/issue
fi

echo "EC        : "`get_ofw_file /ec-name`
echo "OFW       : "`get_ofw_file /openprom/model`
echo "Libertas  : "`ethtool -i eth0|awk '/firmware/ {print $2}'`
echo
if [ -e /home/olpc/.sugar/default/config ];then
    echo "Nick      : "`cat /home/olpc/.sugar/default/config|grep ^nick|sed 's/^[^=]*= //'`
else
    echo "Nick      : "`gconftool-2 --config-source=xml:readwrite:/home/olpc/.gconf -g /desktop/sugar/user/nick`
fi

echo "Uptime    : "`uptime|awk '{print $1}'`
}

function net {

echo
#ip
for i in `ifconfig|grep HWaddr|awk '{print $1}'`
do
	echo -n "IP $i   : "
	echo `ifconfig $i|grep "inet addr"|awk 'BEGIN{FS="addr:"}{print $2}'|awk '{print $1}'` 
done

#nameserver
dns=''
dns=`cat /etc/resolv.conf|grep "^name"|awk 'NR==1 {print $2}'`
echo 'DNS       : '$dns
echo ''


#telepathy
sugar-telepathies|sed -e 's/0//' -e 's/1/(connecting)/' -e 's/2/(disconnected)/'|
sed 's/^.*$/            &/'|sed '1 s/            /Telepathy : /'

#jabber
jabber=''
jabber=`netstat -tpT --numeric-ports 2> /dev/null|grep telepathy|grep ESTABLISHED|awk '{print $5}'|awk 'BEGIN{FS=":522"}{print $1}'`
echo 'Jabber    : '$jabber

#xos
echo "XOs       : "`sugar-xos|wc -l`
echo

#essid
essid=''
essid=`iwconfig eth0|grep ESSID|awk 'BEGIN{FS="ESSID:\""}{print $2}'|awk 'BEGIN{FS="\""}{print $1}'`
echo 'Essid     : '$essid


#channel
ch=''
freq=`iwconfig eth0|grep Freq|grep -v eth0|awk 'BEGIN{FS="uency:"}{print $2}'|awk '{print $1}'`
[ "$freq" = 2.412 ] && ch=1
[ "$freq" = 2.437 ] && ch=6
[ "$freq" = 2.462 ] && ch=11

[ "$freq" = 2.417 ] && ch=2
[ "$freq" = 2.422 ] && ch=3
[ "$freq" = 2.427 ] && ch=4
[ "$freq" = 2.432 ] && ch=5
[ "$freq" = 2.442 ] && ch=7
[ "$freq" = 2.447 ] && ch=8
[ "$freq" = 2.452 ] && ch=9
[ "$freq" = 2.457 ] && ch=10
[ "$freq" = 2.467 ] && ch=12

echo 'Channel   : '$ch
echo ''

#ethernet
ethernet=''
for i in `ifconfig|grep HWaddr|awk '{print $1}'`
do
	iwconfig $i 2>&1|grep "no wireless" > /dev/null && ethernet="$i $ethernet"
done

#school
school=""
school=`ping -c 1 schoolserver 2>&1|sed -n 's/^.*bytes from \([^ ]*\) .*$/\1/p'`
echo "School    : $school"

#config
eth=''
msh=''
ipeth=''
ipmsh=''

for i in `ifconfig|grep HWaddr|grep ^eth|awk '{print $1}'`
do
	ifconfig $i|grep "inet addr" > /dev/null && eth=$i
done

for i in `ifconfig|grep HWaddr|grep ^msh|awk '{print $1}'`
do
	ifconfig $i 2>&1|grep "inet addr" > /dev/null && msh=$i
done

[ -n "$eth" ] && ipeth=`ifconfig $eth|sed -n '/t addr/s/^[^:]*:\([^ ]*\) .*$/\1/p'`
[ -n "$msh" ] && ipmsh=`ifconfig $msh|sed -n '/t addr/s/^[^:]*:\([^ ]*\) .*$/\1/p'`

config=''
if [ -n "$ipeth" ]
then 
	echo $ethernet|grep $eth > /dev/null && config="Ethernet"
	echo $ethernet|grep $eth > /dev/null || config="Simple Wifi"
	[ -n "$school" ] && config=`echo "$config"|sed s/Simple/School/`	
elif [ -z "$dns" ]
then
	config="Simple Mesh"
elif [ "${ipmsh:0:3}" = "169" ]
then
	config='MPP'
else config='School Mesh'
fi	

#acting as MPP
asmpp='' 
[ -n "$ipmsh" ] && cat /etc/dhcpd.conf 2>&1|grep "$ipmsh" > /dev/null && asmpp=' - Acting as MPP'



echo "Config    : $config$asmpp"
[ -n "$ethernet" ] && echo "Ethernet  : $ethernet"
echo ''
}

function help {

echo "Usage: olpc-netstatus [-i|-n]"
echo
echo "  -i     device info only"
echo "  -n     network info only"
echo
}

if [ "$1" = "-i" ];then info;echo
elif [ "$1" = "-n" ];then net
elif [ -n "$1" ];then help;
else info;net
fi
