#!/bin/bash
#                       /usr/local/bin/galleries
# https://crystalfaeries.net/posix/bin/galleries
# celeste:crystalfaery GALLERIES 2020-09-15 04:04:00+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}
# New images tossed into /imgs/* should get imagescaped into /imgs/*/{landscape,portrait}
# New  directories  to  be  processed  must  be  added  to  /imgs/.include (paranoia vs manual creations)

echo	'IMAGES BEGIN' 2>> "${HOME}"/.galleries.log 1>&2
cd -P "${HOME}"/crystalfaeries.net/imgs/										\
	2>> "${HOME}"/.galleries.log 1>&2 || exit -17	# Error: 239 Image Directory Permissions Error
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 -22	# Error: 234 Require writeable log file
else	# have new image file in top directory
	echo "imagescape: `pwd`" 2>> "${HOME}"/.galleries.log 1>&2		|| exit -18	# Error: 238 Require writeable log file
	imagescape . 2>> "${HOME}"/.galleries.log 1>&2			#	|| exit -19	# Error: 237 igal2 /imgs/HEADER.html
#	cd landscape	&& /usr/bin/igal2 2>> "${HOME}"/.galleries.log 1>&2 #	|| exit -25	# Error: 231 Image Gallery Error
#	cd portrait	&& /usr/bin/igal2 2>> "${HOME}"/.galleries.log 1>&2 #	|| exit -25	# Error: 231 Image Gallery Error
fi

echo	'IMAGESCAPE BEGIN' 2>> "${HOME}"/.galleries.log 1>&2
#	DO NOT alter these hand-built directories: "${HOME}"/crystalfaeries.net/imgs/{bettie+ets,coins,puthg}
#	NOTE the imgs/coins directory is entirely manually managed for /fae/lawful_money.html
#	DO NOT imagescape these directories (see also: "${HOME}"/crystalfaeries.net/imgs/.include):
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 -23	# Error: 233 Require writeable log file
	else
		echo "index being updated: `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -24	# Error: 232 Require writeable log file

		# remove the old index files as any file addition or deletion shifts gallery entries
#		find . -maxdepth 1	-iname	'*.html' 						\
#		\!			-name	HEADER.html 						\
#		\!			-name	README.html 						\
#		\!			-name	.indextemplate2.html 					\
#		\!			-name	.slidetemplate2.html 					\
#					-exec	rm {} \;						\
#		2>> "${HOME}"/.galleries.log 1>&2

		# 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 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 #	|| exit -25	# Error: 231 Image Gallery Error

		# prepend our HEADER in front of igal2's index and postpend README, leaving index.html as newer than .
		cat {HEADER,index,"${HOME}"/crystalfaeries.net/README}.html >				\
						/tmp/$$.html						\
		2>> "${HOME}"/.galleries.log			# Do we ALWAYS HAVE an HEADER file?
		cat				/tmp/$$.html >	index.html				\
		2>> "${HOME}"/.galleries.log	|| exit -26	# Error: 230 Image Gallery Error
		echo "index was updated in `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -27	# Error: 229 Require writeable log file
	fi
done

#	DO imagescape these directories:
echo	'IMAGESCAPE INCLUDED DIRECTORIES BEGIN'		2>> "${HOME}"/.galleries.log 1>&2

for d in	$(cat "${HOME}"/crystalfaeries.net/imgs/.include)
do
	cd -P	      "${HOME}"/crystalfaeries.net/imgs/"${d}"	2>> "${HOME}"/.galleries.log 1>&2	# NOTE DIRECTORIES RELATIVE TO imgs directory

	# 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 -23	# Error: 233 Require writeable log file
		continue	# loop
	else
		echo "index being updated: `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -24	# Error: 232 Require writeable log file

		# remove the old index files as any file addition or deletion shifts gallery entries
		# find . -maxdepth 1 -iname '*.html' 							\
		# \!	-name	HEADER.html 								\
		# \!	-name	README.html 								\
		# \!	-name	.indextemplate2.html 							\
		# \!	-name	.slidetemplate2.html 							\
		# -exec	rm {} \;									\
		# 2>> "${HOME}"/.galleries.log 1>&2

		# 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		# 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 -25	# Error: 231

				# remove igal2 index pages
				rm ?.html ??.html ???.html ????.html	2>> "${HOME}"/.galleries.log 1>&2	# || exit -25	# Error: 231

				# remove all thumbnails as obsolete or orphanned
				rm .thumb_*				2>> "${HOME}"/.galleries.log 1>&2	# || exit -25	# Error: 231

				# generate the new gallery index
				/usr/bin/igal2				2>> "${HOME}"/.galleries.log 1>&2	# || exit -25	# Error: 231

				popd					2>> "${HOME}"/.galleries.log 1>&2	|| exit -25	# Error: 231
			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 -25	# Error: 231 Image Gallery Error
	    if [ -s index.html ]
	    then
		# prepend our HEADER in front of igal2's index leaving index.html as newer than .
		cat {HEADER,index,"${HOME}"/crystalfaeries.net/README}.html >	/tmp/$$.html		\
		2>> "${HOME}"/.galleries.log			# Do we ALWAYS HAVE an HEADER file?
		cat	/tmp/$$.html	>	index.html						\
		2>> "${HOME}"/.galleries.log	|| exit -26	# Error: 230 Image Gallery Error
		echo "index was updated in `pwd`" 2>> "${HOME}"/.galleries.log 1>&2	|| exit -27	# Error: 229 Require writeable log file
	    else
		rm index.html	# 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 -24	# Error: 232 Require writeable log file

		# remove the old index files
		find . -maxdepth 1 -iname '*.html' -o -iname '*.css' -o -iname '.thumb_*'		\
		\!	-name	HEADER.html 								\
		\!	-name	README.html 								\
		-exec	rm {} \;									\
		2>> "${HOME}"/.galleries.log 1>&2

		# imagescape the directory
		imagescape . 2>>"${HOME}"/.galleries.log		# 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 -25	# Error: 231

				# remove igal2 index pages
				rm ?.html ??.html ???.html ????.html	2>> "${HOME}"/.galleries.log 1>&2	# || exit -25	# Error: 231

				# remove all thumbnails as obsolete or orphanned
				rm .thumb_*				2>> "${HOME}"/.galleries.log 1>&2	# || exit -25	# Error: 231

				# generate the new gallery index
				/usr/bin/igal2				2>> "${HOME}"/.galleries.log 1>&2	# || exit -25	# Error: 231

				popd					2>> "${HOME}"/.galleries.log 1>&2	|| exit -25	# Error: 231
			fi
		done

    fi		# end no index.html
done

# 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 -45	# Error: 211
for i in {1..9}
do
	fdedupe -f											\
	2>> "${HOME}"/.galleries.log 1>&2 || exit -53	# Error: 203
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
echo "################################################################################################"	\
	2>> "${HOME}"/.galleries.log 1>&2	|| exit -1	# Error: 255 Require writeable log file
exit $?

