#!/bin/bash

#nick
i=$(cat /home/olpc/.sugar/default/config|grep nick)
nick=${i:11}

#nameserver
dns=''
while read line
do
	i=$(echo $line | grep nameserver)
	if [ ${#i} -ne 0 ]; then dns=${line:11};fi
done < <(cat /etc/resolv.conf)

#telepathy
tele=''
for i in $(ps U olpc -f|grep telepathy)
do
	if [ ${i:0:1} = '/' ]
	then
		tele=${i:19}
	fi
done

#jabber
jabber=''
for i in $(netstat -tpT|grep telepathy)
do
	j=$(echo $i|grep xmpp)
	if [ ${#j} -ne 0 ]
	then
		jabber=${j:0:$((${#j}-12))}
	fi
done

#mac
mac=''
mac=$(ifconfig|grep msh0|awk '{print $5}'|tr '[A-F]' '[a-f]')

#ip
ipeth=''
ipmsh=''
for i in $(ifconfig eth0|grep 'inet addr')
do
	j=$(echo $i|grep addr)
	if [ ${#j} -ne 0 ]
	then
		ipeth=${j:5}
	fi
done

for i in $(ifconfig msh0|grep 'inet addr')
do
	j=$(echo $i|grep addr)
	if [ ${#j} -ne 0 ]
	then
		ipmsh=${j:5}
	fi
done

#essid
essid=''
i=$(iwconfig eth0|grep ESSID)
essid=${i:33:$((${#i}-59))}

#channel
ch=''
for i in $(iwconfig eth0)
do
	if [ ${i:0:5} = Frequ ];then j=${i:10};fi
	if [ "$j" = 2.412 ];then ch=1;fi
	if [ "$j" = 2.437 ];then ch=6;fi
	if [ "$j" = 2.462 ];then ch=11;fi
done

#config
config=''
if [ ${#dns} -eq 0 ]
then 
	config='Local-link'
elif [ "${dns:0:3}" = "169" ]
then
	config='MPP'
elif [ ${#ipeth} -eq 0 ]
then
	config='School server'
else	config='Access point'
fi
echo ''
echo 'nick      : '$nick
echo 'mac       : '$mac
echo ''
echo 'ip eth0   : '$ipeth
echo 'ip msh0   : '$ipmsh
echo 'dns       : '$dns
echo ''
echo 'essid     : '$essid
echo 'channel   : '$ch
echo ''
echo 'telepathy : '$tele
echo 'jabber    : '$jabber
echo ''
echo 'config    : '$config
echo ''
