#!/data/data/com.termux/files/usr/bin/bash
# /data/data/com.termux/files/home/bin/syncblue
# https://crystalfaeries.net/posix/bin/syncblue
# celeste:crystalfaery SYNCBLUE Thu, 06 Jul 2017 04:39:42 +0000
# syncblue is executed on mobiles' TermUX vs BlueHost server to synchronize
# the PORTION of the crystalfaeries.net website locally maintained.
# The complementary script executing chronicle on laptop "fey" is blog.

# CONFIGURATION
let sshleep=7	# Bluehost rate-limits ssh connections

# there's no place like $HOME
echo "=======	=======	======="	 > ~/.syncblue.txt 2>&1
echo "$(now) SYNCBLUE START"		>> ~/.syncblue.txt 2>&1
cd					>> ~/.syncblue.txt 2>&1	|| alert $?
echo "=======	=======	======="		>> ~/.syncblue.txt 2>&1
tail -F ~/.syncblue.txt &		# Watch us running...
sync					# Android apps crash when backgrounded

# Clean-Up null, swap, and obsolete_todo files
echo "Clean-Up"				>> ~/.syncblue.txt 2>&1
find  ~/crystalfaeries.net/ 					\
   \( -type f -size 0			-exec rm {} \; \)	\
-o \( -name '.*.swp'	-mtime +1	-exec rm {} \; \)	\
-o \( -name '.??*'	-mtime +90	-exec rm {} \; \)	\
					>> ~/.syncblue.txt 2>&1

# sync website contents not excluded for size considerations
echo "rsync -auvH ~/crystalfaeries.net divservi@box6537.bluehost.com:/home1/divservi/public_html"		>> ~/.syncblue.txt 2>&1
rsync -auvH ~/crystalfaeries.net divservi@box6537.bluehost.com:/home1/divservi/public_html			>> ~/.syncblue.txt 2>&1
sleep	${sshleep}	# Bluehost rate-limits ssh connections

echo "ssh -tC divservi@box6537.bluehost.com 'find /home1/divservi/public_html/crystalfaeries.net/ -type d \! -perm 2775 -print -exec chmod 2775 {} \;'"	>> ~/.syncblue.txt 2>&1
ssh -tC divservi@box6537.bluehost.com 'find /home1/divservi/public_html/crystalfaeries.net/ -type d \! -perm 2775 -print -exec chmod 2775 {} \;'	>> ~/.syncblue.txt 2>&1
sleep	${sshleep}	# Bluehost rate-limits ssh connections

echo "ssh -tC divservi@box6537.bluehost.com 'find /home1/divservi/public_html/crystalfaeries.net/ -type f \! -perm 664 -print -exec chmod 664 {} \;'"	>> ~/.syncblue.txt 2>&1
ssh -tC divservi@box6537.bluehost.com 'find /home1/divservi/public_html/crystalfaeries.net/ -type f \! -perm 664 -print -exec chmod 664 {} \;'	>> ~/.syncblue.txt 2>&1
sleep	${sshleep}	# Bluehost rate-limits ssh connections

echo "=======	=======	======="	>> ~/.syncblue.txt 2>&1
echo "EXCLUDING from DownLoad:"		>> ~/.syncblue.txt 2>&1
cat .to-termux.txt			>> ~/.syncblue.txt 2>&1
echo "=======	=======	======="	>> ~/.syncblue.txt 2>&1
echo "rsync -auvH --exclude-from=.to-termux.txt divservi@box6537.bluehost.com:/home1/divservi/public_html/crystalfaeries.net/{.??,}* ~/crystalfaeries.net"	>> ~/.syncblue.txt 2>&1
rsync -auvH --exclude-from=.to-termux.txt divservi@box6537.bluehost.com:/home1/divservi/public_html/crystalfaeries.net/{.??,}* ~/crystalfaeries.net	>> ~/.syncblue.txt 2>&1
sleep	${sshleep}	# Bluehost rate-limits ssh connections

# THIS SET OF DIRECTORIES ARE LOCALLY SYMLINKS:
# therefore, sync their contents not whole directories, and
# only download the txt and html files, not huge media files we have no space for.
echo "=======	=======	======="		>> ~/.syncblue.txt 2>&1
echo "UPLOADING ALL+DOWNLOADING .txt AND .html:"					\
					>> ~/.syncblue.txt 2>&1	
echo "=======	=======	======="		>> ~/.syncblue.txt 2>&1
for d in $(grep -v lost+found .to-termux.txt)
do
    echo	"=======	${d}	======="
    rsync -auvH										\
    ~/crystalfaeries.net/${d}/{.??,}*							\
    divservi@box6537.bluehost.com:~/public_html/crystalfaeries.net/${d}	# PUSH
    sleep	${sshleep}	# Bluehost rate-limits ssh connections
    rsync -auvH										\
    divservi@box6537.bluehost.com:~/public_html/crystalfaeries.net/${d}/*.{txt,html}	\
    ~/crystalfaeries.net/${d}	# PULL ONLY .txt and .html files
    sleep	${sshleep}	# Bluehost rate-limits ssh connections
done					>> ~/.syncblue.txt 2>&1

# echo "=======	=======	======="		>> ~/.syncblue.txt 2>&1
# echo "rsync CRYSTALFAERIES.NET/IMGS"	>> ~/.syncblue.txt 2>&1
# rsync -auvH --exclude='*.css' --exclude='.thumb_*' --exclude='.tile.png	'	\
# --exclude='?.html' --exclude='??.html' --exclude='???.html' --exclude='????.html'	\
# 	divservi@box6537.bluehost.com:~/public_html/crystalfaeries.net/imgs/{.??,}*	\
# 	/storage/sdcard1/Pictures/www
# sleep	${sshleep}	# Bluehost rate-limits ssh connections

echo "=======	=======	======="		>> ~/.syncblue.txt 2>&1
echo "SERVER PERMISSIONS"		>> ~/.syncblue.txt 2>&1
echo "ssh -tC divservi@box6537.bluehost.com 'find /home1/divservi/public_html/crystalfaeries.net/ -type d \! -perm 2775 -print -exec chmod 2775 {} \;'"	>> ~/.syncblue.txt 2>&1
ssh -tC divservi@box6537.bluehost.com 'find /home1/divservi/public_html/crystalfaeries.net/ -type d \! -perm 2775 -print -exec chmod 2775 {} \;'	>> ~/.syncblue.txt 2>&1
sleep	${sshleep}	# Bluehost rate-limits ssh connections

echo "ssh -tC divservi@box6537.bluehost.com 'find /home1/divservi/public_html/crystalfaeries.net/ -type f \! -perm 664 -print -exec chmod 664 {} \;'"	>> ~/.syncblue.txt 2>&1
ssh -tC divservi@box6537.bluehost.com 'find /home1/divservi/public_html/crystalfaeries.net/ -type f \! -perm 664 -print -exec chmod 664 {} \;'	>> ~/.syncblue.txt 2>&1
sleep	${sshleep}	# Bluehost rate-limits ssh connections

echo "=======	=======	======="	>> ~/.syncblue.txt 2>&1
echo "LOCAL PERMISSIONS"		>> ~/.syncblue.txt 2>&1
echo "find ~/crystalfaeries.net/ -type d \! -perm 2775 -print -exec chmod 2775 {} \;"	>> ~/.syncblue.txt 2>&1
find ~/crystalfaeries.net/ -type d \! -perm 2775 -print -exec chmod 2775 {} \;	>> ~/.syncblue.txt 2>&1
echo "find ~/crystalfaeries.net/ -type f \! -perm 664 -print -exec chmod 664 {} \;"	>> ~/.syncblue.txt 2>&1
find ~/crystalfaeries.net/ -type f \! -perm 664 -print -exec chmod 664 {} \;	>> ~/.syncblue.txt 2>&1

echo "=======	=======	======="	>> ~/.syncblue.txt 2>&1
echo "UPDATE ~/bin"			>> ~/.syncblue.txt 2>&1
echo "apt update"			>> ~/.syncblue.txt 2>&1
apt update				>> ~/.syncblue.txt 2>&1
echo "apt upgrade"			>> ~/.syncblue.txt 2>&1
apt upgrade				>> ~/.syncblue.txt 2>&1
echo "apt autoremove"			>> ~/.syncblue.txt 2>&1
apt autoremove				>> ~/.syncblue.txt 2>&1
echo ""					>> ~/.syncblue.txt 2>&1
echo "======= YOUTUBE-DL ======="	>> ~/.syncblue.txt 2>&1
python ~/bin/youtube-dl --update	>> ~/.syncblue.txt 2>&1

# Synchronize our open source scripts library:
# /usr/local/bin on a normal Linux(TM) system;
# http://crystalfaeries.net/posix/bin
# On TermUX we mix scripts and native code in one directory:
# BEWARE: neither clobber nor propagate ARM ELF executable files.
echo "=======	=======	======="		>> ~/.syncblue.txt 2>&1
echo "CALCULATE bin EXCLUSIONS"			>> ~/.syncblue.txt 2>&1
# TermUX's BusyBox erroneously tests this as dir vs link:
if [ -d bin ]
then
	ls -Flad bin				>> ~/.syncblue.txt 2>&1
else
	echo   "ln -s ../usr/bin || exit 1"	>> ~/.syncblue.txt 2>&1
		ln -s ../usr/bin || exit 1	>> ~/.syncblue.txt 2>&1
fi

# ARM ELF binary executables of TermUX
file bin/* | grep ELF | grep ARM | sed 's/:.*$//g' > .termux.bin.txt-			\
					       2>> ~/.syncblue.txt
# SymLinks
find bin/ -type l	>> .termux.bin.txt-    2>> ~/.syncblue.txt

# File Types Filter
grep /data/data/com.termux/files/usr/bin/ bin/* |&					\
grep -v "Binary file" |									\
sed 's/:.*$//' |									\
grep -v syncblue	>> .termux.bin.txt-    2>> ~/.syncblue.txt

# remainder should be portable scripts library ignoring applets
sort -u < .termux.bin.txt- 2>> ~/.syncblue.txt | grep -v bin/syncblue > .termux.bin.txt
echo 'bin/[' >> .termux.bin.txt
echo "rsync -auvH --exclude=applets --exclude-from=.termux.bin.txt divservi@box6537.bluehost.com:/home1/divservi/bin ../usr"	>> ~/.syncblue.txt 2>&1
rsync -auvH --exclude=applets --exclude-from=.termux.bin.txt divservi@box6537.bluehost.com:/home1/divservi/bin ../usr	>> ~/.syncblue.txt 2>&1
sleep	${sshleep}	# Bluehost rate-limits ssh connections

echo "rsync -auvH --exclude=applets --exclude-from=.termux.bin.txt ../usr/bin divservi@box6537.bluehost.com:/home1/divservi"	>> ~/.syncblue.txt 2>&1
rsync -auvH --exclude=applets --exclude-from=.termux.bin.txt ../usr/bin divservi@box6537.bluehost.com:/home1/divservi	>> ~/.syncblue.txt 2>&1
sleep	${sshleep}	# Bluehost rate-limits ssh connections

echo "=======	=======	======="		>> ~/.syncblue.txt 2>&1
echo "rsync -auvH ~/.syncblue.txt ~/.termux.bin.txt divservi@box6537.bluehost.com:/home1/divservi"	>> ~/.syncblue.txt 2>&1
rsync -auvH ~/.syncblue.txt ~/.termux.bin.txt divservi@box6537.bluehost.com:/home1/divservi	>> ~/.syncblue.txt 2>&1
sleep	${sshleep}	# Bluehost rate-limits ssh connections

# Log completion
echo "=======	=======	======="	>> ~/.syncblue.txt 2>&1
echo "$(now) SYNCBLUE DONE"		>> ~/.syncblue.txt 2>&1
echo "=======	=======	======="	>> ~/.syncblue.txt 2>&1
sync					# Android apps crash when backgrounded
sleep	${sshleep}	# give "tail" time to work
kill	$!	# DONE watching
exit	$?	# pau



syntax highlighted by Code2HTML, v. 0.9.1