#!/bin/bash 

. f-colours

sendemail ()
{
	clear
	yellow 
	bold
	echo
	figlet inx"          "mail
	echo
	echo

	start
	
	echo
	green
	echo "Please enter the address for the recipient."
	white
	echo
	printf "To: "
	read TO
	echo
	green
	echo "Do you want to send copies to others? ( CC:) [n] "
	read -s  -n 1 COPY

	case $COPY in
		y)
		echo "Enter the addresses for the CC: recipients, separated by spaces."
		white
		printf "CC: "
		read CC
		;;

		*)
		;;
	esac

	echo
	green
	echo "Do you want to send private copies? (BCC:) [n] "
	echo
	read -s  -n 1 PRIVATE
	case $PRIVATE in
		y)
		echo
		echo "Enter the addresses for the BCC: recipients, separated by spaces."
		echo
		white
		printf "BCC: "
		read BCC
		;;
		
		*)
		;;
	esac
	
	echo
	white
	printf "Subject: "
	read SUBJECT
	echo
	green
	echo "Message will be composed in your editor."
	echo
	echo "Hit any key to begin."
	read -s -n 1
	editor /tmp/email-$N

	send-the-mail

}


start ()
{
# These should be non-existent on first run: fix them if they are missing 

	if ! [ -f $HOME/.mailcounter ] ; then
		N=0
		echo $N > $HOME/.mailcounter
	else
		N=$(cat $HOME/.mailcounter)
	fi

	if ! [ -f $HOME/.mailident ] ; then 
		name
		start
	elif ! [ -f $HOME/.mailserver ] ; then
		server
		start
	elif ! [ -f $HOME/.mailaddress ] ; then
		address
		start
	else
		settings
	fi

}
	
settings ()
{ 
# Ask the user to confirm, and give an opportunity to edit settings
	clear
	bold
	yellow
	figlet inx"          "mail
	echo
	printf "\n"
	green	
	echo "You currently have these settings: "
	echo
	echo
	yellow
	printf "\t 1: Your name       : " ; white ; printf "$(cat $HOME/.mailident) \n\n"
	yellow
	printf "\t 2: Your address    : " ; white ; printf "$(cat $HOME/.mailaddress) \n\n"
	yellow
	printf "\t 3: Your mail server: " ; white ; printf "$(cat $HOME/.mailserver) \n\n"
	echo
	green
	echo "Choose the number for any change you wish to make."
	echo
	yellow
	echo "You will have the opportunity to change the others if required"
	echo
	green
	echo "If these settings are correct, just hit <enter> or any other key."
	echo
	read -s -n 1 CHANGE
	echo
	case $CHANGE in
		1)
		name
		settings
		;;

		2)
		address
		settings
		;;

		3)
		server
		settings
		;;

		*)
		IDENTITY=$(cat $HOME/.mailident)
		SERVER=$(cat $HOME/.mailserver)
		ADDRESS=$(cat $HOME/.mailaddress)
		FROM="$IDENTITY $ADDRESS"
		;;
		
	esac

}


name ()
{
	bold
	green
	echo "Please enter your full name. ( e.g. Arthur Dent)"
	echo
	white
	read IDENTITY 
	echo "$IDENTITY" > $HOME/.mailident
}


server ()
{
	bold
	green
	echo 
	echo "Please enter your mail server's address ( e.g. smtp.beeblebrox.com )"
	echo
	white
	read SERVER
	echo "$SERVER" > $HOME/.mailserver
}


address ()
{
	bold
	green
	echo
	echo "Please enter your new \"From:\" address ( e.g. arthur.dent@vogon.org )"
	echo
	white
	read ADDRESS
	echo "<$ADDRESS>" > $HOME/.mailaddress
}


send-the-mail ()
{
	clear
	echo
	bold
	green
	figlet inx"          "mail
	echo
	yellow
	echo "Hit any key to send, n to return to the mail menu."
	read -s -n 1 SEND

case $SEND in
	*)
	echo	
	green
	echo "Please wait a few moments while I ask the server to send your message."
	echo
	yellow
	sendEmail -f "$FROM" \
		 -cc "$CC" \
		-bcc "$BCC" \
		  -t "$TO" \
		  -s "$SERVER" \
		  -u "$SUBJECT" \
		  -o message-file=/tmp/email-$N

		RET=$?


	
		if [ $RET != 0 ] ; then
			echo
			setterm -background red
			yellow
			echo "There was an error - try again in a minute or so. "
			echo "The mail server might be having some problems, "
			echo "or you have filled in your details incorrectly.   "
			echo
			setterm -background black
			green
			echo "Hit < r > to try sending your message again, any other key to re-enter your details."
			read -s -n 1 ERROR
			case $ERROR in
				r)
				send-the-mail
				;;
				*)
				sendemail
				;;
			esac
		fi

	white 
	unbold

# Append to the sentemail file.
	for each in "\n" From: "$FROM" "\n" Subject: "$SUBJECT" "\n" To: "$TO" "\n" CC: "$CC" "\n" BCC: "$BCC" "\n\n" 
	do printf "$each"  >> $HOME/sentemail
	done
	cat /tmp/email-$N >> $HOME/sentemail
	for break in "\n" "***************************" "\n"
	do printf "$break" >> $HOME/sentemail
	done
# Increment a counter for each message sent.
	N=$[ $N + 1 ]
	echo $N > $HOME/.mailcounter

	echo
	bold
	green
	if [ $RET = 0 ] ; then
		echo "You can find appended copies of sent messages in the $HOME/sentemail file."
		echo
	fi

yellow
	echo "Send another message ? [n]"
	read -s -n 1 AGAIN
	case $AGAIN in
		y)
		sendemail
		;;
		*)
		;;
	esac

clear

# Just a bit of silliness ... I might remove it ... then again, I might not.

printf "\n\n\n\n\n"
green
echo "Goodbye, then..."
sleep 1
magenta
printf "\n\n\n"
printf  "Thank you for making a" ; yellow ; printf " humble" ; \
 magenta ;  printf " mail script from the" ; green ; printf " Sirius Cybernetics Corporation" ; \
 yellow ; printf " happy."
printf "\n\n\n"
green
echo
printf "Oh," ; yellow ; printf " Brave" ; green ; printf " New World ..." ;\
 green ; printf " Oh," ; \
 green ; printf " I" ; yellow ; printf " love" ; green ; printf " my" ; yellow ; printf " job..." ; \
 green ; printf " *" ; magenta ; printf " *" ; yellow ; printf " *" ; green ; printf " *" ; red ; printf " *" ; \
 white ; printf " *" ; red ; printf " *" ; green ; printf " *" ; white
echo
bold
printf "\n\n\n"
setterm -background red -clear rest
printf "\n\n"
setterm -background yellow -clear rest
printf "\n\n"
setterm -background green -clear rest
printf "\n\n"
setterm -background cyan -clear rest
printf "\n\n"
setterm -background blue -clear rest
printf "\n\n"
setterm -background magenta -clear rest
printf "\n\n"
setterm -background black -clear rest
printf "\n\n"
green
echo "Hope you enjoyed your little rainbow. We brainless script robots like to thank our users."
echo
echo "I would tell you to \"Have a Nice Day\" , but I'm told some humans find orders annoying."
echo
yellow
echo "You can go back to the menu now - any key should do. "
read -s -n 1

clear
printf "\n\n\n\n\n\n\n\n\t\t"
echo "The Sirius Cybernetics Corporation apologises for the inconvenience. "
sleep 3.5

menu

	;;

	*)
	sendemail
	;;
esac

}

sendemail








