#!/bin/bash
#                       /usr/local/bin/journal_images
# https://crystalfaeries.net/posix/bin/journal_images
# celeste:crystalfaery JOURNAL_IMAGES 2021-05-14 01:23:35+00:00
# Generate image reference templates for blogs' journal images,
# which we then manually edit into the correct location within the source file,
# as we re{name,file} the image from the journal input queue into the /imgs/ tree,
# and maybe do some tree trimming or image cropping and enhancement :-)
# If no list of target files provided, process all blog source files,
# ignoring the todo lists (which are the "dot" files in same directory),
# resulting in our iterating through each image currently referenced by blog source code.
# i.e. it's time to sort these things and update references to them :-)
# We do the math to scale the display of the images to fit an article
# displayed on a 1024x768 screen, resulting in an image display size default
# of at least 1/4 of a VGA screen, so standard HTML paragraphs can flow around them.
# If running this does nothing, it means you have moved ALL images the BLOG REFERENCES
# out of the journal folder (presumably cropping and balancing) and renamed into /imgs/ hierarchy.
# updating the source markup.txt files references to those images, so that
# anything left in the journal directory is not yet published, i.e. it's a todo input queue :-)
let help=19	# this line#-1

# Configuration:	(yeah, we COULD accept command-line options beyond coded default :-)
let max_width=512					# scaled for display max landscape width
let max_height=384					# scaled for display max portrait height
identify=/usr/bin/identify				# where the ImageMagick tool?
      journal_files="/imgs/journal/"			# where the journal image files arrive (from camera)
       source_files="$HOME/crystalfaeries.net/src/"	# where the source markup files live
cd "${journal_files}"					# where  we do  our work

# Computation:
case $# in
0)	# no arguments provided, so process all source files
	targets="${source_files}"*.txt
	;;
*)	# aha, we have at least one argument
	case "${1}" in
	--version | -v)
		head -n 4 $0 | tail -n 1	1>&2
		exit
		;;
	--help | -h)
		head -n $help $0		1>&2
		exit
		;;
	*)	# process only the specified files
		targets="$@"
		;;
	esac
	;;
esac
for source_file in "${targets}"
do	# Iterate the source files referencing the journal images:
	for image in $(grep "${journal_files}" "${source_file}" | sed 's/^.*\///;s/".*$//' | uniq)
	do	# Iterate the journal images:
		let  width=$("${identify}" "${image}" | cut -d\  -f3 | cut -dx -f1) || let  width=$max_width
		let height=$("${identify}" "${image}" | cut -d\  -f3 | cut -dx -f2) || let height=$max_height
		while [ $width -gt $max_width ]
		do	# scale down by factor of 2 as necessary to fit page with minimum artifacts
			let new_width=$(echo	"scale=0; ($width)	/ (2)"  | bc -l)
			let new_height=$(echo	"scale=0; ($height)	/ (2)"  | bc -l)
			let width=$new_width
			let height=$new_height
		done
		while [ $height -gt $max_height ]
		do	# scale down by factor of 2 as necessary to fit page with minimum artifacts
			let new_width=$(echo	"scale=0; ($width)	/ (2)"  | bc -l)
			let new_height=$(echo	"scale=0; ($height)	/ (2)"  | bc -l)
			let width=$new_width
			let height=$new_height
		done
		echo	'<!-- '					>> "${source_file}"
		echo -n	'<a	href="'				>> "${source_file}"
		echo -n	"${journal_files}${image}"		>> "${source_file}"
		echo	'"'					>> "${source_file}"
		echo -n	'><img	src="'				>> "${source_file}"
		echo -n	"${journal_files}${image}"		>> "${source_file}"
		echo	'"'					>> "${source_file}"
		echo -n	'	alt="['				>> "${source_file}"
		echo -n	"$(echo ${image}						\
	|	sed 's/^.*\///;s/\.jpg$//;s/\.png//')"		>> "${source_file}"
		echo	']"'					>> "${source_file}"
		echo -n	'	align="right" width="'		>> "${source_file}"
		echo -n "${width}"				>> "${source_file}"
		echo -n '" height="'				>> "${source_file}"
		echo -n "${height}"				>> "${source_file}"
		echo	'"></a>'				>> "${source_file}"
		echo	' -->'					>> "${source_file}"
	done
done
exit	$?

