#!/bin/bash
#                       /usr/local/bin/synctails
# https://crystalfaeries.net/posix/bin/synctails
# celeste:crystalfaery SYNCTAILS 2017-07-03 06:02:31+00:00
# update TAILS persistent storage

exit -1	# configuration has changed, execution in new environment will be destructive

# optional argument is "--dry-run"
let result=0	# default success exit
echo	"`now`	=======	$0	=======	$1	======="								2>>/var/log/syncup.log 1>&2	|| exit -2
if [ $# == 0 ]
then
	:	# no argument is acceptable invocation
else
	if [ "$1" == "--dry-run" ]
	then
		:	# --dry-run is acceptable argument
	else
		echo "$0 does not cognize $1"										2>>/var/log/syncup.log 1>&2
		echo "`now`	-------	$0	-------	$1	-------"						2>>/var/log/syncup.log 1>&2
																			   exit -1
	fi
fi
     mkdir -p			/media/`whoami`/TailsData/Persistent/`whoami`	|| \
sudo mkdir -p			/media/`whoami`/TailsData/Persistent/`whoami`
     chown `whoami`:		/media/`whoami`/TailsData/Persistent/`whoami`	|| \
sudo chown `whoami`:		/media/`whoami`/TailsData/Persistent/`whoami`
     chmod 770			/media/`whoami`/TailsData/Persistent/`whoami`	|| \
sudo chmod 770			/media/`whoami`/TailsData/Persistent/`whoami`
cd				/media/`whoami`/TailsData/Persistent/`whoami`						2>>/var/log/syncup.log 1>&2	|| exit 1

# DOCUMENTS (TAILS is slave):
echo "rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
				~/documents					."					2>>/var/log/syncup.log 1>&2	|| exit 6
      rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
				~/documents					.					2>>/var/log/syncup.log 1>&2	|| exit 7

echo "rsync $1 -auvzH --delete ~/Mail						."					2>>/var/log/syncup.log 1>&2	|| exit 8
      rsync $1 -auvzH --delete ~/Mail						.					2>>/var/log/syncup.log 1>&2	|| exit 9

# crystalfaeries.net WEBSITE (TAILS is slave):
echo "rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
		/home/www							."					2>>/var/log/syncup.log 1>&2	|| exit 10
      rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
		/home/www							.					2>>/var/log/syncup.log 1>&2	|| exit 11
echo "rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
		/home/audio/`whoami`						audio"					2>>/var/log/syncup.log 1>&2	|| exit 12
#		/home/audio/{`whoami`,clairvoyance,www}				audio"					2>>/var/log/syncup.log 1>&2	|| exit 12
      rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
		/home/audio/`whoami`						audio					2>>/var/log/syncup.log 1>&2	|| exit 13
#		/home/audio/{`whoami`,clairvoyance,www}				audio					2>>/var/log/syncup.log 1>&2	|| exit 13
#echo "rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
#		/home/video/www							video"					2>>/var/log/syncup.log 1>&2	|| exit 14
#      rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
#		/home/video/www							video					2>>/var/log/syncup.log 1>&2	|| exit 15

# devideo:
#echo "mkdir -p									video/devideo"				2>>/var/log/syncup.log 1>&2	|| exit 16
#mkdir -p									video/devideo				2>>/var/log/syncup.log 1>&2	|| exit 17
#echo "rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
#		/home/video/devideo/`whoami`					video/devideo"				2>>/var/log/syncup.log 1>&2	|| exit 18
#      rsync $1 -auvzH --delete --exclude-from=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt \
#		/home/video/devideo/`whoami`					video/devideo				2>>/var/log/syncup.log 1>&2	|| exit 19

# GALLERIES (TAILS is slave):
mkdir -p	galleries												2>>/var/log/syncup.log 1>&2	|| exit 20
pushd		galleries												2>>/var/log/syncup.log 1>&2	|| exit 21
echo 'for gallery in *; do rsync -auvzH --delete /home/galleries/${gallery} .; done'					2>>/var/log/syncup.log 1>&2	|| exit 22
for gallery in *
do
	rsync -auvzH --delete /home/galleries/${gallery} .								2>>/var/log/syncup.log 1>&2	|| exit 23
done
popd															2>>/var/log/syncup.log 1>&2	|| exit 24
fdedupe -f														2>>/var/log/syncup.log 1>&2	|| exit 25

# POSIX (TAILS is slave):
mkdir -p	posix													2>>/var/log/syncup.log 1>&2	|| exit 26
pushd		posix													2>>/var/log/syncup.log 1>&2	|| exit 27
    for item in *
	do
	  echo "rsync $1 -auvzH --delete --exclude=jargon \
					--exclude=avidemux \
					--exclude=cryptocat \
					--exclude=telegram \
					--exclude=telegram-gui \
					--exclude=unicode \
							/usr/local/$item	."					2>>/var/log/syncup.log 1>&2	|| exit 28
		rsync $1 -auvzH --delete --exclude=jargon \
					--exclude=avidemux \
					--exclude=cryptocat \
					--exclude=telegram \
					--exclude=telegram-gui \
					--exclude=unicode \
							/usr/local/$item	.					2>>/var/log/syncup.log 1>&2	|| exit 29
	done
popd															2>>/var/log/syncup.log 1>&2	|| exit 30

# BOOKMARKS (bi-directional):
echo "rsync $1 -auvzH       ~/.mozilla/firefox/*.default/bookmarks.html /media/`whoami`/TailsData/bookmarks"		2>>/var/log/syncup.log 1>&2	|| exit 31
      rsync $1 -auvzH       ~/.mozilla/firefox/*.default/bookmarks.html /media/`whoami`/TailsData/bookmarks		2>>/var/log/syncup.log 1>&2	|| exit 32
echo "rsync $1 -auvzH /media/`whoami`/TailsData/bookmarks/bookmarks.html       ~/.mozilla/firefox/*.default"		2>>/var/log/syncup.log 1>&2	|| exit 33
      rsync $1 -auvzH /media/`whoami`/TailsData/bookmarks/bookmarks.html       ~/.mozilla/firefox/*.default		2>>/var/log/syncup.log 1>&2	|| exit 34

# Log what we DID NOT SYNC based on the EXCLUDE FILE which lives on the USB stick, to keep it from overflowing!
echo	 "EXCLUDED-FROM=/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt:"					2>>/var/log/syncup.log 1>&2	|| exit 35
cat			/media/`whoami`/TailsData/Persistent/`whoami`/.exclude.txt					2>>/var/log/syncup.log 1>&2	|| exit 36

echo	"`now`	-------	$0	-------	$1	-------"								2>>/var/log/syncup.log 1>&2	|| exit 37
exit $result


syntax highlighted by Code2HTML, v. 0.9.1