#!/bin/sh

# Extract all HTML date entries and sort them by date,
# then convert them to an HTML index of these entries.
#
# Usage: chronolog [outfile [r]]
#

FILE=${1:-chronolog.html}
NEWFILE=$FILE+
NEWFILE2=$FILE++
NEWFILE3=$FILE+++

trap "echo ABORTED;rm -f $NEWFILE $NEWFILE2 $NEWFILE3;exit 0" INT

# Output HTML header.

if [ -z "$2" ] ; then
	cat >$NEWFILE <<-EOF
		<html>
		<head>
		<!-- Created automatically by the $(basename $0) script! -->
		<title>Chronological Log Index</title>
		</head>
		<body>
	EOF
else
	cat >$NEWFILE <<-EOF
		<html>
		<head>
		<!-- Created automatically by the $(basename $0) script! -->
		<title>Reverse-Chronological Log Index</title>
		</head>
		<body>
	EOF
fi

# Title it.

if [ -z "$2" ] ; then
	echo "<H1>Chronological Log Index</H1>" >>$NEWFILE
else
	echo "<H1>Reverse-Chronological Log Index</H1>" >>$NEWFILE
fi

# Table header.

cat >>$NEWFILE <<-EOF
	<TABLE>
EOF

# Get the goodies.  Could be tidier, but this works.  Maybe later.
#
# Converts all lines in all HTML files like:
#
#  <H3>Day, Month DD, YYYY<A NAME=DDMonYYYY></A>...</H3>
#
# to lines like (where f is a file name):
#
#  <TR><TD><A HREF=f#DDMonYYYY>Day, Month DD, YYYY</A> <TD>(<A HREF=f>f</A>)
#
egrep -H '^<H3>.*<A NAME=[0-9]+.*></A>' *.html */*.html \
     | fgrep -v $FILE \
     | sed -e 's/:/: /' -e 's/>/> /' \
     | sort -s -bd +5n$2 -6 +3M$2 -4 +4n$2 -5 \
     | sed 's/\(^.*\): <H3> \(.*\)<A NAME=\(.*\)><.*$/<A HREF=\1#\3>\2<\/A> (<A HREF=\1>\1<\/A>)/' \
     | sed -e 's@</A>@@' -e 's/^/<TR><TD>/' -e 's/(/<TD>(/' >> $NEWFILE

# Output HTML trailer.

cat >>$NEWFILE <<-EOF
	</TABLE>
	<P>
	<A HREF=index.html>Return to Site Home</A>
	</body>
	</html>
EOF

# Collect (and label) all new year marks.  Make each year its own
# table for quicker display in the browser?  (No, that didn't help.)

awk <$NEWFILE >$NEWFILE2 '
	BEGIN {year=0; first=1}
	{pos=match($0, "[1-2][09][0-9][0-9]");
	 if (pos)
	     if (year!=substr($0, pos, 4)) {
		 year=substr($0, pos, 4);
		 if (first != 1) {
		     print "</TABLE>"
		     print "<HR>"
		     print "<TABLE>"
		 } else {
		     print "<TR><TD><HR></TD><TD><HR></TD></TR>"
		 }
		 print "<TR><TD><U>Dated Entry</U><TD><U>From file</U>"
		 print "<TR><TD><B>"year"</B><A NAME=",year,"></A>"
		 first=1; # Set =0 for separate tables, =1 for one table.
	     }
	 print
	}'

mv $NEWFILE2 $NEWFILE

# Extract all new year label marks for making an index table.

<$NEWFILE fgrep '<TR><TD><B>' | sed 's@.*\([1-2][09][0-9][0-9]\).*@<TD><A HREF=#\1>\1</A>@' >$NEWFILE2

# Only ten years per row of the index table

awk <$NEWFILE2 >$NEWFILE3 '
	BEGIN {line=1}
	{
	    if (line++ > 10) {
		print "<TR>"
		line=1
	    }
	    print
	}'

# Insert index table at front of file.

while read LINE; do
    if [[ -r $NEWFILE3 ]]; then
	if [[ "$LINE" == '<TABLE>' ]]; then
	    echo "<TABLE><TR>"
	    cat $NEWFILE3 2>/dev/null
	    rm -f $NEWFILE3
	    echo "</TABLE><P>"
	fi
    fi
    echo $LINE
done <$NEWFILE >$NEWFILE2

mv $NEWFILE2 $NEWFILE

# If the new file is different overwrite the old one, else throw it
# away.  This keeps the timestamp more valid, and lets the doupdate
# script do its job better.

if cmp -s $NEWFILE $FILE ; then
   rm $NEWFILE
else
   mv $NEWFILE $FILE
fi

##############################################################################
# The date entries are put into the blogs via these emacs macros:
#
#(defun today-insert (&optional comment-only)
# "Insert today's date tag into the blog"
#  (interactive "P") ; "P" == Raw universal prefix into the arg "comment-only".
#  (if comment-only
#      (progn (shell-command "date '+<!---%A, %B %d, %Y-->'|tr -d \\\\012|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t)
#             (exchange-point-and-mark))
#      (progn (shell-command "date '+<H3>%A, %B %d, %Y<A NAME=%d%b%Y></A></H3>'|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t) 
#             (exchange-point-and-mark)
#             (insert "\n") (open-line 2))))
#
#(defun yesterday-insert (&optional comment-only)
# "Insert yesterday's date tag into the blog"
#  (interactive "P") ; "P" == Raw universal prefix into the arg "comment-only".
#  (if comment-only
#      (progn (shell-command "~/bin/yesterday '+<!---%A, %B %d, %Y-->'|tr -d \\\\012|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t) 
#             (exchange-point-and-mark))
#      (progn (shell-command "~/bin/yesterday '+<H3>%A, %B %d, %Y<A NAME=%d%b%Y></A></H3>'|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t) 
#             (exchange-point-and-mark)
#             (insert "\n") (open-line 2))))
#
#(defun tomorrow-insert (&optional comment-only)
# "Insert tomorrow's date tag into the blog"
#  (interactive "P") ; "P" == Raw universal prefix into the arg "comment-only".
#  (if comment-only
#      (progn (shell-command "~/bin/tomorrow '+<!---%A, %B %d, %Y-->'|tr -d \\\\012|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t) 
#             (exchange-point-and-mark))
#      (progn (shell-command "~/bin/tomorrow '+<H3>%A, %B %d, %Y<A NAME=%d%b%Y></A></H3>'|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t) 
#             (exchange-point-and-mark)
#             (insert "\n") (open-line 2))))
#
# The ~/bin/tomorrow shell script:
#
#if [ `date +%Z` == "PDT" ] ; then
#    TZ=GMT-17 date "$@"
#else # PST
#    TZ=GMT-16 date "$@"
#fi
#
# The ~/bin/yesterday shell script:
#
#if [ `date +%Z` == "PDT" ] ; then
#    TZ=GMT+31 date "$@"
#else # PST
#    TZ=GMT+32 date "$@"
#fi
#
##############################################################################
