#!/bin/bash


f-mail ()
{
	clear
        fig-col
        echo
        figlet inx"          "mail
        echo
	head-col
        echo "This is the mail menu for inx."
	echo
	bold
	head-col
	echo "Please read the introduction first !"
	echo
	info-col
	echo "1: Introduction to Mutt"
	echo "2: Simple Setup"
	echo "3: Use Mutt Mail"
	echo "4: Restore saved Mutt settings after a change"
	echo
	echo "5: Gmail"
	echo
	head-col
	echo "w: Net and Web menu."
	echo "m: Return me to the main inx menu."
	echo "f: Flip colours."
	echo "x: Give me a command prompt."

	# Print tty number right edge of terminal
	f-ttynum

	bold
	read -s -n 1 MAILCHOICE
	case $MAILCHOICE in
		1)
		clear	
		fig-col
		echo
		figlet Mutt"   "!
		head-col
		bold
		echo "INX uses the Mutt mail user agent."
		info-col
		unbold
		echo
		echo "It is an excellent program, but if you are not used to it, some pointers"
		echo "can help. There is a step-by-step setup sequence under \"Simple Setup\" (2:)."
		echo 
		echo "All you will need is your user name, your ISP's mail server address, and"
		echo "your personal address for email. You will also need your password, of course."
		echo
		echo "Most ISPs today support the IMAPS protocol on which this setup of Mutt is"
		echo "based. Try the setup sequence - chances are that it will work for you."
		echo
		echo "Mutt has key shortcuts showing at the top, but briefly:"
		echo
		echo "* < m > to send a mail. Mutt will prompt for addresses and subjects."
		echo "* < h > is set up in INX to show you your folders on the server.    "
		echo "* < q > quits from most menus or indices - read the top bar for hints. "
		echo "* < r > to reply "
		echo
		echo "* Your editor will be used to compose mail - by default, nano ."
		echo
		echo "* Once Mutt is running, it's best to leave it running for your "
		echo "	  session. It will poll the server for new mail every fifteen "
		echo "	  seconds or so - but don't be concerned, the mail stays on the"
		echo "	  server. Think of it as remotely accessible webmail :-) "
		echo
		echo "* The configuration file is $HOME/.muttrc ..."
		echo "  You can turn things on and off there - it has comments."
		echo
		head-col
		bold
		echo "To know more (recommended), hit y for yes: otherwise"
		echo "hit any other key to return to the inx mail menu."
			f-ttynum
			read -s -n 1 BACK
			case $BACK in
				y)
		clear
		head-col
		bold
		echo "How the INX Mutt mail set-up actually works."
		info-col
		unbold
		echo "INX Mail sets up details for your email in two respects:"
		echo
		head-col
		bold
		echo "1: The way email is to be sent."
		info-col
		unbold
		echo
		echo "    To send mail you need a program known as an \"SMTP\" program."
		echo "    (Simple Mail Transfer Protocol)"
		echo "    INX uses the \"msmtp\" program for this. The address you enter"
		echo "    in the \"Simple Setup\" should be for your ISP's \"sending\" server"
		echo "    if your ISP uses two servers. You will be prompted for this if needed."
		echo "    In this case the information should have been given to you by the ISP."
		echo
		head-col
		bold
		echo "2: The way email is to be read."
		info-col
		unbold
		echo
		echo "    INX uses Mutt to read \"folders\" on the mail server - this server"
		echo "    is often, but not necessarily, the same as the SMTP server."
		echo "    If your ISP gave you only one mail server address, both sending"
		echo "    and \"reading\" take place on the same machine."
		head-col
		bold
		echo
		echo "Other Details"
		info-col
		unbold
		echo
		echo "In a few cases, the ISP only supports the \"pop\" protocol."
		echo "The current set-up in INX is for the IMAPS protocol."
		echo
		echo "The set-up script uses a ~/.muttrc-base file which contains the settings"
		echo "that normally require no change for IMAPS."
		echo
		echo "INX allows you to set up two different accounts and swap between them."
		echo "Simply use the \"Restore\" menu entry to swap the \"backups\" around."
		echo
		head-col
		bold
		echo "Hit any key to return to the INX Mail menu."
				f-ttynum
				read -s -n 1 
				f-mail
				;;
				*)
				f-mail
				;;
			esac
		;;

		2)
		f-mail-settings
		;;

		3)
		f-mutt
		yellow
		bold
		echo "(m) Return to the inx mail menu, (x) give me a command prompt."
		echo "Any other key to return to the Mutt mail program."
		f-ttynum
		read -s -n 1
			case $REPLY in
				m)
				f-mail
				;;
				x)
				f-prompt
				;;
				*)
				f-mutt
				;;
			esac
		;;

		4)
		echo

		# sanity checks ?
		# If either muttrc or msmtprc are missing, they need fixing
		if ! [ -f ~/.muttrc ] || ! [ -f ~/.msmtprc ] ; then
			# bail out
			echo "Something wicked happened to your mail setup..."
			echo "Let's replace it."
			sleep 4
			f-mail-settings
		fi

		if [ -f ~/.muttrc.bak ] ; then
			mv ~/.muttrc ~/.mutt-temp 
			mv ~/.muttrc.bak ~/.muttrc
			mv ~/.mutt-temp ~/.muttrc.bak  

		elif [ -f ~/.muttrc ] ; then
			f-mail-settings
		fi

		if [ -f ~/.msmtprc.bak ] ; then
                        mv ~/.msmtprc ~/.msmtprc-temp
                        mv ~/.msmtprc.bak ~/.msmtprc
                        mv ~/.msmtprc-temp ~/.msmtprc.bak
                elif [ -f ~/.msmtprc ] ; then
                        f-mail-settings
                fi
			echo "OK - restored your previous settings."
			chmod 600 ~/.muttrc ~/.muttrc.bak ~/.msmtprc ~/.msmtprc.bak
			sleep 2
			f-mail
		;;
		
		5)
		clear
		echo
		head-col
		echo "You can use your Gmail account through the basic no-frills interface."
		echo "This is simply Gmail using the Elinks browser - it is limited, but it works."
		echo
		fig-col
		echo "Go ahead and use Elinks with Gmail ? (n) for no..."
			f-ttynum
			read -s -n 1 GMAIL
				case $GMAIL in
					n)
					f-mail
					;;
					*)
					gmail
					f-mail
					;;
				esac
		;;


		w)
		f-net
		;;
		m)
		f-menu
		;;

		x)
		clear
		f-prompt
		;;
		
		f)
		export CALL=mailinx
		f-switch
		;;

		*)
		echo
		head-col
		echo "I didn't understand that choice. Try again ? "
		sleep 2
		f-mail
		;;
	esac
	

}


						


