#!/bin/sh

muttdir="$HOME/.config/mutt"		# Main mutt config location
accdir="$muttdir/accounts"		# Directory for account settings
maildir="$HOME/.local/share/mail"	# Location of mail storage
creddir="$HOME/.local/share/muttwizard"	# Location of encrypted credentials
bindir="$HOME/.config/mutt/bin"		# Location of scripts run by mutt or the wizard
namere="^[a-z_][a-z0-9_-]*$"		# Regex to ensure viable username
emailre=".\+@.\+\\..\+" 		# Regex to confirm valid email address
[ ! -f "$muttdir/domains.csv" ] || [ ! -d "$bindir" ] && printf "Read the README. Be sure to put the repo in the right place before running.\\n" && exit 1
gpgemail="$(grep "$emailre" "$creddir/gpgemail")"	# Get previously set gpg email address
tmpdir="$(mktemp -d)"
GPG="gpg"; command -v gpg >/dev/null || GPG="gpg2"	# Ensure proper gpg command

# Get certificate location depending on OS. Linux is elsewhere condition.
case "$(uname)" in
	"Darwin") sslcert="/usr/local/etc/openssl/cert.pem" ;;
	*) sslcert="/etc/ssl/certs/ca-certificates.crt" ;;
esac

getprofiles() { \
	unset msmtp_header msmtp_profile mutt_profile offlineimap_header offlineimap_profile
	printf "Creating profiles for \`%s\`..." "$title"
msmtp_header="defaults
auth	on
tls	on
tls_trust_file	$sslcert
logfile	~/.config/msmtp/msmtp.log
"
msmtp_profile="

account $title
host $smtp
port $sport
from $login
user $login
passwordeval \"$GPG -d --quiet --for-your-eyes-only --no-tty $creddir/$title.gpg | sed -e '\$a\\'\"
"

if [ "$accounttype" = "offline" ]; then
mutt_profile="# vim: filetype=neomuttrc
# muttrc file for account $title
set realname = \"$realname\"
set from = \"$fulladdr\"
set sendmail = \"/usr/bin/msmtp -a $title\"
set folder = \"$maildir/$title\"
set header_cache = $accdir/$title/cache/headers
set message_cachedir = $accdir/$title/cache/bodies
set certificate_file = $accdir/$title/certificates
source \"$bindir/getmuttpass $title |\"

alias me $realname <$fulladdr>

set mbox_type = Maildir
set ssl_starttls = yes
set ssl_force_tls = yes

bind index,pager gg noop
bind index,pager g noop
bind index,pager M noop
bind index,pager C noop
bind index gg first-entry
unmailboxes *
set imap_check_subscribed
"
offlineimap_header="[general]
accounts =
starttls = yes
ssl = true
pythonfile = $bindir/imappwd.py

"
offlineimap_profile="
[Account $title]
localrepository = $title-local
remoterepository = $title-remote

[Repository $title-remote]
auth_mechanisms = LOGIN
type = $type
remoteuser = $login
remotepasseval = mailpasswd(\"$title\")
remoteport = $iport
sslcacertfile = $sslcert
$ifgoogleline

[Repository $title-local]
type = Maildir
localfolders = $maildir/$title
"

else
mutt_profile="# vim: filetype=neomuttrc
# muttrc file for account $title
set realname = \"$realname\"
set from = \"$fulladdr\"
set sendmail = \"/usr/bin/msmtp -a $title\"
set folder = \"imaps://$imap:$iport\"
set imap_user = $login
set header_cache = $accdir/$title/cache/headers
set message_cachedir = $accdir/$title/cache/bodies
set certificate_file = $accdir/$title/certificates
source \"$bindir/getmuttpass $title |\"

alias me $realname <$fulladdr>

set mbox_type = Maildir
set ssl_starttls = yes
set ssl_force_tls = yes

bind index,pager gg noop
bind index,pager g noop
bind index,pager M noop
bind index,pager C noop
bind index gg first-entry
unmailboxes *
"
fi
	printf "DONE.\\n"
}

addaccount() { \
	printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/offlineIMAP\\n\\nEmail: "
	printf "\033[36m\t"
	read -r fulladdr
	printf "\033[0m"
	while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
		printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: "
		printf "\033[36m\t"
		read -r fulladdr
		printf "\033[0m"
	done
	domain="$(echo "$fulladdr" | sed "s/.*@//")"
	printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
	serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
	if [ -z "$serverinfo" ]; then
		printf "Your email domain is not in mutt-wizard's database yet.\\nmutt-wizard will still autoconfigure everything, but you will have to manually type in your service's IMAP and SMTP server information.\\nYou can usually quickly find this by internet searching for it.\\n"
		printf "Insert the IMAP server for your email provider (excluding the port number)\\n"
		printf "\033[36m\t"
		read -r imap
		printf "\033[0m"
		printf "What is your server's IMAP port number? (Usually something like 993)\\n"
		printf "\033[36m\t"
		read -r iport
		printf "\033[0m"
		printf "Insert the SMTP server for your email provider (excluding the port number)\\n"
		printf "\033[36m\t"
		read -r smtp
		printf "\033[0m"
		printf "What is your server's SMTP port number? (Usually 587 or 465)\\n"
		printf "\033[36m\t"
		read -r sport
		printf "\033[0m"
		printf "\\nGreat! If you want to be helpful, copy the line below and you can add it to the \`domains.csv\` file on Github.\\nThis will make things easier for others who use your email provider.\\n\\n%s,%s,%s,%s,%s\\n\\nAlthough be sure to test to see if these settings work first! ;-)\\n" "$domain" "$imap" "$iport" "$smtp" "$sport"
	else
		IFS=, read -r service imap iport smtp sport <<EOF
$serverinfo
EOF
	printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically be found, so you won't have to look anything up!\\n\t\033[1mIMAP server\033[0m: %s\\n\t\033[1mIMAP port\033[0m: %s\\n\t\033[1mSMTP server\033[0m: %s\\n\t\033[1mSMTP port\033[0m: %s\\nThis data will be used by the wizard.\\n" "$imap" "$iport" "$smtp" "$sport"
	fi
	cont
	printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
	read -r realname
	printf "Enter a short, \033[36mone-word identifier\033[0m for this email account that will distinguish them from any other accounts you add.\\n\tAccount name: "
	read -r title
	while ! echo "$title" | grep "$namere" >/dev/null; do
		printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: "
		printf "\033[36m\t"
		read -r title
		printf "\033[0m"
	done
	printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): "
	printf "\033[36m\t"
	read -r login
	printf "\033[0m"
	[ -z "$login" ] && login="$fulladdr"
	if [ "$service" = "gmail.com" ]; then
		type="Gmail"; ifgoogleline="folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']
ssl_version = tls1_2"
		printf "\033[1mGoogle\033[0m mail account detected. Remember to check the README to make sure of Google-specific settings you must enable.\\n"
	else
		type="IMAP"; ifgoogleline="remotehost = $imap"
	fi
	grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
	seq 1 9 > "$tmpdir/mutt_all"
	idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
	getpass
	getprofiles
	mkdir -p "$accdir/$title/cache/bodies" "$HOME/.config/msmtp"
	[ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
	echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
	echo "$mutt_profile" > "$accdir/$title.muttrc"
	if [ "$accounttype" = "offline" ]; then
		mkdir -p "$HOME/.config/offlineimap/"
		[ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
	 	echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
		sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\\s*$/accounts = $title/g" "$HOME/.config/offlineimap/config"
	fi
	echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
	! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
	trysync && finalize
}

getpass() { \
	printf "Now enter your password for the \"%s\" account. Don't worry, this will be encrypted and only you with your GPG key can view it.\\n\tPassword: " "$title"
	stty -echo
	read -r password
	stty echo
	echo "$password" > "$tmpdir/$title"
	printf "Encrypting your password with %s..." "$GPG"
	"$GPG" -r "$gpgemail" --encrypt "$tmpdir/$title"
	printf "DONE\\nShredding all memory of your password for safety's sake..."
	unset password
	shred -u "$tmpdir/$title"
	mkdir -p "$creddir"
	mv "$tmpdir/$title.gpg" "$creddir/"
	printf "DONE.\\n"
}

askgpg() { \
	printf "To safely encrypt passwords, mutt-wizard requires that you have a GPG public/private key pair.\\n\\nPlease input the email address of your GPG key pair below.\\nEmail: "
	printf "\033[36m\t"
	read -r gpgemail
	printf "\033[0m"
	while ! echo "$gpgemail" | grep "$emailre" >/dev/null; do
		printf "That is not a valid email address. Please try again.\\nEmail: "
		printf "\033[36m\t"
		read -r gpgemail
		printf "\033[0m"
	done
	if "$GPG" -K | grep "<$gpgemail>" >/dev/null; then
		mkdir -p "$creddir"
		echo "$gpgemail" > "$creddir/gpgemail"
	else
		printf "You do not appear to have a private key associated with \033[33m%s\033[0m.\\nPlease generate a GPG key pair by running \`\033[32m%s --full-gen-key\033[0m\` and rerun the wizard.\\n" "$gpgemail" "$GPG"
		exit 1
	fi
}

formatShortcut() { \
	while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
	echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
	echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
	done ;}

trysync() { \
	! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning offlineimap manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && return 1
	printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run offlineimap briefly to (1) ensure that login details are functional and (2) allow offlineimap to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run offlineimap manually to finish the mail sync later.\\n"
	cont
	(sleep 15; killall offlineimap; killall offlineimap; killall offlineimap)>/dev/null 2>&1 &
	mkdir -p "$maildir"
	offlineimap -qoa "$title"
	if ls -d "$maildir/$title/"* >/dev/null 2>&1; then
		printf "\033[32mSync successful.\033[0m\\n"; return
	else
		printf "\033[31m\033[31mSync not successful.\033[0m Try running offlineimap manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n"; return 1
	fi
}

finalize() { \
	boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
	[ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that offlineimap has not been successfully run.\\nRun offlineimap, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && return
	echo "$boxes" > "$tmpdir/title_boxes"
	printf "Setting up the mutt sidebar...\\n"
	sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
	delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
	printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
	oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
	oneline="=$title $delim $oneline"
	spoolfile=$(grep -i "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
	record=$(grep -i "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
	postponed=$(grep -i "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
	trash=$(grep -i "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
	sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
	{ echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
	echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
	printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
	sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
	grep -i "$tmpdir/title_boxes" -e inbox | sed 1q | formatShortcut i inbox "$title"
	grep -i "$tmpdir/title_boxes" -e sent | sed 1q | formatShortcut s sent "$title"
	grep -i "$tmpdir/title_boxes" -e draft | sed 1q | formatShortcut d drafts "$title"
	grep -i "$tmpdir/title_boxes" -e trash | sed 1q | formatShortcut t trash "$title"
	grep -i "$tmpdir/title_boxes" -e spam | sed 1q | formatShortcut S spam "$title"
	grep -i "$tmpdir/title_boxes" -e junk | sed 1q | formatShortcut j junk "$title"
	grep -i "$tmpdir/title_boxes" -e archive | sed 1q | formatShortcut a archive "$title"
	printf "All done.\\n"
}

wipe () { \
	printf "Are you \033[31;1mreally\033[0m sure you want to delete all email accounts?\\n" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
	printf "Are you really, really sure?" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
	#rm -rf "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc"
	echo deleted
}

pick() { \
	numbered="$(ls $accdir/*muttrc | sed "s/.*\///;s/\..*//" | nl)"
	[ "$(echo "$numbered" | wc -w)" = 0 ]  && printf "No accounts to delete.\\n" && return 1
	printf "Select an accounts to %s:\\n" "$1"
	echo "$numbered"
	printf "\033[36m\t"
	read -r input
	printf "\033[0m"
	[ -z "$input" ] && return 1
	title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
	[ -z "$title" ] && printf "Invalid response." && return 1
	[ -n "$2" ] && printf "Are you sure you want to %s the \`%s\` account?\\n" "$1" "$title" && read -r input && echo "$input" | grep -i "y\(es\)*" >/dev/null
}

delete() { sed -i "
	/Account $title]/,/Account/{//!d}
	/Account $title]/d
	s/ $title\(,\|$\)//g
	s/=$title\(,\|$\)/=/g
	s/,$//g
	" "$HOME/.config/offlineimap/config"
	rm -rf "${accdir:?}/${title:?}" "$creddir/$title.gpg" "$accdir/$title.muttrc"
	sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
	# Delete from the line matching the account name, until the next account or empty line
	sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config";}

choosecron() { ! pgrep crond >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
	if crontab -l | grep mailsync >/dev/null; then
		echo "Active mail sync cronjob detected. Do you want to remove it?"
		printf "\033[36m\t"
		read -r rmyn
		printf "\033[0m"
		echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
	else
		echo "How many minutes between each mail sync?"
		printf "\033[36m\t"
		read -r minnum
		printf "\033[0m"
		while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
			printf "That doesn't look like a number. How many minutes between each mail sync?"
			printf "\033[36m\t"
			read -r minnum
			printf "\033[0m"
		done
		(crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus; export DISPLAY=:0; . \$HOME/.profile; $muttdir/bin/mailsync -f INBOX") | crontab - &&
			echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
	fi
}

cont() { [ -n "$1" ] && printf "%s\\n" "$1"
	printf "Press Enter to continue.\\n"
	stty -echo
	read -r null
	stty echo
}

addtype() { \
	while : ; do
		echo "Do you want to keep your mail for this account offline with offlineimap? [yes/no]"
		read -r offnot
		case "$offnot" in
			[Yy]es) accounttype="offline" && break ;;
			[Nn]o) accounttype="online" && break ;;
			*) echo "I don't know what you mean. Try again or press ctrl-c to quit." ;;
		esac
	done
	addaccount
}

main() { \
	while : ; do
	[ -z "$gpgemail" ] && askgpg
	printf "Current GPG key email: %s\\n\033[34m" "$gpgemail"
cat << "EOF"
 _ __ ___  _   _| |_| |_    __      _(_)______ _ _ __ __| |
| '_ ` _ \| | | | __| __|___\ \ /\ / / |_  / _` | '__/ _` |
| | | | | | |_| | |_| ||_____\ V  V /| |/ / (_| | | | (_| |
|_| |_| |_|\__,_|\__|\__|     \_/\_/ |_/___\__,_|_|  \__,_|
EOF

	printf "\033[0mMade and maintained by Luke Smith <https://lukesmith.xyz>

What would you like \033[32mmutt-wizard\033[0m to do?
	\033[31m1 Add an email account\033[0m
	2 Autodetect mailboxes
	3 Change an account's password
	4 Remove an account
	5 Change GPG key pair used for encryption
	6 Delete all account data
	7 Enable/disable autosync
	9 Edit this script
	0 Exit
Input a number to continue or press ctrl-c.\\n"
	printf "\033[36m\t"
	read -r choice
	printf "\033[0m"
	case "$choice" in
		1) addtype ;;
		2) pick finalize && finalize ;;
		3) pick "change the password of" yes && getpass ;;
		4) pick delete yes && delete ;;
		5) askgpg ;;
		6) wipe && printf "Account data purged." ;;
		7) choosecron ;;
		9) "$EDITOR" "$0"; exit ;;
		0) break ;;
		*) printf "Invalid input.\\n"
	esac
	cont
done
}

main
rm -rf "$tmpdir"