#!/bin/bash
#                       /usr/local/bin/mail_tidy
#  http://crystalfaeries.net/posix/bin/mail_tidy
# celeste:crystalfaery MAIL_TIDY 2021-02-28 05:41:21+00:00
# Tidy my Mail folder and my $HOME's artifacts of Mail
# optionally tidy the .abook addressbook
retain=7 # days retention of size 0 mailboxes to ensure that deletions propogate amongst all synchronized hosts = 1 week
# we have a list of special folders to keep even if empty:
for f in $(find ~/Mail -mindepth 1 -maxdepth 1 -size 0 -mtime +$retain \! -name .mail_tidy);do
	case $(/usr/bin/basename "${f}") in
		sent)
			ln -s ~/Mail/sent ~/sent	2>/dev/null
			;;	# sent e-mails redirected from ~ to Mail
		dead.letter)
			ln -s ~/Mail/dead.letter ~/dead.letter	2>/dev/null
			;;	# aborted e-mails redirected from ~ to Mail
		postponed)	
			ln -s ~/Mail/postponed ~/postponed	2>/dev/null
			;;	# postponed e-mails = drafts redirected from ~ to Mail
		*)
			rm	"${f}"	2>/dev/null
			;;	# regular mbox go bye-bye because empty
	esac
done
cp	/dev/null			.mutt/mailboxes
for box in $(find "${HOME}"/Mail	-type f	\! -name '.mail_tidy'	\! -size 0 | sort)
do
	echo "mailboxes    ${box}" >>	"${HOME}"/.mutt/mailboxes
done
touch	~/Mail/.mail_tidy		# tidy time stamp
if [ "${1}" == "-a" ]
then
	addressbook_export		# export all useful formats of current abook
	cd `pwd`/vcf			# including vcards (.vcf)
	vcardbreak ../abook.vcard	# break the vcard file into individual cards per address
fi
exit	$?	# arcanum legendarius
