#!/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.
#131126 zigbert: gui (gtkdialog) improvements.

export TEXTDOMAIN=ipinfo
export OUTPUT_CHARSET=UTF-8

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

[ -f $HOME/.ipinfo ] && . $HOME/.ipinfo || CB0=false

#---------------
get_ip()
#---------------
{
	# external ip
	if [ "$CB0" = "true" ];then
	 var0="`wget -O - -q icanhazip.com`"
	else
	 var0="autocheck disabled"
	fi
	var0="$(gettext '<b>External IP</b> (via icanhazip.com):') ""<b>$var0</b>"
}
export -f get_ip
#---------------
info_func()
#---------------
{
	# external ip info
	$DEFAULTBROWSER https://major.io/icanhazip-com-faq/ &
}
export -f info_func
# --------------
get_data()
# --------------
{
	nl='
'

	# 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()
# --------------
{
XML0='<vbox scrollable="true" shadow-type="0" border-width="15">
       <hbox space-expand="true" space-fill="true">
        <checkbox>
         <label>'$(gettext 'Query icanhazip.com for your external IP ')'</label>
         <variable>CB0</variable>
         <default>'$CB0'</default>
        </checkbox>
        <button tooltip-text="'$(gettext 'Get information about icanhazip.com')'" space-expand="true" space-fill="false">
         <input file stock="gtk-info"></input>
         <action>info_func</action>
        </button>
       </hbox>
      <hbox>
         <text use-markup="true" wrap="false" space-expand="false" space-fill="false">
           <label>"<tt>'

XML1='<vbox scrollable="true" shadow-type="0" border-width="15">
       <hbox>
         <text use-markup="true" wrap="false" space-expand="false" space-fill="false">
           <label>"<tt>'
XML2='    </tt>"</label>
         </text>
         <text space-expand="true" space-fill="true"><label>""</label></text>
       </hbox>
       <text space-expand="true" space-fill="true"><label>""</label></text>
     </vbox>'

export IP_Info='
<window title="IP Info - '$(gettext 'Network information')'" icon-name="gtk-network" default_height="450" default_width="600">
<vbox space-expand="true" space-fill="true">
  <notebook labels="'$(gettext 'Interfaces')'|'$(gettext 'DNS')'|'$(gettext 'Routing')'|'$(gettext 'Wireless')'|'$(gettext 'Statistics')'" space-expand="true" space-fill="true">
    '$XML0'<b>'${var01}'</b>'${nl}${var0}${nl}${nl}${var02}''$XML2'
    '$XML1'<b>'${var04}${nl}'</b>'${nl}${var05}''$XML2'
    '$XML1'<b>'$(gettext 'Kernel IP routing table')'</b>'${nl}${var03}${nl}${nl}''$XML2'
    '$XML1'<b>'$(gettext 'Wireless configuration:')${nl}'</b>'${nl}${var12}''$XML2'
    '$XML1'<b>'$(gettext 'Network statistics:')'</b>'${nl}${nl}${var13}''$XML2'
  </notebook>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
  </hbox>
</vbox>
</window>'
}

#  
# --------------
display_gui()
# --------------
{
	eval `gtkdialog -p IP_Info`
}

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

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

#xmessage -buttons "" -center -bg orange "$(gettext 'Please wait, probing...')" &
/usr/lib/gtkdialog/box_splash -close never -text "$(gettext 'Please wait, probing...')" &
XPID=$!
get_ip
get_data
show_data
make_gui
kill $XPID
display_gui
echo "CB0=$CB0" > $HOME/.ipinfo
exit 0

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