#!/bin/bash
#                       /usr/local/bin/preview
# https://crystalfaeries.net/posix/bin/preview
# celeste:crystalfaery PREVIEW 2017-07-15 16:15:27+00:00
# This script is used to preview in our browser
# all blog entries, which will become auto-published in the future
# via the blog script, (which is a wrapper around chronicle),
# when invoked without arguments. With arguments,
# it will preview just the specified files.
# The configured file format of source code is:
# /home/crystalfaeries.net/src/.2016-08-26.fae.txt
# 1st Line:		"Date:	2016-08-26 10:00:00+00:00"
# 2nd Line: optional	"Expiry:	1500135412"
# next Line:		empty
# remaining lines:	simple HTML code
# The output is normally directed to /home/crystalfaeries.net/fae/
# We shall preview into an alternate directory /home/crystalfaeries.net/fay/
# thus producing the file /home/crystalfaeries.net/fay/2016_08_26_fae.html
# TYPICAL USAGE:
# 1. When editing a file in the vi editor, when you :w
#    write the file out to disk, the editor displays the file name...
#    double click on that to get the full path into the clipboard...
#    :!preview <middle-click-to-paste-file-path> <enter>
#    generates the HTML preview of that just edited file in /home/crystalfaeries.net/fay/
#    for viewing with your browser.
# 2. When the blog script runs it invokes preview sans arguments, thereby
#    generates previews for all future articles in /home/crystalfaeries.net/fay/

rm	/home/crystalfaeries.net/fay/{.??,}*	2>/dev/null				# cleanup from previous execution(s)

if [ $# -eq 0 ]
then	# we were invoked with NO arguments, therefore default to all pending future articles
	if [ "$(find /home/crystalfaeries.net/src/.2[0-9][0-9][0-9]-* -type f \! -name '*.swp' 2>/dev/null)" == "" ]
	then
		exit 0	# nothing to do
	else
		exec $0 $(find /home/crystalfaeries.net/src/.2[0-9][0-9][0-9]-* -type f \! -name '*.swp')	# Y3K bug :-)
		exit	-1
	fi
fi

while [ $# -ne 0 ]
do	# we have another argument on command line to process:
	infile="$(basename ${1})"; shift						# RELATIVE INfile
#echo	"INFILE: $infile"								# DEBUG
	inpath="/home/crystalfaeries.net/src/${infile}"					# ABSOLUTE INpath
#echo	"INPATH: $inpath"								# DEBUG
	outfile="$(echo ${infile} | sed 's/^\.//;s/-/_/g;s/\./_/g;s/_txt$/.html/')"	# RELATIVE OUTfile
#echo	"OUTFILE: $outfile"								# DEBUG
	outpath="/home/crystalfaeries.net/fay/${outfile}"				# ABSOLUTE OUTpath
#echo	"OUTPATH: $outpath"								# DEBUG
	outurl="http://crystalfaeries.net/fay/${outfile}"				# Preview URL
#echo	"OUTURL: $outurl"								# DEBUG
	echo '<html><head><title>'							>> ${outpath}
	head -n 1 ${inpath} | sed 's/Date://;s/	*//;s/ *//;s/ .*$//'			>> ${outpath}
	echo '</title></head><body><h1><a href="/fay/">'				>> ${outpath}
	head -n 1 ${inpath} | sed 's/Date://;s/	*//;s/ *//;s/ .*$//'			>> ${outpath}
	echo '</a></h1><br>'								>> ${outpath}
	tail -n +3 ${inpath}								>> ${outpath}
	echo '</body>'									>> ${outpath}
#	firefox "${outurl}"								# PREVIEW it! (if we have DNS!)
done



syntax highlighted by Code2HTML, v. 0.9.1