#!/bin/tcsh -f

# Run this when you add or change entries and want them
# to be incorporated into the formatted journal.

# What format should dates be displayed in? (see man date)
set dateFmt="+%A, %B %d, %Y"


# Do you want the index in forward or reverse chronological order?
#set order=forward
set order=reverse


# NOTE Neither of these dirs should be a child of Journal:

# Where are you going to put images (relative to Journal/)?
set imsDir="../ims"

# Where do you want to keep scripts (relative to Journal/)?
set scriptsDir="/home/simon/scripts"





# The rest of this script is generic.
# (I.e., you shouldn't need to change anything below here.)



if (! -d entries) then
	echo "'entries' source directory doesn't exist."
	exit 1
endif

if (! -d parts) then
	echo "'parts' source directory doesn't exist."
	exit 1
endif

if (! -d Journal) then
	echo "Journal target directory doesn't exist.  Make it."
	exit 1
endif



echo "Searching for new entries..."

cd entries
set entries=*
cd ..

echo "There are $#entries raw entries to date..."


# It's easiest just to rebuild the index from scratch...
# We'll throw the index entries into a temp file so we
#  can stick it together with header and footer later.
#  (This also lets us reverse the order at that time if
#   we choose...):
echo -n "" > Journal/index.tmp


set previous="NONE"

foreach entry ($entries)

	set dateString=`date -d $entry "$dateFmt"`

	if ($status != 0) then
		echo "WARNING: entry '$entry' cannot be parsed as a date.  Please fix."
		continue
	endif

	set target="Journal/$entry.html"

	# The names of the previous and next html files:
	set prevFile="$previous.html"
	set nextFile="$entry.next.html"     # A symlink, yet to be made.

	# To make 'next' links easy, we'll create a symlink
	#  from the previous entry to this one:
	#
	if ("$previous" != "NONE") then
		set nextlink="$previous.next.html"
		/bin/rm -f "Journal/$nextlink"
		ln -s "$entry.html" "Journal/$nextlink"
	endif
	set previous="$entry"


	set entryFile="entries/$entry"

	set title=`head -1 $entryFile | sed -f NameMappings`

	set numims=`grep "<img" $entryFile | wc -l`
	if ($numims > 0) then
		set imnote="<b>($numims)</b>"
	else
		set imnote=""
	endif

	echo -n "<tr><td align=right><a href=$entry.html>${dateString}</a></td>" >> Journal/index.tmp
	echo "<td>$title $imnote</td></tr>" >> Journal/index.tmp


	# If the target already exists and is current, we don't need to make it again:

	if (-e "$target") then
		if ( -M "$target" > -M "$entryFile" ) continue
		echo "Target exists but is out of date. Rebuilding it..."
	endif


	echo "New on ${dateString}: $title"

	# Here's where we actually put the pieces together into a target entry:

	echo "<html>"                               > "$target"
	echo "<title>$title</title>"               >> "$target"

	sed -f NameMappings < parts/header         >> "$target"

	echo "<br>"                                >> "$target"
	echo "<br>"                                >> "$target"
	echo "<center>"                            >> "$target"
	echo '<font face="Arial,Helvetica">'       >> "$target"
	echo '<font size=+1>'                      >> "$target"
	echo "<h2>$dateString</h2>"                >> "$target"
	echo "<h4><em>$title</em></h4>"            >> "$target"
	echo "</center>"                           >> "$target"
	echo "<br>"                                >> "$target"
	echo "<br>"                                >> "$target"

	tail +2 "$entryFile" | sed -f NameMappings >> "$target"

	echo -n "<br><br><center>["                >> "$target"
	if ("$prevFile" != "NONE.html") then
	  echo "<a href=$prevFile>Prev</a> | "     >> "$target"
	endif
	echo "<a href=index.html>Index</a> | "     >> "$target"
	echo "<a href=$nextFile>Next</a>]"         >> "$target"
	echo "<p></center>"                        >> "$target"

	sed -f NameMappings < parts/footer         >> "$target"

end

# This is a clone of the "previous" related code
#  above, except instead of linking to the current
#  entry, it links back to the index:
if ("$previous" != "NONE") then
	set nextlink="$previous.next.html"
	/bin/rm -f "Journal/$nextlink"
	ln -s "index.html" "Journal/$nextlink"
endif


# Now let's build the index.html file:

/bin/rm -f Journal/index.html
sed -f NameMappings < parts/indexHeader  > Journal/index.html
if ("$order" == "reverse") then
	tac Journal/index.tmp               >> Journal/index.html
else
	cat Journal/index.tmp               >> Journal/index.html
endif
sed -f NameMappings < parts/indexFooter >> Journal/index.html
/bin/rm -f Journal/index.tmp
echo "Index updated."


if (! -e Journal/scripts && ! -l Journal/scripts) then
	ln -s "$scriptsDir" Journal/scripts
endif


if (! -e Journal/ims && ! -l Journal/ims) then
	ln -s "$imsDir" Journal/ims
endif