#!/bin/bash
#                       /usr/local/bin/other_images
# https://crystalfaeries.net/posix/bin/other_images
# celeste:crystalfaery OTHER_IMAGES 2021-05-14 02:34:26+00:00
# create in crystalfaeries.net/imgs/other/ a synthetic directory
# of all the images we intend to publish, which live outside the
# crystalfaeries.net/imgs/ hierarchy, implemented via symlinks.
# this should be called from /usr/local/bin/galleries :-)

# Work in the target directory
mkdir -p	"${HOME}"/crystalfaeries.net/imgs/other/	2>> "${HOME}"/.galleries.log ||	exit $?
cd    -P	"${HOME}"/crystalfaeries.net/imgs/other/	2>> "${HOME}"/.galleries.log ||	exit $?

# remove all old symbolic links:
# find	./ -type l -exec									 rm {} \;	2>> "${HOME}"/.galleries.log
# remove broken  symbolic links:
find	./ -type l -exec file {} \; | grep ': broken symbolic link to ' | sed 's/:.*$//' | xargs delete		2> "${HOME}"/.galleries.log

# Find the images outside /imgs/ hierarchy and link to them
for f in $(	find "${HOME}"/crystalfaeries.net/ -mindepth 2	\
\( -name '*.gif'	-print \) -o				\
\( -name '*.jpg'	-print \) -o				\
\( -name '*.png'	-print \) -o				\
\( -name '*.svg'	-print \) -o				\
\( -name '*.webp'	-print \)				\
				2> "${HOME}"/.galleries.log	\
|	sed	's/\/home\/celeste\/crystalfaeries.net//'	\
| grep -v "/astrolog/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].[0-9][0-9]-[0-9][0-9]."	\
| grep -v /astrolog/astro.png					\
| grep -v /astrolog/celeste.2017-06-23.birthday.png		\
| grep -v /astrolog/full_moon.png				\
| grep -v /astrolog/moon.png					\
| grep -v /astrolog/new_moon.png				\
| grep -v /astrolog/planetary_ascension_portal.png		\
| grep -v /celeste/						\
| grep -v /fae/							\
| grep -v /galleries/						\
| grep -v /icons/						\
| grep -v /imgs/						\
| grep -v .thumb_						\
| grep -v .tile.png						\
| grep -v /documents/directory_does_not_exist/			\
| sort -u							)
do
	ln -s "../../${f}"	2>> "${HOME}"/.galleries.log	# make symlinks to actual files
done

# Build the gallery
cp /dev/null	index.html
igal2									2>> "${HOME}"/.galleries.log 1>&2	# make the gallery

# Add the HEADER and README
cat HEADER.html	index.html ../../README.html >	/tmp/$$.index.html		2>> "${HOME}"/.galleries.log
mv						/tmp/$$.index.html index.html	2>> "${HOME}"/.galleries.log

exit	$?	# Pau for now
