#!/bin/bash
#                       /usr/local/bin/galleries
# https://crystalfaeries.net/posix/bin/galleries
# celeste:crystalfaery GALLERIES 2021-05-02 03:47:05+00:00
# This is a blog maintenance script in support of /usr/local/bin/blog
# It updates the directories imgs and galleries
# New images tossed into /imgs   should get imagescaped into  /imgs/{landscape,portrait} NOT LEFT AT TOP LEVEL
# New images tossed into /imgs/* should get imagescaped into /imgs/*/{landscape,portrait} WITH COPIES AT TOP
# New  directories  to  be  processed  must  be  added  to  /imgs/.include (paranoia vs manual creations)
# NOTE: we are ignoring the /imgs/.exclude file listing all dirs not in /imgs/.include
let help=10 # 1 less than line #

case $# in
0)	# No options nor arguments
	: # fall through
	;;
*)	# oh, something to process
    until [[ $# -eq 0 ]]
    do
	case "${1}" in
    -h | --help )
	head -n $help $0
	exit
	;;
    -v | --version )
	head -n 4 $0 | tail -n 1
	exit
	;;
    * )
	echo "$0 does not cognize $@"
	exit 1
	;;
	esac
	shift
    done
	;;
esac

echo	'EXPUNGE DELETED'			2>> "${HOME}"/.galleries.log 1>&2 || exit -1
find	"${HOME}"/crystalfaeries.net/imgs/	\
	"${HOME}"/crystalfaeries.net/galleries/	\
	-type f -size 0 -print -exec rm {} \;	2>> "${HOME}"/.galleries.log 1>&2
echo	'EXPUNGE COMPLETED'			2>> "${HOME}"/.galleries.log 1>&2 || exit -1

echo	'IMAGESCAPE BEGIN'			2>> "${HOME}"/.galleries.log 1>&2 || exit -2
cd -P	"${HOME}"/crystalfaeries.net/imgs/	2>> "${HOME}"/.galleries.log 1>&2 || exit -2
if [ 0 -eq $(find . -maxdepth 1 -iname '*.gif' -o -iname '*.jpg' -o -iname '*.png'	\
						2>> "${HOME}"/.galleries.log	|	\
	/usr/bin/wc | /bin/sed 's/^ *//g;s/ .*$//g') ]
then	# ok
	echo "FILING IS CURRENT IN: `pwd`"	2>> "${HOME}"/.galleries.log 1>&2 || exit -3
else	# have new image file in top directory to sort
	echo "IMAGESCAPE: `pwd`"		2>> "${HOME}"/.galleries.log 1>&2 || exit -4
	imagescape -d .				2>> "${HOME}"/.galleries.log 1>&2 || exit $?
fi

for d in	"${HOME}"/crystalfaeries.net/imgs/{landscape,portrait}/
do
	cd -P "${d}"	# note we expect ABSOLUTE PATHS!

	# do we have updates in this directory? (including deletions)
	if [ 0 -eq $(find . -newer index.html 2> /dev/null | /usr/bin/wc | /bin/sed 's/^ *//g;s/ .*$//g' 2>> "${HOME}"/.galleries.log) ]
	then
		echo "INDEX IS CURRENT IN: `pwd`"			2>> "${HOME}"/.galleries.log 1>&2	|| exit -6
	else
		echo "INDEX BEING UPDATED: `pwd`"			2>> "${HOME}"/.galleries.log 1>&2	|| exit -7
		# remove igal2 index pages + thumbnails as obsolete or orphanned
		rm .thumb_* ?.html ??.html ???.html ????.html index.html	2>> "${HOME}"/.galleries.log 1>&2

		# generate new thumbnails and index files
		cp	/dev/null	index.html	# no output unless image files present
		/usr/bin/igal2						2>> "${HOME}"/.galleries.log 1>&2

		# prepend our HEADER in front of igal2's index and postpend README, leaving index.html as newer than .
		cat {HEADER,index,README}.html	>	/tmp/$$.html			2>> "${HOME}"/.galleries.log	|| exit -10
		cat					/tmp/$$.html	> index.html	2>> "${HOME}"/.galleries.log	|| exit -11
		echo "INDEX WAS UPDATED IN `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -12
	fi
done

#	DO imagescape these directories:
echo	'IMAGESCAPE .includeD DIRECTORIES BEGIN'			2>> "${HOME}"/.galleries.log 1>&2	|| exit -13
for d in	$(cat "${HOME}"/crystalfaeries.net/imgs/.include)
do
	mkdir	-p      "${HOME}"/crystalfaeries.net/imgs/"${d}"	2>> "${HOME}"/.galleries.log 1>&2	|| exit -14
	cd	-P      "${HOME}"/crystalfaeries.net/imgs/"${d}"	2>> "${HOME}"/.galleries.log 1>&2	|| exit -14

	# do we have updates in this directory? (including deletions)
    if [ -s index.html ]
    then	# we have a non-deleted index.html
	if [ 0 -eq $(find . -newer index.html 2>>"${HOME}"/.galleries.log|/usr/bin/wc|/bin/sed 's/^ *//g;s/ .*$//g' 2>>"${HOME}"/.galleries.log) ]
	then
		echo "index is current in: `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -15
		continue	# loop
	else
		echo "INDEX BEING UPDATED: `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -16

		# remove igal2 index pages
		rm .thumb_* ?.html ??.html ???.html ????.html index.html	2>> "${HOME}"/.galleries.log 1>&2

		# imagescape the directory
		imagescape .						2>> "${HOME}"/.galleries.log 1>&2	|| exit $?
		cp /dev/null index.html					2>> "${HOME}"/.galleries.log 1>&2	|| exit -16
		/usr/bin/igal2						2>> "${HOME}"/.galleries.log 1>&2	|| exit -17
		cat HEADER.html index.html README.html > /tmp/$$.txt	2>> "${HOME}"/.galleries.log		|| exit -17
		cat /tmp/$$.txt > index.html				2>> "${HOME}"/.galleries.log 		|| exit -17

		# update the subdirectories
		for d in landscape portrait
		do
			if [ -d $d ]
			then
				pushd $d				2>> "${HOME}"/.galleries.log 1>&2	|| exit -18

				# remove igal2 index pages
				rm .thumb_* ?.html ??.html ???.html ????.html	2>> "${HOME}"/.galleries.log 1>&2

				# generate the new gallery index
				cp /dev/null index.html	# no output unless images exist
				/usr/bin/igal2				2>> "${HOME}"/.galleries.log 1>&2
				cat HEADER.html index.html README.html > /tmp/$$.txt			2>> "${HOME}"/.galleries.log
				cat					/tmp/$$.txt > index.html	2>> "${HOME}"/.galleries.log
				popd					2>> "${HOME}"/.galleries.log 1>&2	|| exit -21
			fi
		done

		# generate new thumbnails and index files
		cp	/dev/null	index.html	2>>	"${HOME}"/.galleries.log	1>&2	# no output unless image files present
		/usr/bin/igal2	2>> "${HOME}"/.galleries.log 1>&2 || exit -22
	    if [ -s index.html ]
	    then
		# prepend our HEADER in front of igal2's index leaving index.html as newer than .
		cat {HEADER,index,README}.html >	/tmp/$$.html				2>> "${HOME}"/.galleries.log
		cat					/tmp/$$.html	>	index.html	2>> "${HOME}"/.galleries.log	|| exit -23
		echo "index was updated in `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -24
	    else
		rm index.html	2>> "${HOME}"/.galleries.log 1>&2 # empty file is in the way of apache serving {HEADER,README}.html if they exist
	    fi
	fi
    else	# no index.html
		echo "images being updated: `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -25

		# remove the old index files
		# remove igal2 index pages
		rm ?.html ??.html ???.html ????.html	2>> "${HOME}"/.galleries.log 1>&2

		# remove all thumbnails as obsolete or orphanned
		rm .thumb_*				2>> "${HOME}"/.galleries.log 1>&2

		# imagescape the directory
		imagescape . 2>>"${HOME}"/.galleries.log	|| exit $?	# hardlink in subdirectories from un-sorted files

		# update the subdirectories
		for d in landscape portrait
		do
			if [ -d $d ]
			then
				pushd $d				2>> "${HOME}"/.galleries.log 1>&2	|| exit -28

				# remove igal2 index pages
				rm ?.html ??.html ???.html ????.html	2>> "${HOME}"/.galleries.log 1>&2

				# remove all thumbnails as obsolete or orphanned
				rm .thumb_*				2>> "${HOME}"/.galleries.log 1>&2

				# generate the new gallery index
				cp /dev/null index.html
				/usr/bin/igal2				2>> "${HOME}"/.galleries.log 1>&2	|| exit -31
	    if [ -s index.html ]
	    then
		# prepend our HEADER in front of igal2's index leaving index.html as newer than .
		cat {HEADER,index,README}.html >	/tmp/$$.html	2>> "${HOME}"/.galleries.log
		cat	/tmp/$$.html	>	index.html		2>> "${HOME}"/.galleries.log	|| exit -23
		echo "index was updated in `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -24
	    else
		rm index.html	2>> "${HOME}"/.galleries.log 1>&2 # empty file is in the way of apache serving {HEADER,README}.html if they exist
	    fi

				popd					2>> "${HOME}"/.galleries.log 1>&2	|| exit -32
			fi
		done

    fi		# end no index.html
done

# update galleries directory we do not publish but fdedupe with imgs
	cd -P	      "${HOME}"/crystalfaeries.net/galleries/	2>> "${HOME}"/.galleries.log 1>&2

	# do we have updates in this directory? (including deletions)
    if [ -r index.html ]
    then	# we have an index.html
	if [ 0 -eq $(find . -newer index.html 2>> "${HOME}"/.galleries.log | /usr/bin/wc | /bin/sed 's/^ *//g;s/ .*$//g' 2>> "${HOME}"/.galleries.log) ]
	then
		echo "index is current in: `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -33
	else
		echo "index being updated: `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -34

		# remove igal2 index pages
		rm ?.html ??.html ???.html ????.html			2>> "${HOME}"/.galleries.log 1>&2

		# remove all thumbnails as obsolete or orphanned
		rm .thumb_*						 2>> "${HOME}"/.galleries.log 1>&2

		# imagescape the directory
		imagescape . 2>>"${HOME}"/.galleries.log	|| exit $?	# hardlink in subdirectories from un-sorted files

		# update the subdirectories
		for d in landscape portrait
		do
			if [ -d $d ]
			then
				pushd $d				2>> "${HOME}"/.galleries.log 1>&2	|| exit -35

				# remove igal2 index pages
				rm ?.html ??.html ???.html ????.html	2>> "${HOME}"/.galleries.log 1>&2

				# remove all thumbnails as obsolete or orphanned
				rm .thumb_*				2>> "${HOME}"/.galleries.log 1>&2

				# generate the new gallery index
				cp /dev/null index.html	# no output unless images exist
				/usr/bin/igal2				2>> "${HOME}"/.galleries.log 1>&2	|| exit -38
	    if [ -s index.html ]
	    then
		# prepend our HEADER in front of igal2's index leaving index.html as newer than .
		cat {HEADER,index,README}.html >	/tmp/$$.html	2>> "${HOME}"/.galleries.log
		cat	/tmp/$$.html	>	index.html		2>> "${HOME}"/.galleries.log	|| exit -38
		echo "index was updated in `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -24
	    else
		rm index.html	2>> "${HOME}"/.galleries.log 1>&2 # empty file is in the way of apache serving {HEADER,README}.html if they exist
	    fi

				popd					2>> "${HOME}"/.galleries.log 1>&2	|| exit -39
			fi
		done

		# generate new thumbnails and index files
		cp	/dev/null	index.html	2>>	"${HOME}"/.galleries.log	1>&2	# no output unless image files present
		/usr/bin/igal2							2>> "${HOME}"/.galleries.log 1>&2 || exit -40
	    if [ -s index.html ]
	    then
		# prepend our HEADER in front of igal2's index leaving index.html as newer than .
		cat {HEADER,index,README}.html >	/tmp/$$.html	2>> "${HOME}"/.galleries.log
		cat	/tmp/$$.html	>	index.html					2>> "${HOME}"/.galleries.log	|| exit -41
		echo "index was updated in `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -42
	    else
		rm index.html	2>> "${HOME}"/.galleries.log 1>&2	# empty file is in the way of apache serving {HEADER,README}.html if they exist
	    fi
	fi
    else	# no index.html
		echo "images being updated: `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -43

		# remove the old index files
		# remove igal2 index pages
		rm ?.html ??.html ???.html ????.html	2>> "${HOME}"/.galleries.log 1>&2

		# remove all thumbnails as obsolete or orphanned
		rm .thumb_*				2>> "${HOME}"/.galleries.log 1>&2


		# imagescape the directory
		imagescape . 2>>"${HOME}"/.galleries.log	|| exit $?	# hardlink in subdirectories from un-sorted files

		cp	/dev/null	index.html	2>>	"${HOME}"/.galleries.log	1>&2	# no output unless image files present
		/usr/bin/igal2							2>> "${HOME}"/.galleries.log 1>&2 || exit -40
	    if [ -s index.html ]
	    then
		# prepend our HEADER in front of igal2's index leaving index.html as newer than .
		cat {HEADER,index,README}.html >	/tmp/$$.html	2>> "${HOME}"/.galleries.log
		cat	/tmp/$$.html	>	index.html					2>> "${HOME}"/.galleries.log	|| exit -41
		echo "index was updated in `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -42
	    else
		rm index.html	2>> "${HOME}"/.galleries.log 1>&2	# empty file is in the way of apache serving {HEADER,README}.html if they exist
	    fi
		# update the subdirectories
		for d in landscape portrait
		do
			if [ -d $d ]
			then
				pushd $d				2>> "${HOME}"/.galleries.log 1>&2	|| exit -46

				# remove igal2 index pages
				rm ?.html ??.html ???.html ????.html	2>> "${HOME}"/.galleries.log 1>&2

				# remove all thumbnails as obsolete or orphanned
				rm .thumb_*				2>> "${HOME}"/.galleries.log 1>&2

				# generate the new gallery index
				cp /dev/null index.html
				/usr/bin/igal2				2>> "${HOME}"/.galleries.log 1>&2	|| exit -49
	    if [ -s index.html ]
	    then
		# prepend our HEADER in front of igal2's index leaving index.html as newer than .
		cat {HEADER,index,README}.html >	/tmp/$$.html	2>> "${HOME}"/.galleries.log
		cat	/tmp/$$.html	>	index.html					2>> "${HOME}"/.galleries.log	|| exit -41
		echo "index was updated in `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -42
	    else
		rm index.html	2>> "${HOME}"/.galleries.log 1>&2	# empty file is in the way of apache serving {HEADER,README}.html if they exist
	    fi

				popd					2>> "${HOME}"/.galleries.log 1>&2	|| exit -50
			fi
		done

    fi		# end no index.html


# update astrolog gallery
# /usr/local/bin/astrolog -g	2>> "${HOME}"/.galleries.log 1>&2 || exit $?	# Error: astrolog_gallery return code

# create the "other" images gallery
other_images			2>> "${HOME}"/.galleries.log 1>&2 || exit $?	# Error: other_images return code

# fdedupe the images tree
cd -P "${HOME}"/crystalfaeries.net/										\
	2>> "${HOME}"/.galleries.log 1>&2 || exit -51
for i in {1..9}
do
	fdedupe -f											\
	2>> "${HOME}"/.galleries.log 1>&2 || exit -52
done

# echo 'UPLOAD IMGS+documents	TO SERVER'				2>> "${HOME}"/.galleries.log 1>&2
pushd	"${HOME}"/crystalfaeries.net/documents				2>> "${HOME}"/.galleries.log 1>&2
tree -Fht	-T "crystalfaeries documents (most recent on top)"	\
		-H http://crystalfaeries.net/documents	> index.html	2>> "${HOME}"/.galleries.log 
popd									2>> "${HOME}"/.galleries.log 1>&2

 echo   'rsync -auvzH	"${HOME}"/crystalfaeries.net/{documents,imgs}	\
	 		`my_twin`:~/crystalfaeries.net'			2>> "${HOME}"/.galleries.log 1>&2
         rsync -auvzH	"${HOME}"/crystalfaeries.net/{documents,imgs}	\
		 	`my_twin`:~/crystalfaeries.net			2>> "${HOME}"/.galleries.log 1>&2

# done updating galleries
echo "`now` GALLERIES END $?"										\
	2>> "${HOME}"/.galleries.log 1>&2	|| exit -53
echo "################################################################################################"	\
	2>> "${HOME}"/.galleries.log 1>&2	|| exit -54
exit $?

