#!/bin/bash
#                       /usr/local/bin/sync_fay-bluehost
# https://crystalfaeries.net/posix/bin/sync_fay-bluehost
# celeste:crystalfaery SYNC_FAY-BLUEHOST 2018-07-16 06:29:51+00:00
# rsync mirroring script maps filesystem layouts between:
# BlueHost server box6537.bluehost.com, and
# "fay" (if i could set her hostname in TermUX), my Android.

# CONFIGURATION:
let SSHleep=10	# BLUEHOST RATE-LIMITS SSH CONNECTIONS :-(

cd	# There's no place like Om...
echo "box6537.bluehost.com	<=====>	fay.crystalfaeries.net"	>&2
# SETUP LOGGING
mv				~/.sync_fay-bluehost.txt	\
				~/.sync_fay-bluehost.txt-	# backup sync log
cp /dev/null			~/.sync_fay-bluehost.txt	# clear       log
for f in   crystalfaeries.net/src/.sync_fay-bluehost.*
do
	cat		${f} >> ~/.sync_fay-bluehost.txt-	# backup sync log
	cp /dev/null	${f}					# clear       log
done

# WATCH THE LOGS ON THE CONSOLE WHILE RUNNING:
tail -F	~/.sync_fay-bluehost.txt crystalfaeries.net/src/.sync_fay-bluehost.* &

echo "=======	box6537.bluehost.com	<=====>	fay.crystalfaeries.net	======="	>> ~/.sync_fay-bluehost.txt
echo "	  `now`"	>> ~/.sync_fay-bluehost.txt
# collect updates to loose files at top level of server crystalfaeries.net
# some of which are updated periodically / automatically / frequently

rsync -auvH	\
	divservi@box6537.bluehost.com:~/crystalfaeries.net/{*.dat,*.gz,*.ico,*.php,*.png,{.,}*.txt,*.{,s}html,*.xml}	\
				      ~/crystalfaeries.net/		>> ~/.sync_fay-bluehost.txt 2>&1
echo -n "SSHleeping	${SSHleep}: "	>> ~/.sync_fay-bluehost.txt
	uptime	>> ~/.sync_fay-bluehost.txt
	sleep	${SSHleep}	# BLUEHOST RATE-LIMITS SSH CONNECTIONS :-(

	echo "	  `now`"	>> ~/.sync_fay-bluehost.txt
rsync -auvH		      ~/crystalfaeries.net/{*.dat,*.gz,*.ico,*.php,*.png,{.,}*.txt,*.{,s}html,*.xml}	\
divservi@box6537.bluehost.com:~/crystalfaeries.net/		>> ~/.sync_fay-bluehost.txt 2>&1
echo -n "SSHleeping	${SSHleep}: "	>> ~/.sync_fay-bluehost.txt
	uptime	>> ~/.sync_fay-bluehost.txt
	sleep	${SSHleep}	# BLUEHOST RATE-LIMITS SSH CONNECTIONS :-(

#	TOP LEVEL DIRECTORIES:
#	BLUEHOST -> FAY
# sadly, microshaft compatible filesystems in Android phone do NOT permit
# setting a transferred file's date-stamp(s) via rsync, so that all directories
# visible outside TermUX in Android's Graphica lUser Interface get "today's"
# timestamp, and SHOULD NOT BE rsync'D BACK TO THE SERVER as an "updated" file.
#	audio	\
for target in	\
	celeste	\
	linkdoc	\
	posix	\
	src	\
	fae	\
	pdf	\
	video	\
	clairvoyance	\

do	# roughly size order for directories
	echo "	  `now`"	>> ~/.sync_fay-bluehost.txt
	echo "=======	${target}	======="	>> ~/.sync_fay-bluehost.txt
	rsync	-auvH	\
	  divservi@box6537.bluehost.com:~/crystalfaeries.net/${target}/{.??,}* \
 					~/crystalfaeries.net/${target}/	\
|	 grep -v "failed to set times on"				\
>>		 ~/crystalfaeries.net/src/.sync_fay-bluehost.${target}.txt 2>&1
echo "-------	${target}	-------"	>> ~/.sync_fay-bluehost.txt
echo -n "SSHleeping	${SSHleep}: "	>> ~/.sync_fay-bluehost.txt
	uptime	>> ~/.sync_fay-bluehost.txt
	sleep	${SSHleep}	# BLUEHOST RATE-LIMITS SSH CONNECTIONS :-(

done

#	FAY -> BLUEHOST
# sadly, microshaft compatible filesystems in Android phone do NOT permit
# setting a transferred file's date-stamp(s) via rsync, so that all directories
# visible outside TermUX in Android's Graphica lUser Interface get "today's"
# timestamp, and SHOULD NOT BE rsync'D BACK TO THE SERVER as an "updated" file.
#	audio	\
for target in	\
	celeste	\
	linkdoc	\
	posix	\
	src	\
	clairvoyance	\

do	# roughly size order for directories
	echo "	  `now`"	>> ~/.sync_fay-bluehost.txt
	echo "=======	${target}	======="	>> ~/.sync_fay-bluehost.txt
	rsync	-auvH							\
			       ~/crystalfaeries.net/${target}/{.??,}*	\
 divservi@box6537.bluehost.com:~/crystalfaeries.net/${target}/		\
|	 grep -v "failed to set times on"				\
>>	~/crystalfaeries.net/src/.sync_fay-bluehost.${target}.txt 2>&1
echo "-------	${target}	-------"	>> ~/.sync_fay-bluehost.txt
echo -n "SSHleeping	${SSHleep}: "	>> ~/.sync_fay-bluehost.txt
	uptime	>> ~/.sync_fay-bluehost.txt
	sleep	${SSHleep}	# BLUEHOST RATE-LIMITS SSH CONNECTIONS :-(

done

#######	#######	#######	#######	#######	#######	#######	#######	#######	#######
# GRAB THE imgs (sans the igal2 thumbnails for web access)
	echo "	  `now`"	>> ~/.sync_fay-bluehost.txt
echo "=======	imgs	======="	>> ~/.sync_fay-bluehost.txt
rsync	-auvH	\
--exclude='clipart'	\
--exclude='*/*/*.css'	\
--exclude='*/*/?.html'	\
--exclude='*/*/??.html'	\
--exclude='*/*/???.html'	\
--exclude='*/*/????.html'	\
--exclude='*/*/.indextemplate2.html'	\
--exclude='*/*/.slidetemplate2.html'	\
--exclude='*/*/.thumb_*'	\
--exclude='*/*/.tile.png'	\
--exclude='*/*.css'	\
--exclude='*/?.html'	\
--exclude='*/??.html'	\
--exclude='*/???.html'	\
--exclude='*/????.html'	\
--exclude='*/.indextemplate2.html'	\
--exclude='*/.slidetemplate2.html'	\
--exclude='*/.thumb_*'	\
--exclude='*/.tile.png'	\
--exclude='*.css'	\
--exclude='?.html'	\
--exclude='??.html'	\
--exclude='???.html'	\
--exclude='????.html'	\
--exclude='.indextemplate2.html'	\
--exclude='.slidetemplate2.html'	\
--exclude='.thumb_*'	\
--exclude='.tile.png'	\
  divservi@box6537.bluehost.com:~/crystalfaeries.net/imgs/{.??,}*	\
				~/crystalfaeries.net/imgs/		\
|	grep -v "failed to set times on"				\
>>				~/crystalfaeries.net/src/.sync_fay-bluehost.imgs.txt 2>&1
echo "-------	imgs	-------"	>> ~/.sync_fay-bluehost.txt
echo -n "SSHleeping	${SSHleep}: "	>> ~/.sync_fay-bluehost.txt
	uptime	>> ~/.sync_fay-bluehost.txt
	sleep	${SSHleep}	# BLUEHOST RATE-LIMITS SSH CONNECTIONS :-(

# WHICH imgs EXIST ONLY ON fay?
	echo "	  `now`"	>> ~/.sync_fay-bluehost.txt
rsync	-auvH --dry-run	--delete	\
--exclude='clipart'	\
--exclude='*/*/*.css'	\
--exclude='*/*/?.html'	\
--exclude='*/*/??.html'	\
--exclude='*/*/???.html'	\
--exclude='*/*/????.html'	\
--exclude='*/*/.indextemplate2.html'	\
--exclude='*/*/.slidetemplate2.html'	\
--exclude='*/*/.thumb_*'	\
--exclude='*/*/.tile.png'	\
--exclude='*/*.css'	\
--exclude='*/?.html'	\
--exclude='*/??.html'	\
--exclude='*/???.html'	\
--exclude='*/????.html'	\
--exclude='*/.indextemplate2.html'	\
--exclude='*/.slidetemplate2.html'	\
--exclude='*/.thumb_*'	\
--exclude='*/.tile.png'	\
--exclude='*.css'	\
--exclude='?.html'	\
--exclude='??.html'	\
--exclude='???.html'	\
--exclude='????.html'	\
--exclude='.indextemplate2.html'	\
--exclude='.slidetemplate2.html'	\
--exclude='.thumb_*'	\
--exclude='.tile.png'	\
  divservi@box6537.bluehost.com:~/crystalfaeries.net/imgs/{.??,}*	\
				~/crystalfaeries.net/imgs/		\
|	grep -v "failed to set times on"				\
>>				~/crystalfaeries.net/src/.sync_fay-bluehost.imgs.txt 2>&1
echo "-------	imgs	-------"	>> ~/.sync_fay-bluehost.txt
echo -n "SSHleeping	${SSHleep}: "	>> ~/.sync_fay-bluehost.txt
	uptime	>> ~/.sync_fay-bluehost.txt
	sleep	${SSHleep}	# BLUEHOST RATE-LIMITS SSH CONNECTIONS :-(

#######	#######	#######	#######	#######	#######	#######	#######	#######	#######
# FOLD THE clipart (sans the igal2 thumbnails for web access) INTO imgs HIERARCHY
	echo "	  `now`"	>> ~/.sync_fay-bluehost.txt
echo "=======	clipart	======="	>> ~/.sync_fay-bluehost.txt
rsync	-auvH	\
--exclude='*.css'	\
--exclude='?.html'	\
--exclude='??.html'	\
--exclude='???.html'	\
--exclude='????.html'	\
--exclude='.indextemplate2.html'	\
--exclude='.slidetemplate2.html'	\
--exclude='.thumb_*'	\
--exclude='.tile.png'	\
divservi@box6537.bluehost.com:~/crystalfaeries.net/clipart	\
			      ~/crystalfaeries.net/imgs/	\
|	 grep -v "failed to set times on"			\
> 	      ~/crystalfaeries.net/src/.sync_fay-bluehost.clipart.txt 2>&1
echo "-------	clipart	-------"	>> ~/.sync_fay-bluehost.txt
echo -n "SSHleeping	${SSHleep}: "	>> ~/.sync_fay-bluehost.txt
	uptime	>> ~/.sync_fay-bluehost.txt
	sleep	${SSHleep}	# BLUEHOST RATE-LIMITS SSH CONNECTIONS :-(

# WHAT clipart EXISTS ONLY ON fay?
	echo "	  `now`"	>> ~/.sync_fay-bluehost.txt
rsync	-auvH --dry-run	--delete \
--exclude='*.css'	\
--exclude='?.html'	\
--exclude='??.html'	\
--exclude='???.html'	\
--exclude='????.html'	\
--exclude='.indextemplate2.html'	\
--exclude='.slidetemplate2.html'	\
--exclude='.thumb_*'	\
--exclude='.tile.png'	\
divservi@box6537.bluehost.com:~/crystalfaeries.net/clipart	\
			      ~/crystalfaeries.net/imgs/	\
|	 grep -v "failed to set times on"			\
>>			      ~/crystalfaeries.net/src/.sync_fay-bluehost.clipart.txt 2>&1
echo "-------	clipart	-------"	>> ~/.sync_fay-bluehost.txt
echo -n "SSHleeping	${SSHleep}: "	>> ~/.sync_fay-bluehost.txt
	uptime	>> ~/.sync_fay-bluehost.txt
	sleep	${SSHleep}	# BLUEHOST RATE-LIMITS SSH CONNECTIONS :-(

	echo "	  `now`"	>> ~/.sync_fay-bluehost.txt
echo "-------	box6537.bluehost.com	<----->	fay.crystalfaeries.net	-------"	>> ~/.sync_fay-bluehost.txt

# find the files unique to fay:
grep deleting	~/crystalfaeries.net/src/.sync_fay-bluehost.*.txt	\
	2>>	~/.sync_fay-bluehost.txt				\
	|	sed 's/^.* /galleries\//'				\
	2>>	~/.sync_fay-bluehost.txt				\
	>	~/crystalfaeries.net/src/.fay.unique.txt

# expunge files "deleted" by zeroing
cd
find crystalfaeries.net/posix/bin/ download/ video/ audio/ galleries/	\
	-type f								\
	-size 0								\
	-exec rm {}							\;

# force permissions on box6537.bluehost.com ELSE WEBSITE IS INACCESSIBLE
ssh -tC divservi@box6537.bluehost.com	\
	"cd ~/crystalfaeries.net/ || exit -1; find . -type d \! -perm 755 -print -exec chmod 755 {} \;; find . \( -path posix/bin -prune \) -o \( -type f \! -perm 644 -print -exec chmod 644 {} \; \) ; find posix/bin -type f \! -name '*.txt' -exec chmod 755 {} \;; find posix/bin -name '*.txt' -exec chmod 644 {} \;"	\
				>> ~/.sync_fay-bluehost.txt 2>&1
# NOTE TO SELF - BE VEWWY CAREFUL NOT TO MAKE SCRIPTS UNEXECUTABLE!

# force permissions on fay
cd ~/crystalfaeries.net/	>> ~/.sync_fay-bluehost.txt 2>&1 || exit -1
find . -type d \! -perm 755 -print	-exec chmod 755 {} \; >> ~/.sync_fay-bluehost.txt 2>&1
find . \( -path posix/bin -prune \) -o \( -type f \! -perm 644 -print	-exec chmod 644 {} \; \) >> ~/.sync_fay-bluehost.txt 2>&1
find posix/bin -name '*.txt' \! -perm 644 -exec chmod 644 {} \; >> ~/.sync_fay-bluehost.txt 2>&1
find posix/bin -type f \! -name '*.txt' \! -perm 755	-exec chmod 755 {} \; >> ~/.sync_fay-bluehost.txt 2>&1
# NOTE TO SELF - BE VEWWY CAREFUL NOT TO MAKE SCRIPTS UNEXECUTABLE!
ssh -tCM divservi@box6537.bluehost.com	\
'chmod o-rwx ~/crystalfaeries.net/{clairvoyance,celeste,linkdoc,src}/'
 chmod o-rwx ~/crystalfaeries.net/{clairvoyance,celeste,linkdoc,src}/

# kill the tail process watching the logs
 kill %1			2>> ~/.sync_fay-bluehost.txt	||\
pkill tail			2>> ~/.sync_fay-bluehost.txt	# try no bother
echo "box6537.bluehost.com	<----->	fay.crystalfaeries.net"	>&2
exit	$?			2>> ~/.sync_fay-bluehost.txt	# try no bother

