#!/bin/sh

#
#	.mkrevchron.sh -	Script acquires a list of files to be
#				processed from the current directory
#				and generate an byage.html file which
#				has the stories sorted in reverse
#				chronological order
#		Author:	Jack C Lipton,  cupasoup@softhome.net
#
#	$Id: .mkrevchron.sh,v 1.3 2003/12/27 14:37:42 jcl Exp jcl $
#	$Log: .mkrevchron.sh,v $
#	Revision 1.3  2003/12/27 14:37:42  jcl
#	corrected issue w/ RCSINFO not being extracted correctly.
#
#	Revision 1.2  2003/12/27 14:35:19  jcl
#	doinked w/ sortation formule;  uses RCS date/time info rather than
#	actual age of the source file.
#
#	Revision 1.1  2003/12/27 13:58:05  jcl
#	Initial revision
#
#


HTML=byage.html			# target file
INFO=.sort
TMP=/tmp/mkindexT.$$
AWK=/tmp/mkindexA.$$

EMAILTO=cupasoup@softhome.net	# Author's e-Mail


#
#	Here's an awk script that will isolate the "most recent"
#	entry in the RCS log for a file...
#

cat				>$AWK			<<AWKSCRIPT
BEGIN	{
		skipping =	1;
		lines =		0;
	}
/^----/ || /^====/ {
		if	( skipping != 0 )	{
			skipping =	0;
		} else {
			exit;
		}
		next;
	}
skipping != 0 	{ next; }
lines++ == 0	{ }
/^revision /	{ next; }
/^date: /	{ next; }
		{ printf( "%s<BR>\n", \$0); }
END	{
	}
AWKSCRIPT


#
#	Here's the index file prefix.  You can tell that I like
#	shell "here" documents, can't you?
#

cat				>${HTML}+		<<INDEXHTML
<HTML>

<HEAD>
	<TITLE>Jack C Lipton's Stories - Reverse Chrono</TITLE>
</HEAD>

<BODY>

<CENTER>

<H1>
Jack C Lipton<BR>
Sorted Story Index
</H1>

<BR> <BR>

<B>eMail:</B>
	<A HREF="mailto:cupasoup@softhome.net">
		<TT>cupasoup@softhome.net</TT>
	</A>							<BR>

<BR> <BR>

Wanna see <B>why</B> things got changed?			<BR>
My <A HREF="diary.html"> Diary </A> should provide a clue!	<BR>

<BR> <BR>

<I>Last Modified: `date`</I>					<BR>

<BR> <BR>

<H2>Warning: Caveat Lector</H2>

<TT>
This page links to <B><I><FONT SIZE="+2">adult</FONT></I></B> material.		<BR>
<BR>
If you are under 18 years of age you really			<BR>
*should* use the "BACK" button to leave NOW.			<BR>
<BR>
If you live somewhere where the perusal				<BR>
of adult-oriented material is illegal it			<BR>
might be time to consider a revolution.				<BR>
For now, though, hit the "BACK" button.				<BR>
<BR>
Material linked here likely to be offensive to			<BR>
*somebody*.  If you're uncomfortable with sex			<BR>
and sexual issues, you also should "BACK" out			<BR>
of this page too.  It's your choice to proceed.			<BR>
<BR>
<B>You have been warned.</B>					<BR>
</TT>

</CENTER>

<BR> <BR>

<B>Links to my other pages:</B>					<BR>
<UL>
 <LI>	<A HREF="index.html">		Main			</A>
 <LI>	<A HREF="all-index.html">	Main (includes incomplete works)	</A>
 <LI>	<A HREF="scripts.html">		Website maintenance scripts	</A>
 <LI>	Things I find	<A HREF="Humor.html">	funny		</A>
</UL>

<BR> <BR>

<H2>Stories</H2>

<BR>

<UL>

INDEXHTML



#
#	This works through the file names.  If I really wanted
#	to be subtle I'd probably map each file into the titles
#	and sort against that.  Perhaps another day I'll do so.
#	But not today, obviously.  Again, I slapped this together
#	pretty quickly.
#

>$INFO				# make sure the file is empty

echo	"Bringing in header information:"

for	F	in	*.x
    do

	echo	-e	"\r                            \r$F \c"

	#	isolate name so we can check whether this should be displayed

	CORENAME=`echo $F | awk -F. '{print $1}'`	# independant of ext
	#CORENAME=`basename $F .x`

	#
	#	grab information from the SHS headers...
	#	This is most convenient using the text file...
	#

	TITLE="`head -15 $F | grep '^Title: ' | sed 's/^Title: //'`"
	INCOMPLETE="`echo "$TITLE" | grep -i incomplete | wc -l | sed 's/  *//'`"

	if	[ $INCOMPLETE != 0 ]
	    then
		TITLE=`echo "$TITLE" | sed 's/ ([Ii]ncomplete)//'`
	    fi

	MAINTTL="`echo $TITLE | sed 's/:..*$//'`"
	SUBTTL="`echo $TITLE | sed 's/^..*: //'`"

	if	[ "$MAINTTL" = "$SUBTTL" ]
	    then
		SUBTTL=""
	    fi

	if	[ -z "$TITLE" ]
	    then

		#
		#	If SHS headers not in place we'll need to cheat.
		#	This does that.  (I hope)
		#

		TITLE=`head -15 $F | grep -v '^$' | head -1 | sed 's/^[ 	][ 	]*//'`

		PART=""
		CODES=""
		SUMMARY=""
		UNIVERSE=""

		RCSVERSION=
		RCSCHGDATE=
		RCSCHGTIME=
		RCSINFO=

		WORDS=`cat $F | wc -w`		# count the words

	    else

		PART="`head -15 $F | grep '^Part: ' | sed 's/^Part: //'`"
		CODES="`head -15 $F | grep '^Keywords: ' | sed 's/^Keywords: //'`"
		SUMMARY="`head -15 $F | grep '^Summary: ' | sed 's/^Summary: //'`"
		UNIVERSE="`head -15 $F | grep '^Universe: ' | sed 's/^Universe: //'`"
		[ "$UNIVERSE" = "n/a" ] &&	UNIVERSE=

		RCSLINE="`head -15 $F | grep '^RCS: ' | sed 's/RCS: \$Id: //'`"

		if	[ ! -z "$RCSLINE" ]
		    then

			RCSVERSION="`echo $RCSLINE | awk '{print $2}'`"
			RCSCHGDATE="`echo $RCSLINE | awk '{print $3}'`"
			RCSCHGTIME="`echo $RCSLINE | awk '{print $4}'`"

			RCSINFO="$RCSCHGDATE $RCSCHGTIME $RCSVERSION"

		    else

			RCSINFO=

		    fi

		WORDS=`cat $F | wc -w`		# count the words

	    fi

	AGE=`./fileage ${CORENAME}.x | awk '{print $1}'`

	echo	"$AGE|$CORENAME|$UNIVERSE|$MAINTTL|$PART|$SUBTTL|$CODES|$SUMMARY|$WORDS|$INCOMPLETE|$RCSINFO"	>>$INFO

    done

echo	-e	"\r                            \r\c"


#
#	OK, we have extracts of all files now in $INFO;  We'll sort the
#	file by the RCS date and time in reverse order...
#

#sort	-t '|'	+1	$INFO						>${INFO}+
sort	-r	-t '|'	+10	$INFO					>${INFO}+
cutover	$INFO

FILES=`cat $INFO | awk '-F|' '{print $2}'`	# sorted list of files



#
#	Now we'll build the actual linkages for the web page from the sorted
#	file-  which simplifies matters enormously...
#

for	CORENAME	in	$FILES
    do

	>$TMP

	#	isolate name so we can check whether this should be displayed

	F=$CORENAME.x					# text version of file
	WF=$CORENAME.html				# HTML version of file

	SORTLINE="`grep "|$CORENAME|" $INFO`"

	echo	-e	"$F ... \c"					>/dev/tty

#	$AGE|$CORENAME|$UNIVERSE|$MAINTTL|$PART|$SUBTTL|$CODES|$SUMMARY|$WORDS|$INCOMPLETE|$RCSINFO
#	  1      2         3         4      5      6       7       8       9       10         11

	MAINTTL="`echo $SORTLINE | awk '-F|' '{print $4}'`"
	PART="`echo $SORTLINE | awk '-F|' '{print $5}'`"
	SUBTTL="`echo $SORTLINE | awk '-F|' '{print $6}'`"
	CODES="`echo $SORTLINE | awk '-F|' '{print $7}'`"
	SUMMARY="`echo $SORTLINE | awk '-F|' '{print $8}'`"
	WORDS="`echo $SORTLINE | awk '-F|' '{print $9}'`"
	INCOMPLETE="`echo $SORTLINE | awk '-F|' '{print $10}'`"
	RCSINFO="`echo $SORTLINE | awk '-F|' '{print $11}'`"


	#
	#	Report the title associated with the file name
	#

	echo	-e	"$MAINTTL $PART $SUBTTL \c"		>/dev/tty


	#
	#	We encapsulate the following to reduce the need for
	#	individual redirections of stdout;  Also, it goes to
	#	a temporary file so that it can be conditionally
	#	appended to the index files under construction...
	#

	(
		echo	-e	"  <LI>	<A HREF=\"$WF\">\n\t\t<B>$MAINTTL\c"
		[ ! -z "$SUBTTL" ] &&	echo	-e	": \c"

		[ ! -z "$PART" ] &&	echo	-e	" [$PART] \c"
		[ ! -z "$SUBTTL" ] &&	echo	-e	" $SUBTTL \c"

		echo	-e	"\n\t</B> </A> \c"

		[ $INCOMPLETE != 0 ] &&	echo	-e	"<B><I>In Progress</B></I> \c"

		[ ! -z "$CODES" ] &&	echo	-e	"($CODES) \c"

		[ ! -z "$SUMMARY" ] &&	echo	-e	"<BR>\n\t$SUMMARY"

		[ ! -z "$WORDS" ] &&	echo	-e	"<BR>\n\t$WORDS words"

		if	[ -f d-$WF ]
		    then
			echo	-e	"( <A HREF=\"d-$WF\"> View Latest Revisions </A> ) \n"
		    fi

		if	[ ! -z "$RCSINFO" ]
		    then

			RCSDATE=`echo $RCSINFO | awk '{print $1}'`
			RCSTIME=`echo $RCSINFO | awk '{print $2}'`
			RCSVERSION=`echo $RCSINFO | awk '{print $3}'`

			echo	-e	"<BR>	<B>Version</B> $RCSVERSION\n"
			echo	-e	"	<B>Changed:</B> <TT>$RCSDATE</TT> at <TT>$RCSTIME</TT>\n"

			echo	"<BR>"

			rlog	$F				\
			    |	awk	-f $AWK
		    fi

		echo	-e	"<BR>\n"

	)								>$TMP


	#
	#	The "all" index file gets it regardless, but if it's
	#	amongst the "hidden" it won't be seen on the default
	#	index file...
	#

	cat	$TMP						>>${HTML}+

	echo		""					>/dev/tty

    done


#
#	Each index file needs to be closed out.  We'll do that here.
#

cat				>$TMP			<<INDEXHTML

</UL>

<BR>
<BR>

<HR SIZE=5>

<CENTER>
Copyright &copy; 2002, 2003 - Jack C. Lipton			<BR>
<IMG SRC="anydamn2.gif" LABEL="Viewable in any damn browser">	<BR>
</CENTER>

</BODY>
</HTML>
INDEXHTML

cat	$TMP							>>${HTML}+


#
#	If any actual changes have been made to the newly generated files
#	bring them "current" so we're live with the updated file.  This
#	keeps the index files from being updated at the published site
#	UNLESS actual changes have been made.
#

for	F	in	${HTML}
    do
	diff	${F}	${F}+					>$TMP

	if	[ -s $TMP ]
	    then
		cutover		$F
	    else
		rm -f		${F}+
		touch		$F
	    fi
    done


#
#	Perform clean-up of volatile files
#

rm -f		$TMP

exit	0		# all done!

    

.mkrevchron