which_mail ()
    {
    head-col
    echo -e "\n  1) I want to use Gmail."
    echo -e "  2) I want to use my ISP's email (or another service)\n"
    bold ; white
    echo -e "\n  Type the number for your choice ..."
    
	repair ()
 	{
	read -s -n 1 REPAIR
	case $REPAIR in
	    1)
	    GMAIL=yes
	    f-mail-settings
	    ;;
	    2)
	    f-mail-settings
	    ;;
	    *)
	    echo -e "\n  That doesn't look right - please try again ...\n"
	    repair
	    ;;
	esac
	}
	repair
    }
    
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: Set up Mutt with IMAPS "
	echo "  3: Set up Mutt for Gmail "
	echo "  4: Start the Mutt Mail Program"
	echo "  5: Restore / Swap settings"
	echo
	echo "  6: Gmail in the Elinks browser"
	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
		figlet "  Mutt    !"
		head-col
		bold
		echo "  INX uses the Mutt mail user agent."
		info-col
		unbold
cat << EOMUTT

  It is an excellent program, but if you are not used to it, some pointers
  can help. There is a step-by-step setup sequence under the "Setup"
  menu entries. If you have a Gmail account you can use that, too.

  All you will need is your user name, your ISP mail server address, and
  your personal address for email. You will also need your password, of course.
  Most ISPs today support the IMAPS protocol on which this setup of Mutt is
  based. Try the setup sequence - chances are that it will work for you.

  Mutt has key shortcuts showing at the top, but briefly:

  * < m > to send a mail. Mutt will prompt for addresses and subjects.
  * < h > is set up in INX to show you your folders on the server.    
  * < q > quits from most menus or indices - read the top bar for hints. 
  * < r > to reply 

  * Your preferred editor will be used to compose mail -see Preferences
    in the main menu.
  * Once Mutt is running, it's best to leave it running for your 
    session. It will poll the server for new mail every fifteen 
    seconds or so - but don't be concerned, the mail stays on the
    server. Think of it as remotely accessible webmail :-) 
  * The configuration file is $HOME/.muttrc ...
    You can turn things on and off there - it has comments.

EOMUTT
		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 ; printf "\n\n"
		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)
		GMAIL=yes
		f-mail-settings
		;;
		
		4)
		f-mutt
		;;

		5)
		echo

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

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

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

		if [ -f ~/.msmtprc.bak ] ; then
                        mv ~/.msmtprc ~/.msmtprc-temp
                        mv ~/.msmtprc.bak ~/.msmtprc
                        mv ~/.msmtprc-temp ~/.msmtprc.bak
                elif [ -f ~/.msmtprc ] ; then
                        which_mail
                fi
			echo "  OK - restored your previous settings."
			chmod 600 ~/.muttrc ~/.muttrc.bak ~/.msmtprc ~/.msmtprc.bak
			sleep 2
			f-mail
		;;
		
		6)
		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 ELINKSGMAIL
				case $ELINKSGMAIL in
					n)
					f-mail
					;;
					*)
					gmail
					f-mail
					;;
				esac
		;;


		w)
		exec netinx
		;;
		m)
		exec menu
		;;

		x)
		clear
		f-prompt
		;;
		
		f)
		switch
		f-mail
		;;

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

}

f-mail-settings ()
{
        clear
	fig-col
        echo
        figlet "  inx          mail"
        echo
        echo
	fig-col

	echo " Set up or alter your settings for the Mutt mail program"
	
# Make sure we have the ingredients...
if ! [ -f ~/.muttrc-base ] ; then
	cp /etc/skel/.muttrc-base $HOME/
fi

# Blow the old .muttrc away if it's there ;-) But give a warning.. and a backup
echo
if [ -f "$HOME/.muttrc" ] ; then
        bred ; bold 
        yellow
        echo "  Your existing settings will be saved as a backup for you if you continue."
        white
        bblack
        echo
        head-col
        echo "  Your existing settings are for the account:"
        bold
        white
        echo
        printf " " ; grep ^my_hdr $HOME/.muttrc | cut -d: -f2
        echo
        bred
        yellow
        echo "  Do you want to change your account settings for email? (y/n)"
        bblack
        yellow
	f-ttynum
        read -s -n 1
                case $REPLY in
                        n)
                        f-mail
                        ;;
                        y)
                        echo
                        echo "  OK - making a backup of your mutt settings in $HOME/.muttrc.bak"
                        mv ~/.muttrc ~/.muttrc.bak
                        sleep 2
                        echo
                        echo "  Making a backup of your mail sending settings in $HOME/.msmtprc.bak"
			            mv ~/.msmtprc ~/.msmtprc.bak
			            sleep 2
                        ;;
                        *)
                        echo
                        echo "  Please type y for yes, or n for no."
                        sleep 2
                        f-mail-settings
                        ;;
                esac
fi


	echo

			
# Check each entry

checkit ()
{
white
bold
printf "  You have set " ; yellow ; printf "$SETTING" ; white ; printf ". Is that OK ? (n for \"no\")" ; printf "\n  "
	read -s -n 1 OOPS
	if [ "$OOPS" = "n" ] ; then
		"$HERE"
	fi
}
			
# Get info to use mutt as imap and put it in the ~/.muttrc and ~/.msmtp files...

name ()
{
HERE=name
        head-col
        bold
        echo -e "  Please enter your full name. ( e.g. Arthur Dent)\n  "
        printf "  "
        white
        read IDENTITY
	SETTING=$IDENTITY
	checkit
}


server ()
{
HERE=server
        head-col
        bold
        echo
        echo "  Do you use separate servers for sending and receiving? (y/n) "
        	read -s -n 1 SEPARATE
        	case $SEPARATE in
        		y)
        		echo
        		echo -e "  Please enter the server you use for sending mail ( e.g. smtp.beeblebrox.com )\n  "
                        printf "  "
        		white
        		read SMTP
        		SETTING=$SMTP
        		SEND=$SMTP
        		checkit
        		echo
        		head-col
        		bold
        		echo -e "  Please enter the server you use for receiving mail ( e.g. mail.beeblebrox.com )\n  "
                        printf "  "
        		white
        		read SERVER
        		SETTING=$SERVER
        		RECEIVE=$SERVER
        		checkit
        		;;
        		n)
        		;;
        		*)
        		echo
        		head-col
        		bold
        		echo "  Please type y for yes, n for no."
        		server
        		;;
        	esac
        	
        if [ "$SMTP" = "" ] ; then
        	echo
        	head-col
        	bold
        	echo -e "  Please enter your mail server's address ( e.g. mail.beeblebrox.com )\n  "
        	white
                printf "  "
        	read SERVER
                SETTING=$SERVER
                SEND=$SERVER
                RECEIVE=$SERVER
                checkit
        fi
}


address ()
{
HERE=address
        head-col
        bold
        echo
        echo -e "  Please enter your \"From:\" address ( e.g. arthur.dent@beeblebrox.com )\n  "
        white
        printf "  "
        read ADDRESS
        SETTING=$ADDRESS
        checkit
}


username ()
{
HERE=username
        head-col
        bold
        echo
        echo -e "  Please enter your \"user name\" ( It's the bit before the \" @ \", usually )\n  "
        printf "  "
        white
        read USERNAME
        SETTING=$USERNAME
        checkit	
}

password ()
{
    if ! grep -q persistent /proc/cmdline ; then
        head-col
        bold
        echo
        echo -e "  Please enter your email password - it will not show as you type.\n  "
        white
        printf "  "
        read -s PASSWORD
        echo
        echo -e "  Please type your password again as a check.\n  "
        printf "  "
        read -s CHECKIT
        if [ "$PASSWORD" != "$CHECKIT" ] ; then
            yellow
            bold
            echo "  The passwords do not match - please try again."
            password	
        fi
    fi	
}

put ()
{

# Put the info in a useful form in the right files

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

if [ ! $GMAIL ] ;then
    cat << EOMUTT >> $HOME/.muttrc

# Set by inx mailinx script
#-----------------------------------------------#

set folder="imaps://${SERVER}/INBOX"
set spoolfile="imaps://${SERVER}/INBOX."
set imap_user="${USERNAME}"
my_hdr From: ${IDENTITY} <${ADDRESS}> 

#-----------------------------------------------#

EOMUTT

else
    cat << EOGMAIL >> $HOME/.muttrc
    
# Set for gmail by inx mailinx script
#------------------------------------------------#

set folder="imaps://${USERNAME}@imap.gmail.com:993"
set spoolfile="imaps://${USERNAME}@imap.gmail.com/INBOX"
set imap_user="${USERNAME}"
my_hdr From: ${IDENTITY} <${ADDRESS}>
#------------------------------------------------#

EOGMAIL

fi

# ~/.msmtprc

if [ -f ~/.msmtprc ] ; then
	mv ~/.msmtprc ~/.msmtprc.bak
fi

# Check for separate smtp server before setting smtp
if [ "$RECEIVE" != "$SEND" ] ; then
	SERVER="$SEND"
fi

if [ ! $GMAIL ] ; then
    cat << EOMSMTP >> $HOME/.msmtprc
    
# Set by inx mailinx script
#-----------------------------------------------#

host $SERVER
from $IDENTITY <${ADDRESS}>
user $USERNAME

#-----------------------------------------------#
EOMSMTP

else
    cat << EOMSMTP >> $HOME/.msmtprc
    
# Set for gmail by inx mailinx script
#-----------------------------------------------#

host smtp.gmail.com
from ${USERNAME}@gmail.com
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
user ${USERNAME}@gmail.com
port 587

#-----------------------------------------------#

EOMSMTP

fi

# Blat the Mutt base file in below the settings ( order seems to matter in .muttrc )

echo " " >> $HOME/.muttrc
cat $HOME/.muttrc-base >> $HOME/.muttrc

# Fix the permissions
chmod 600 ~/.muttrc ~/.msmtprc

if [ -f ~/.muttrc.bak ] ; then
	chmod 600 ~/.muttrc.bak
fi
if [ -f ~/.msmtprc.bak ] ; then
	chmod 600 ~/.msmtprc.bak
fi

# Unset the pesky GMAIL variable...
unset GMAIL

echo

}

if [ ! $GMAIL ] ; then
    name
    server
    address
    username
    password
    put       
    
else
    name
    username
    ADDRESS="$USERNAME"@gmail.com
    RECEIVE=imap.gmail.com:993
    SEND=smtp.gmail.com
    password
    put
	
fi

    yellow
    bold
    echo
    echo "OK, setup is complete. Your settings look like this:"
    white
    echo
	
# Shove the settings in a file for easy later parsing, excluding password, of course ;-)
echo "  NAME    : $IDENTITY" > ~/.muttcurrent
echo "  EMAIL   : $ADDRESS" >> ~/.muttcurrent
echo "  SEND    : $SEND   " >> ~/.muttcurrent
echo "  RECEIVE : $RECEIVE" >> ~/.muttcurrent

cat ~/.muttcurrent

echo
echo "  Start the Mutt mail program? (y/n)"
startmutt ()
    {
	read -s -n 1 CONFIRM
	case $CONFIRM in
		y|Y)
		f-sup
		f-mutt
		;;
		n|N)
		# OK we need to store the password for non-persistent cases then...
		if ! grep -q persistent /proc/cmdline ; then
                    echo password $PASSWORD >> $HOME/.msmtprc  
                    echo set imap_pass=\"$PASSWORD\" >> $HOME/.muttrc
		fi    
		f-mail
		;;
		*)
		echo -e "\n  I didn't understand that choice - try again?"
		sleep 2
		startmutt
		;;
	esac
    }
	startmutt
	
# Wipe mail variables before starting f-mail
SERVER=
SMTP=
SEND=
RECEIVE=
ADDRESS=
IDENTITY=
PASSWORD=
GMAIL=

f-mail
}

# Annoying mutt wrapper that geeks will hate ;-)

f-mutt ()
{

schoolmaster ()
{
yellow
bold
echo "  *cough* please type \"yes\"  \" no\" or \"help\"..."
echo
echo "  I really recommend \"yes\" if you haven't done this before."
echo
white ; bblack
offer_config
}

offer_config ()
{                                                                                
	bold ; bred ; white
	echo -e "  Configure settings first? (yes/no/help)\n  "
	read COWBOYGEEK
	case $COWBOYGEEK in
		yes)
		bblack
		which_mail
		;;
		no)
		bblack && clear
		touch ~/.muttrc
		clear
		echo "  OK - have fun... "
		echo "  I've \"touched\" ~/.muttrc to make the warning stop, but any config is up to you now."
		echo
		bblack ; unbold 
		exit
		;;
		help)
		bblack
		f-mail
		;;
		*)
		schoolmaster
		;;
	esac

}


oops ()
{
if [ -f $HOME/.muttrc ] ; then
# Put passwords in readable files only during this mail session, on 'persistent' (security and all that...)
    if grep -q persistent /proc/cmdline ; then
        echo -e "\n  Please type your password for $(grep ^my_hdr $HOME/.muttrc | cut -d'<' -f2 | sed -s 's/>//')
  It will not show as you type...\n"
	read -s
	PASSWORD="$REPLY"
    fi	
    if [ -n "$PASSWORD" ] ; then # $PASSWORD should now exist either way... 
        echo password $PASSWORD >> $HOME/.msmtprc  
        echo set imap_pass=\"$PASSWORD\" >> $HOME/.muttrc
    fi


        echo 
	/usr/bin/mutt # OK, bypass this and use the actual program, since the config is there.
# Remove readable passwords before we exit to the menu again on 'persistent' sessions
# Leave it there if not 'persistent' ( hdd installs, or live 'volatile' sessions.)
    if grep -q persistent /proc/cmdline ; then
        sed -i 's/^password.*//g' $HOME/.msmtprc
        sed -i 's/^set imap_pass.*//g' $HOME/.muttrc
    fi	
	f-mail
else    if [ "$MAILCHOICE" != "4" ] ; then
            echo  
	    bred ; yellow ; bold 
	    echo
	    echo "  Umm... you seem to be trying to use mutt for mail without configuring it."
	    echo "  This will only work if you know *exactly* what you are doing.            "
	    bblack
	    offer_config
	else
	    which_mail
	fi    
fi
}

oops

}
