#!/bin/bash
#                      /usr/local/bin/synchome
# http://crystalfaeries.net/posix/bin/synchome
# celeste crystalfaery 2020-08-28 19:42:38+00:00
# synchronize and backup /home
let	exitval=0	# presume success

#######	#######	#######	#######
# ARGUMENTS:
#######	#######	#######	#######
if [ $# == 0 ]
	then
		dryrun=""		# do it for real
	else	# total hack accepting ANY argument as meaning --dry-run
		dryrun="--dry-run"	# only testing
fi

#######	#######	#######	#######
# LOG:
#######	#######	#######	#######
echo	"`now`	=======	$0	=======	$0	======="

#######	#######	#######	#######
# SYNC ~ WITH MY_TWIN
#######	#######	#######	#######
cd		$HOME		||	exit	-2
mkdir -p	$HOME/documents
touch		$HOME/documents
echo   "rsync -auvzH $dryrun					      `my_twin`:~/.abook				  ~"
rsync -auvzH $dryrun						      `my_twin`:~/.abook		  		  ~
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun --exclude=.muttrc.local --exclude=.muttheaders	~/.abook	   ~/.mutt*	`my_twin`:~"
rsync -auvzH $dryrun         --exclude=.muttrc.local --exclude=.muttheaders	~/.abook	   ~/.mutt*	`my_twin`:~
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun --exclude=.muttrc.local --exclude=.muttheaders		 `my_twin`:~/.mutt*		  ~"
rsync -auvzH $dryrun	     --exclude=.muttrc.local --exclude=.muttheaders		 `my_twin`:~/.mutt*		  ~
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun ~/.feed2imaprc `my_twin`:~/.feed2imaprc"
rsync -auvzH $dryrun         ~/.feed2imaprc `my_twin`:~/.feed2imaprc
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun		    `my_twin`:~/.feed2imaprc ~/.feed2imaprc" 
rsync -auvzH $dryrun			    `my_twin`:~/.feed2imaprc ~/.feed2imaprc
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun                  ~/.calendar `my_twin`:~"
rsync -auvzH $dryrun                          ~/.calendar `my_twin`:~
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun        `my_twin`:~/.calendar ~"
rsync -auvzH $dryrun                `my_twin`:~/.calendar ~
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun --exclude=.dush*.txt --exclude=playlists/monitor*.txt	`my_twin`:/home/`whoami`/documents	      ~"
	rsync -auvzH $dryrun --exclude=.dush*.txt --exclude=playlists/monitor*.txt	`my_twin`:/home/`whoami`/documents	      ~
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun --exclude=.dush.txt --exclude=playlists/monitor*.txt	/home/`whoami`/{.min_price.txt,documents}	`my_twin`:~"
	rsync -auvzH $dryrun --exclude=.dush.txt --exclude=playlists/monitor*.txt	/home/`whoami`/{.min_price.txt,documents}	`my_twin`:~
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun ~/.mozilla/firefox/*.default/bookmarks.html `/usr/local/bin/my_twin`:~/.mozilla/firefox/*.default"
	rsync -auvzH $dryrun ~/.mozilla/firefox/*.default/bookmarks.html `/usr/local/bin/my_twin`:~/.mozilla/firefox/*.default
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

echo   "rsync -auvzH $dryrun `/usr/local/bin/my_twin`:~/.mozilla/firefox/*.default/bookmarks.html ~/.mozilla/firefox/*.default"
	rsync -auvzH $dryrun `/usr/local/bin/my_twin`:~/.mozilla/firefox/*.default/bookmarks.html ~/.mozilla/firefox/*.default
let result=$?
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

#######	#######	#######	#######
# BACKUP MY ~
#######	#######	#######	#######
# echo   "rsync -auvzH $dryrun --exclude-from=/home/`whoami`/.exclude.documents.txt /home/`whoami`/{.??,}* ~/documents"
# 	rsync -auvzH $dryrun --exclude-from=/home/`whoami`/.exclude.documents.txt /home/`whoami`/{.??,}* ~/documents
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

#######	#######	#######	#######
# SYNC /home WITH MY_TWIN
#######	#######	#######	#######
cd /home || exit -3	# sync website, audio, video:
case `hostname` in

#######	#######	#######	#######
pixy)
echo "rsync -auvzH	/home/public/downloads `my_twin`:/home/public"
rsync -auvzH  		/home/public/downloads `my_twin`:/home/public
	# we sync audio and video and public only to twin not from twin due to disk space
echo   "rsync -auvzH $dryrun           /home/{www,audio,video,public}	`my_twin`:/home"
	rsync -auvzH $dryrun           /home/{www,audio,video,public}	`my_twin`:/home
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
echo   "rsync -auvzH $dryrun  `my_twin`:/home/www		  /home"
	rsync -auvzH $dryrun  `my_twin`:/home/www		  /home
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
# echo   "rsync -auvzH $dryrun --exclude=audio/devaudio --exclude=video/devideo {www,audio,video} ~/documents"
# 	rsync -auvzH $dryrun --exclude=audio/devaudio --exclude=video/devideo {www,audio,video} ~/documents 
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
# echo   "rsync -auvzH					 $HOME/crystalfaeries.net/audio/devaudio	  $HOME/crystalfaeries.net/video/devideo	~/documents"
# 	rsync -auvzH					 $HOME/crystalfaeries.net/audio/devaudio	  $HOME/crystalfaeries.net/video/devideo	~/documents
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
;;

#######	#######	#######	#######
muse)
echo "rsync -auvzH	--exclude=downloads/youtube.com /home/public/downloads `my_twin`:/home/public"
rsync -auvzH		--exclude=downloads/youtube.com /home/public/downloads `my_twin`:/home/public
	# we don't sync audio and video because our twin remote mounts ours due to disk space limits
echo   "rsync -auvzH $dryrun		/home/www	`my_twin`:/home"
	rsync -auvzH $dryrun		/home/www	`my_twin`:/home
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
echo   "rsync -auvzH $dryrun  `my_twin`:/home/www		  /home"
	rsync -auvzH $dryrun  `my_twin`:/home/www		  /home
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
# echo   "rsync -auvzH $dryrun --exclude=audio/devaudio --exclude=video/devideo {www,audio,video} ~/documents"
# 	rsync -auvzH $dryrun --exclude=audio/devaudio --exclude=video/devideo {www,audio,video} ~/documents 
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
# echo   "rsync -auvzH					 $HOME/crystalfaeries.net/audio/devaudio	  $HOME/crystalfaeries.net/video/devideo	~/documents"
# 	rsync -auvzH					 $HOME/crystalfaeries.net/audio/devaudio	  $HOME/crystalfaeries.net/video/devideo	~/documents
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
;;

#######	#######	#######	#######
tomril)
#echo "rsync -auvzH	/home/public/downloads `my_twin`:/home/public"
#rsync -auvzH		/home/public/downloads `my_twin`:/home/public
echo   "rsync -auvzH $dryrun           /home/www	`my_twin`:/home"
	rsync -auvzH $dryrun           /home/www	`my_twin`:/home
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
echo   "rsync -auvzH $dryrun `my_twin`:/home/{www,audio,video,public}		  /home"
	rsync -auvzH $dryrun `my_twin`:/home/{www,audio,video,public}		  /home
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
# echo   "rsync -auvzH $dryrun --exclude=audio/devaudio --exclude=video/devideo {www,audio,video} ~/documents"
# 	rsync -auvzH $dryrun --exclude=audio/devaudio --exclude=video/devideo {www,audio,video} ~/documents 
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
# echo   "rsync -auvzH					 $HOME/crystalfaeries.net/audio/devaudio	  $HOME/crystalfaeries.net/video/devideo	~/documents"
# 	rsync -auvzH					 $HOME/crystalfaeries.net/audio/devaudio	  $HOME/crystalfaeries.net/video/devideo	~/documents
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
;;

#######	#######	#######	#######
*)
echo "`hostname` unknown to script $0" 1>&2
exitval=3
;;

esac
#######	#######	#######	#######

#######	#######	#######	#######
# SYNC /home/local/{bin,doc,etc}
#######	#######	#######	#######
for d in  bin doc etc
do
  echo "rsync -auvzH --exclude=doc/avlinux /home/local/$d `my_twin`:/home/local"
	rsync -auvzH --exclude=doc/avlinux /home/local/$d `my_twin`:/home/local
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi

  echo "rsync -auvzH --exclude=doc/avlinux `my_twin`:/home/local/$d           /home/local"
	rsync -auvzH --exclude=doc/avlinux `my_twin`:/home/local/$d           /home/local
if [[ $result -ne 0 ]]
then
	let   exitval=$result
fi
done

#######	#######	#######	#######
# DELETE EMPTY MAILBOXES we likely will not reuse, keeping certain ones even if empty we likely will reuse
#######	#######	#######	#######
retain=6 # days retention of size 0 mailboxes to ensure that deletions propogate amongst all synchronized hosts = 1 week
cd	~/Mail		||	exit	-1	# MY MailBoxes:
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
		Drafts)
			ln -s ~/Mail/Drafts ~/Drafts
			;;	# draft e-mails
		Sent)
			ln -s ~/Mail/Sent ~/Sent
			;;	# sent e-mails
		sent)
			ln -s ~/Mail/sent ~/sent
			;;	# sent e-mails
		sent-mail)
			ln -s ~/Mail/sent-mail ~/sent-mail
			;;	# sent e-mails
		postponed)	
			ln -s ~/Mail/postponed ~/postponed
			;;	# postponed = drafts
		templates)
			ln -s ~/Mail/templates ~/templates
			;;	# templates for outgoing e-mails
		calendar)
			ln -s ~/Mail/calendar ~/calendar
			;;	# scheduled items with specific dates
		download)
			ln -s ~/Mail/download ~/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	# done deleting empty mailboxes
touch	.mail_tidy	# tidy time stamp
# echo "rsync -auvzH ~/Mail ~/documents"
#	rsync -auvzH ~/Mail ~/documents	# backup Mail

#######	#######	#######	#######
# LOG:
#######	#######	#######	#######
if [[ $exitval -eq 0 ]]
then
	echo	"`now`	-------	$0	-------	$0	-------"
else
	echo	"`now`	-------	$0	ERROR:	$exitval	-------"
fi
exit	$exitval	# failure is an option, exit code is most recent of multiple error codes

# on host pixy:
cd /home/galleries/divas
for d in *;do rsync -auvzH  --delete tomril:/home/galleries/divas/$d . ; done
##########################################################################
# on host muse:
cd
rsync -auvzH /home/{audio,video,www,public} pixy:/home
rsync -auvzH /home/{audio,video,www,public} tomril:/home
rsync -auvzH documents Mail public_html txt pixy:~
rsync -auvzH documents Mail public_html txt tomril:~
echo should have excluded documents/.dush.txt* documents/playlists/monitor*.txt*
 2007  history >> /usr/local/bin/synchronize/synchome 
echo	"`now`	-------	$0	-------	$1	-------"
