#!/bin/bash
PATH=$PATH:/usr/local/lib/inx
. inx_essentials
maybe_switch

f-net ()
{

export THEME

connect ()
{
clear
bold
fig-col
figlet "  dialup   &   dsl"
echo
head-col
echo "  You will only need these settings if INX did not connect you to the Internet."
info-col
echo
echo "  1: Set up a dial-up connection                          (sudo pppconfig)"
echo "  2: Set up a direct PPPoE DSL connection                 (sudo pppoeconf)"
echo
fig-col
echo "  You can use the \"c\" and \"d\" options below to connect/disconnect after setting up."
echo
head-col
echo "  c: Connect to dialup/ dsl"
echo "  d: Disconnect dialup/ dsl"
echo
fig-col
echo "  w: Net and Web menu"
echo "  m: INX main menu"
echo "  x: Give me a command prompt"

f-ttynum

read -s -n 1 CONNECT
	case $CONNECT in
		1)
		clear
		head-col
		figlet "  dial up"
		echo
		fig-col
		echo "  This option configures your dialup connection."
		echo "  You will need the usual information from your ISP."
		info-col
		printf "\n\n"
		echo "  * Dial-up phone number."
		echo "  * Password and user name"
		echo "  * DNS nameserver numbers, if your ISP does not automatically set them."
		printf "\n\n"
		fig-col
		echo "  This set-up method uses the pppconfig utility ( sudo pppconfig)"
		echo
		head-col
		echo "  1: Go ahead and use the dial-up settings utility"
		echo "  2: Help with using the utility"
		echo
		fig-col
		echo
		echo "  w: Net and Web menu."
		echo "  m: INX main menu"
		echo "  x: Give me a command prompt."
		f-ttynum
		read -s -n 1 DIALUP
			case $DIALUP in
				1)
				echo
				echo "  Starting sudo pppconfig - please wait"
                                sleep 1
				sudo pppconfig
				connect
				;;
				2)
				dialconfig ()
				{
				clear
				fig-col
				echo "  Using the pppconfig utility."
				info-col
				echo
				echo "  The interface to \"pppconfig\" uses a \"false window box\" "
				echo
				echo "  You can navigate with the \"arrow\" keys, and use <tab> "
				echo "  to select \"buttons\"."
				echo
				echo "  \"Dynamic\" DNS means your ISP sets \"nameservers\" automatically."
				echo
				echo "  Unless you need more than one provider, leave the \"provider\" field as \"provider\"."
				echo
				echo "  You can then dial up with the command \"pon\" ( without quotes)."
				echo 
				echo "  To disconnect, type \"poff\"."
				echo
				echo "  Or use the menu entries provided by INX. ( c and d )"
				echo
				fig-col
				echo "  If you can't connect:"
				head-col
				echo
				echo "  * You might have an incompatible modem (\"Winmodem\") - many internal modems are like this."
				echo "  - see if you can find the device's type with lspci -v | grep -i modem"
				echo "  - send a polite note to the manufacturers asking them to provide Linux support"
				echo
				fig-col
				echo "  If pppconfig can't find your modem:"
				echo
				head-col
				echo "  - serial devices on linux are /dev/ttyS0, /dev/ttyS1 etc. Try these instead of /dev/modem ."
				echo
				fig-col
				echo "  Things that Won't Work Well (tm) on dial-up."
				head-col
				echo
				echo "  Most streams. You will need to search for low bandwith streams. (32 kbps or lower)"
				fig-col
				echo
				echo "  Go ahead with setup? (y/n)"
					f-ttynum
					read -s -n 1 SETUP
						case $SETUP in
							y)
							echo
							echo "  Starting sudo pppconfig - please wait."
							sleep 2
							sudo pppconfig
							connect
							;;
							n)
							connect
							;;
							*)
							echo
							echo "  Please type y for yes, n for no."
							sleep 2
							;;
						esac
							dialconfig
				}
				dialconfig
			esac	
		;;		
		2)
		clear
		bold
		head-col
		figlet "  direct    dsl"
		echo
		fig-col
		echo "  This option configures your DSL connection."
		echo "  It is only needed if INX did not connect to the Internet."
		echo
		info-col
		echo "  The usual reason for using this option is that your ISP provides"
		echo "  a direct modem connecting to its \"access concentrator\"."
		echo
		echo "  You should not need it if you have a router or modem/router."
		echo "  You will need it if you use a direct \"bridged\" connection."
		echo
		fig-col
		echo "  This uses the \"sudo pppoeconf\" utility"
		head-col
		echo
		echo "  1: Go ahead and run the utility to set up DSL."
		echo "  2: Help with the utility."
		echo
		fig-col
		echo "  w: Net and Web menu."
		echo "  m: INX main menu."
		echo "  x: Give me a command prompt."
		f-ttynum
		read -s -n 1 PPPOE
		case $PPPOE in
			1)
			f-sup
			sudo pppoeconf
			connect
			;;
			2)
			pppoehelp ()
			{
			clear
			 
			fig-col
			echo "  Using the pppoeconf utility."
			info-col
			echo
			echo "  This utility uses a \"false window box\" interface."
			echo "  Use arrow keys and <tab> to navigate, <enter> to apply."
			echo
			echo "  You will need your ISP provided details:"
			echo
			echo "  * user name"
			echo "  * password"
			echo
			fig-col
			echo "  \"pppoeconf\" tries to connect to your ISP's \"access concentrator\"."
			echo "  to create a working DSL connection."
			echo
			echo "  Usually it is just a matter of allowing it to scan, and answering questions."
			echo "  Most people will have only one interface - usually eth0."
			head-col
			echo
			echo "  Go ahead and use the setup utility? (y/n)"
			f-ttynum
			read -s -n 1 PPPOEHELP
				case $PPPOEHELP in
					y)
					echo "  Starting sudo pppoeconf - please wait."
					sleep 2
					sudo pppoeconf
					connect
					;;
					n)
					connect
					;;
					*)
					echo
					echo "  Please type y for yes, n for no."
					sleep 2
					;;
				esac
					pppoehelp
			}
			pppoehelp			
			;;
			w)
			f-net
			;;
			m)
			exec menu
			;;
			x)
			clear
			f-prompt
			;;
			*)
			connect
			;;
		esac
		;;
		c)
		head-col
		echo
		route | grep default > /dev/null 2>&1
		if [ $? = 0 ] ; then
		echo
		echo "  You seem to be connected already..."
		echo
		route | grep default 
		sleep 6
		connect
		fi
		echo "  Connecting - please wait..."
		sleep 2
		pon
		until route | grep default  > /dev/null 2>&1
		do
		sleep 1
		done
		echo
		echo "  Connected: $(ifconfig ppp0 | grep inet | cut -d 't' -f 2 | cut -d 'P' -f 1)"
		sleep 6
		connect
		;;
		d)
		echo
		poff
		head-col
		echo
		echo "  Disconnecting ppp ..."
		sleep 4
		connect
		;;
		w)
		f-net
		;;
		m)
		exec menu
		;;
		x)
		clear
		f-prompt
		;;
		*)
		echo "  I didn't understand that request - please choose again."
		sleep 2
		connect
		;;
	esac
		
		

f-net
}

browsers ()
{
clear
bold
fig-col
figlet "  Browsers"
echo
head-col
echo "  Choose by number or letter."
info-col
echo
echo
echo "  1: Introduction to INX browsers."
echo "  2: Xlinks2 ( links2 -g) - graphical browser."
echo "  3: Elinks - command line tabbed browser"
echo
echo
head-col
echo "  w: Back to the INX Web and Net menu."
echo "  m: Back to the INX main menu."
echo "  x: Give me a command prompt."
f-ttynum
read -s -n 1 BROWSER
	case $BROWSER in
		1)
		clear
		fig-col
		echo -e "\n\n  Basic Start for the Browsers"
		echo
		head-col
		echo "  xlinks2 is links2 called as \"links2 -g\""
		info-col
		echo
		echo "  With luck, you will be able to use your mouse with it :)"
		echo "  For a menu, hit the <esc> key or click top left."
		echo "  q - quit"
		echo
		head-col
		echo "  elinks is a pure command line browser - very quick."
		info-col
		echo
		echo "  <esc> for a menu, arrow down for the next link, arrow left to go back a page."
		echo "  To edit an input field, hit <enter> before editing."
		echo "  t - new tab"
		echo "  c - close a tab"
		echo "  q - quit"
		echo "  g - type a URL location (e.g. google.com )"
		echo
		head-col
		echo "  See also the Hints and Tips in the Web and Net menu."
		echo
		fig-col
		echo "  Hit any key to return to the browser menu."
		f-ttynum
		read -s -n 1
		browsers
		;;
		2)
		f-sup
		links2-watch  http://google.com
		browsers
		;;
		3)
		f-sup
		elinks http://google.com
		browsers
		;;
		m)
		exec menu
		;;
		w)
		f-net
		;;
		x)
		clear
		f-prompt
		;;
		*)
		echo "  I didn't understand that request - please try again."
		sleep 2
		browsers
		;;
	esac

}

irc ()
{
clear
bold
fig-col
figlet "  inx    irc"
echo
head-col
echo "  Choose by number or letter."
info-col
printf "\n\n\n"
echo "  1: Introduction to the irssi IRC client"
echo "  2: Connect to irc.freenode.net (Channel #inx)"
echo "  3: Plain irssi - your choices"
printf "\n\n\n"
head-col
echo "  w: Back to the INX Net and Web menu."
echo "  m: Back to the main INX menu."
echo "  x: Give me a command prompt."
echo
f-ttynum
read -s -n 1 IRC
	case $IRC in
		1)
		irchelp ()
		{
		clear
		fig-col
		echo "  This is a very basic introduction to using irssi for Internet Relay Chat."
		echo
		info-col
		cat << EOIRSSI
  Irssi is a very powerful IRC client, but going into detail is beyond the scope of this help.

  For simply chatting on irc.freenode.net, you can choose that entry from the menu.

  Commands that you will need to know for basic chatting:
EOIRSSI
head-col
echo
echo "  /join #name-of-channel"
echo
echo "  /part - to leave a channel"
echo
echo "  /quit - to leave the network and close irssi"
echo
echo "  ctrl + n - next window"
echo "  ctrl + p - previous window"
echo
info-col
echo "  You can find out more at http://irssi.org"
echo
fig-col
echo "  IRC has its own \"culture\". You will want to observe etiquette."
info-col
echo
echo "  * Be patient - the channels are run by volunteers."
echo "  * Be on topic - make a habit of typing"
head-col
echo
echo "  /topic"
echo
info-col
echo "  when you enter a channel, to see what is relevant in that channel."
echo
echo "  It is worth reading the IRC guidelines at http://wiki.ubuntu.com/IrcGuidelines"
echo
echo "  Please don't ask questions about INX in #ubuntu!"
head-col
echo
echo "  1: Show me the IrcGuidelines page."
echo "  2: Show me the irssi home page."
echo 
fig-col
echo "  Hit any other key to return to the IRC menu."
f-ttynum
read -s -n 1 IRCHELP
	case $IRCHELP in
		1)
		f-sup
		links2-watch http://wiki.ubuntu.com/IrcGuidelines
		irchelp
		;;
		2)
		f-sup
		links2-watch  http://irssi.org
		irchelp
		;;
		*)
		irc
		;;
	esac
	}
		irchelp
		;;
		2)
		begin-irssi # Info for the IRC-challenged... only once...
		f-sup
		irssi -c irc.freenode.net -n inx-live
		irc
		;;
		3)
		irssi
		irc
		;;
		w)
		f-net
		;;
		m)
		exec menu
		;;
		x)
		clear
		f-prompt
		;;
		*)
		echo "  I didn't understand that request - please try again"
		sleep 2
		irc
		;;
	esac
	
}

netradio ()
{
clear
bold
fig-col
figlet "  radio    choices"
head-col
echo
echo "  Choose by number or letter."
info-col
echo
echo
echo "  1: Choose from a selection of streams."
echo "  2: Enter search terms for Shoutcast Streams."
echo "  3: Tips on using the Elinks browser with streams."
echo
echo
head-col
echo "  w: Net and Web menu"
echo "  m: Main INX menu"
echo "  x: Give me a command prompt."
f-ttynum
read -s -n 1 STREAM
	case $STREAM in 
		1)
		exec radio
		;;
		2)
		exec random
		;;
		3)
		clear
		
		fig-col
		echo "  Elinks with Shoutcast and Magnatunes."
		echo
		info-col
		cat << EOCPLAY
  The "inx player" music player can handle Shoutcast or Magnatunes playlists.
  Elinks is configured to recognise and play media from most URLs. It can also
  play .wma, .asx, .ram etc., provided that the URLs do not use flash or javascript...
		
  The "Random Radio" menu or "inx Radio" menus use mplayer, while cplay uses
  mpg321 or ogg123. M3u and Shoutcast streams can be played from elinks with 
  the "moc" player ( Music on Console - command "mocp") 

EOCPLAY
fig-col
echo "  Tips for playing streams directly from Elinks."
		info-col

cat << EOELINKS

  On the magnatunes.com pages, you get a choice of using their flash-based
  player, or playing as m3u. Scroll down through the links to the "m3u" link to
  use "mocp" for Magnatunes. A quick way to navigate the magnatunes.com site
  is to use the elinks search function on each page - this will highlight the search
  terms. On the page that has the actual m3u link, doing this will find it:

  /m3u 

  On the shoutcast.com site, the links to look for are the [IMG] links to the left
  of the entry you want to play. To search, select the search field, hit <enter>
  and *then* edit the field / type your search terms.

  I recommend using elinks rather than xlinks2/links2 -g for these purposes. It is not
  as pretty, but it works! You will be prompted for what you want to do with files -
  it should be fine to just accept "mocp", or "playinx", which means just hitting
  <enter>.


EOELINKS
		fig-col
		echo "  1: Take me to Magnatunes.com with the Elinks browser."
		echo "  2: Take me to Shoutcast.com with the Elinks browser."
		echo
		head-col
		echo "  Any other key to return to the Internet Radio menu."
			f-ttynum
			read -s -n 1 ELINKSC
				case $ELINKSC in
					1)
					f-sup
					elinks magnatunes.com
					netradio
					;;
					2)
					f-sup
					elinks classic.shoutcast.com
					netradio
					;;
					*)
					netradio
					;;
				esac
		;;
		w)
		f-net
		;;
		m)
		exec menu
		;;
		x)
		clear
		f-prompt
		;;
		*)
		netradio
		;;
	esac
		
}

monitors ()
{
bold
clear
fig-col
figlet "  Net    Monitors"
printf "\n\n\n"
info-col
echo "  1: iftop monitor"
echo "  2: iptraf monitor"
echo "  3: monitors in GNU Screen."
echo
echo
fig-col
echo "  Hit any key to return to the Net and Web menu."
f-ttynum
read -s -n 1 MONITOR
	case $MONITOR in
		1)
		sudo iftop -BP
		monitors
		;;
		2)
		sudo iptraf
		monitors
		;;
		3)
		f-sup
		screen -S monitor -c $HOME/.screenrc-monitor
		exec screeninx
		;;
		*)
		f-net
		;;
	esac

}

net-tips ()
{
white ; clear

bold ; fig-col
echo 
echo "  A few quick tips for using INX with the network..."
echo
head-col
echo "  Elinks Browser"
white ; unbold
cat << EOTIPS
  In the elinks browser, remember these keystrokes:
  * g                            : "Go" to an entered URL
  * t                            : Open a new tab at the entered URL
  * SHIFT+t                      : Open a selected link in a new tab
  * < >                          : Move from tab to tab
  * ESC                          : Access or dismiss menus
  * [ ]                          : Scroll horizontally
  * CTRL+n                       : Scroll down line by line
  * CTRL+p                       : Scroll up line by line
  * ARROWS up,down               : Select links
  * ARROW left                   : Go back to previous URL
  * ARROW right, or ENTER        : Go to selected URL  
  * SPACE,Page Up/Down           : Up and down a page
  * /                            : Search
  There are others, but those will get you going...

EOTIPS
bold ; head-col
echo "  IRC Channels"
white ; unbold
cat << EOIRC
  Come and say hello to us on Freenode in an obscurely named channel called:
    #inx             ( _The_ place to be seen on the net. ;-) )
  Some others you might want to lurk in might be:
    ##linux
    #ubuntu-offtopic ( you won't be thanked if you ask about INX in #ubuntu ;-))
    #debian          ( don't even *think* about asking INX questions here! )
  In each case type /join #name-of-channel to join

  * There are *lots* of other channels - just be aware of the netiquette points mentioned
    in the IRC section of the Net and Web menu
  * Registering a nick is a good move - type "/msg nickserv help" to get some pointers
  * Just watching and reading can be educational - get a feel for it before you join in
EOIRC
head-col ; echo
echo "  Mutt"
white ; unbold
echo "  Do read the Mutt Mail introduction - it is worth at least trying it out."
echo
fig-col
echo "  Any key to return to the Net And Web menu."
f-escape
f-net
}

wifi ()
{
    clear ; bold ; fig-col
    figlet "  wireless "
    echo
    head-col
    echo "  This tool should enable you to connect using wireless..."
    info-col
cat << EOWIFI

  The "Ceni" connection tool gives you a choice of  recognised 
  networks. To configure your wireless interface choose your 
  interface from the list (e.g. wlan0) then the network name 
  (essid) from the networks  that Ceni shows you.
  
  If Ceni cannot find your wireless interface, it may be that,
  for example, the firmware for the device is not freely 
  available, and thus cannot be included in INX.

  You will need your "key" if the network uses WEP, WPA, etc.
  
  In general, choosing the "DHCP" option will be what you
  want to try first, unless you know otherwise.
  
EOWIFI

head-col
echo "  Use <tab> to navigate in Ceni."
  
fig-col
echo
echo "  1: Go ahead and use Ceni to connect wireless"
echo "  2: I'm on line (wired). Show me some wireless help!"
echo 
head-col
echo "  Any other key to return to the Net & Web menu."
echo ; unbold

f-ttynum
read -s -n 1 WIFI
case $WIFI in
    1)
    sudo ceni
    wifi
    ;;
    2)
    if [ $TERM = linux ] ; then
        links2-watch https://help.ubuntu.com/community/WifiDocs/WiFiHowTo
    else
        elinks  https://help.ubuntu.com/community/WifiDocs/WiFiHowTo
    fi
    wifi
    ;;
    *)
    f-net
    ;;
esac

}

clear
bold
fig-col
figlet "  inx    Net   "\&"   Web"
echo
head-col
echo "  Choose by number or letter."
echo
info-col
echo "  1: Web Browsers"
echo "  2: Email"
echo "  3: Internet Radio"
echo "  4: Internet Relay Chat (IRC)"
echo "  5: Net Monitors"
echo "  6: Hints and Tips"
echo "  7: Dial-up & DSL"
echo "  8: Secure Network File System (sshfs)"
echo "  9: Wireless"
echo
head-col
echo "  m: Back to the main INX Menu."
echo "  f: Flip colours."
echo "  x: Give me a command prompt."


echo
f-ttynum
read -s -n 1 NETSTUFF
	case $NETSTUFF in
		1)
		browsers
		;;
		2)
		exec mailinx
		;;
		3)
		netradio
		;;
		4)
		irc
		;;
		5)
		monitors
		;;
		6)
		net-tips
		;;
		7)
		connect
		;;
		8)
		exec sharinx
		;;
		9)
		wifi
		;;
		m)
		exec menu
		;;
		f)
		switch
		f-net
		;;
		x)
		clear
		f-prompt
		;;
		*)
		echo "I didn't understand that request, sorry."
		sleep 2
		f-net
		;;
	esac

}

f-net