#!/bin/bash
#                       /usr/local/bin/preview
# https://crystalfaeries.net/posix/bin/preview
# celeste:crystalfaery PREVIEW 2020-05-18 18:26:35+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. One of the ways to get into the vi editor with desired file is:
#    today YYYY-MM-DD (e.g. today 2020-04-05), and
#    quitting the edit session auto-previews what you were editing.
# 3. When the blog script runs it invokes preview sans arguments, thereby
#    generates previews for all future articles in $HOME/crystalfaeries.net/fay/
let	help=30	#line number within file - 1

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	$?
	else
		exec $0 $(find $HOME/crystalfaeries.net/src/.2[0-9][0-9][0-9]-* -type f \! -name '*.swp' 2>/dev/null) # Y3K bug :-)
		exit	-1	# how did you get here from the above exec?
	fi
fi

while [ $# -ne 0 ]
do	# we have another argument on command line to process:
    case "${1}" in
    --version | -v )
	head -n 4 $0 | tail -n 1
	exit $?
	;;
    --help | -h )
	head -n $help $0
	exit $?
	;;
    *)
	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>'													>> ${outpath}
	echo '<link rel="stylesheet" type="text/css" media="screen" href="$HOME/crystalfaeries.net/fae/style.css" />'	>> ${outpath}
	echo '<link rel="stylesheet" type="text/css" media="screen" href="$HOME/crystalfaeries.net/fae/vim.css"   />'	>> ${outpath}
	echo '</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}
	echo														  "${outpath}"
	firefox	"${outpath}"	< /dev/null >& /dev/null & disown %1	# preview in our browser
	;;
    esac
done

