#!/bin/bash


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:"
        bold
        white
        echo
        cat ~/.muttcurrent
        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 "Please enter your full name. ( e.g. Arthur Dent)"
        echo
        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 "Please enter the server you use for sending mail ( e.g. smtp.beeblebrox.com )"
        		white
        		read SMTP
        		SETTING=$SMTP
        		SEND=$SMTP
        		checkit
        		echo
        		head-col
        		bold
        		echo "Please enter the server you use for receiving mail ( e.g. mail.beeblebrox.com ) "
        		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 "Please enter your mail server's address ( e.g. mail.beeblebrox.com )"
        	echo
        	white
        	read SERVER
		SETTING=$SERVER
		SEND=$SERVER
		RECEIVE=$SERVER
		checkit
	fi
}


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


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

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

}

put ()
{

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

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

echo " " >> $HOME/.muttrc

echo "# Set by inx mail-mutt script" >> $HOME/.muttrc
echo "#-----------------------------------------------#" >> $HOME/.muttrc
echo " " >> $HOME/.muttrc
echo "set folder=\"imaps://$SERVER/INBOX\""      	>> $HOME/.muttrc
echo "set spoolfile=\"imaps://$SERVER/INBOX.\""	   	>> $HOME/.muttrc
echo "set imap_user=\""$USERNAME"\" "         		>> $HOME/.muttrc
echo "set imap_pass=\""$PASSWORD"\" "         		>> $HOME/.muttrc
echo "my_hdr From: "$IDENTITY" <"$ADDRESS"> " 		>> $HOME/.muttrc
echo " " >> $HOME/.muttrc
echo "#-----------------------------------------------#" >> $HOME/.muttrc

# ~/.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

echo " " >> $HOME/.msmtprc
echo "# Set by inx mail-mutt script" >> $HOME/.msmtprc
echo "#-----------------------------------------------#" >> $HOME/.msmtprc
echo " " >> $HOME/.msmtprc
echo "host "$SERVER" "                         >> $HOME/.msmtprc
echo "from "$IDENTITY" <"$ADDRESS"> "          >> $HOME/.msmtprc
echo "user "$USERNAME" "                       >> $HOME/.msmtprc
echo "password "$PASSWORD" "                   >> $HOME/.msmtprc
echo " " >> $HOME/.msmtprc
echo "#-----------------------------------------------#" >> $HOME/.msmtprc

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

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

# Obliterate the password variable now that it is recorded
PASSWORD=

# 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

echo

}

name
server
address
username
password
put       
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)"
	read -s -n 1 CONFIRM
	case $CONFIRM in
		y)
		f-sup
		mutt
		;;
		n)
		f-mail
		;;
		*)
		echo "I didn't understand that choice - you can select from the menu."
		sleep 2
		f-mail
		;;
	esac

# Wipe mail variables before starting
SERVER=
SMTP=
SEND=
RECEIVE=
ADDRESS=
IDENTITY=
PASSWORD=

f-mail
}


