#!/bin/bash
# AUTHOR: Vovchik
# PURPOSE: GUI to show IP config info
# DATE: 14 May 2009
#120201 BK: internationalized.
#120323 replace 'xmessage' with 'yaf-splash'.
#130216 mavrothal: < > mess up gtkdialog.

export TEXTDOMAIN=ipinfo
export OUTPUT_CHARSET=UTF-8

# --------------
# functions
# --------------

# --------------
get_data()
# --------------
{
	nl='
'
	# external ip
	var0="`wget -O - -q icanhazip.com`"
	var0="$(gettext 'External IP:') ""$var0"
	# tab 1 - interfaces
	var01=`echo $(gettext 'Hostname:') "$HOSTNAME"`
	var02="`ifconfig | tr '>' ')' | tr '<' '('`" #130216 mavrothal: < > mess up gtkdialog.
	# tab 3 - routing
	var03="`route -n | sed -e 's%Kernel IP routing table%%'`"
	# tab 2
	var04="DNS:"
	var05=`cat /etc/resolv.conf`
	# tab 4 - collect wireless data
	wifi="`iwgetid -a | gawk '{print $1}'`"
	# echo "mywifi: ""$wifi"
	spaces="     "
	if [ -n "$wifi" ]; then 
		var06=`iwgetid -a 2>&1`
		var07=`iwgetid -c 2>&1`
		var08=`iwgetid -f 2>&1`
		var09=`iwgetid -m 2>&1`
		var10=`iwlist encryption 2>&1 | grep -v "no encryption"`
		var11=`iwlist rate 2>&1 | grep Current`
		# concatenate wireless data for display
		var12="$var06""$nl""$var07""$nl""$var08""$nl""$var09""$nl""$var10""$nl""$var11""$nl"
		var12="`echo "$var12" | tr '>' ')' | tr '<' '(' | sed '/^$/d'`" #130216 mavrothal: < > mess up gtkdialog.
	else
		var12="<b>$(gettext 'No wireless extensions detected.')</b>"
	fi
	# tab 5 - collect connection data
	var13="`netstat -tan`"
}	
# --------------
show_data()
# --------------
{
	echo "$var01"
	echo
	echo "$var02"
	echo
	echo "$var03"
	echo
	echo "$var04"
	echo "$var05"
	echo
	echo "$var12"
	echo "$var13"
}

# --------------
make_gui()
# --------------
{
export show_ip_info="
<window title=\"$(gettext 'IP Info')\" icon-name=\"gtk-about\">
	<vbox>
		<frame $(gettext 'Network Configuration')>
			<notebook labels=\"$(gettext 'Interfaces')|$(gettext 'DNS')|$(gettext 'Routing')|$(gettext 'Wireless')|$(gettext 'Statistics')\">
				<vbox>
					<text use-markup=\"true\" wrap=\"false\">
						<label>\"<tt><b>${nl}${var01}</b>${nl}${var0}${nl}${nl}${var02}</tt>\"</label>
					</text>
				</vbox>
				<vbox>
					<text use-markup=\"true\" wrap=\"false\">
						<label>\"<tt><b>${nl}${var04}${nl}</b>${nl}${var05}</tt>\"</label>
					</text>
				</vbox>
				<vbox>
					<text use-markup=\"true\" wrap=\"false\">
						<label>\"<tt>${nl}<b>$(gettext 'Kernel IP routing table')</b>${nl}${var03}${nl}${nl}</tt>\"</label>
					</text>
				</vbox>
				<vbox>
					<text use-markup=\"true\" wrap=\"false\">
						<label>\"<tt><b>${nl}$(gettext 'Wireless configuration:')${nl}</b>${nl}${var12}</tt>\"</label>
					</text>
				</vbox>
				<vbox>
<text use-markup=\"true\" wrap=\"false\"><label>\"<tt>${nl}<b>$(gettext 'Network statistics:')</b>${nl}${nl}${var13}</tt>\"</label></text>
				</vbox>
			</notebook>
		</frame>
		<hbox>
			<button ok></button>
		</hbox>
	</vbox>
</window>
"
}

#  
# --------------
display_gui()
# --------------
{
	gtkdialog3 --program=show_ip_info --center 
}

# --------------
# end functions
# --------------

# --------------
# main
# --------------

#xmessage -buttons "" -center -bg orange "$(gettext 'Please wait, probing...')" &
yaf-splash -close never -bg orange -text "$(gettext 'Please wait, probing...')" &
XPID=$!
get_data
show_data
make_gui
kill $XPID
display_gui
exit 0

# --------------
# end main
# --------------
