#!/bin/bash
#                       /usr/local/bin/syncemail
# https://crystalfaeries.net/posix/bin/syncemail
# celeste:crystalfaery SYNCEMAIL 2016-10-13 04:36:42+00:00

let	result=0	#	success
echo	"`now`	=======	$0	=======	$1	======="	|| let result=1		# counting forwards from beginning of file

# retain=6 # how long to retain size 0 mailboxes to ensure that deletions propogate amongst all synchronized hosts

if [ "$1" == "--dry-run" ]
	then
		dryrun="--dry-run"	# only testing
	else
		dryrun=""		# do it for real
fi

# sync with my_twin
cd	$HOME		||	exit	-1
touch	Mail/.mail_tidy	#	synchronization time stamp
echo   "rsync -auvzH $dryrun	`my_twin`:~/Mail		  ~"
rsync -auvzH $dryrun		`my_twin`:~/Mail		  ~
echo   "rsync -auvzH $dryrun		  ~/Mail	`my_twin`:~"
rsync -auvzH $dryrun			  ~/Mail	`my_twin`:~

# tidy:	delete empty mailboxes we likely will not reuse, keeping certain ones even if empty
# cd	~/Mail		||	exit	-1
# echo -n "Deleting Empty Mailboxes:" 1>&2
# for f in $(find . -size 0 -mtime +$retain \! -name .mail_tidy | sed 's/^\.\///');do # we have a list of special folders to keep even if empty:
# 	case "$f" in
# 		sent)
# 			;;	# sent e-mails
# 		sent-mail)
# 			;;	# sent e-mails
# 		postponed)	
# 			;;	# postponed = drafts
# 		templates)
# 			;;	# templates for outgoing e-mails
# 		calendar)
# 			;;	# scheduled items with specific dates
# 		download)
# 			;;	# CONTENTS WILL BE DESTROYED BY DOWNLOADER
# 		*)
# 			echo -n  " $f"	1>&2	# notify the human as we delete...
# 			if [ "$dryrun" == "" ]
# 			then
# 				rm		   ~/Mail/"$f"	2>/dev/null
#				rm	 ~/documents/Mail/"$f"	2>/dev/null
#				rm	~/documents-/Mail/"$f"	2>/dev/null
# 			fi
# 			;;
# 	esac
# done
# echo " ;" 1>&2
echo	"/usr/local/bin/mail_tidy"
/usr/local/bin/mail_tidy	# delete empty mailboxes we likely will not reuse

syncabook			# synchronize addressbook

let	result=$?		# save the result code to return as my result code (pass through unless we override with our own error)
echo	"`now`	-------	$0	-------	$1	-------"	|| let result=255	# counting backwards from end of file
exit	$result


syntax highlighted by Code2HTML, v. 0.9.1