#! /bin/sh 
#
# I set the tabs to 4 in this file.
#
# extracts the ITEMS in each topic into a table of contents file.
# eg. A/aaron.html will have A/aaron_toc.html as TOC file.
#
# each item to appear in the TOC must have the following tagged 
# sections in the explanation file, eg. A/aaron.html
#
#	<!--TOC="abcde"-->
#	<A NAME="abcde">
#
# both must appear, and the string inside the quotes must be identical
# the former is for this script to create the toc file, the latter is
# for the browser to get there from the toc file.
#
# see the file maintenance.notes for more details


echo "	Creating Table of Contents files"
echo "	Initializing.... Please Wait...."

umask 122		# user - rw, group, others - r


# change this to where the index resides.
INDEXDIR=.
cd $INDEXDIR

alphabet="A B C D E F G H I J K L M N O P Q R S T U V W Y Z"

# if filenames are provided, use these only. otherwise
# scan all the files and determine which ones have to be created
# - in the latter case, it is not possible (or rather involved)
# to determine which ones changed and which ones do not, so
# recreate all the toc files anyway.
#
# use the names of toc filenames to do the processing
# it's a little roundabout, but just in case people pass in 
# the wrong file names, we don't destroy them accidentally.
# we safeguard by changing all the file access permission first
#
if [ "$*" != "" ];
then
	files=`echo $* | sed -e 's/_toc.html/.html/g'`
else
	files=`grep TOC= [A-Z]/*.html | cut -d: -f 1 | sort -u`
fi
tocfiles=`echo $files | sed -e 's/.html/_toc.html/g'`
chmod -w $files

#
# initialize each of the toc files first
# this part is a little dangerous because it removes the files
# so, if the code here is not careful, it will remove the
# data files. That is why we have the roundabout workabout above
#
for i in $tocfiles; do
	datafile=`echo $i | sed -e 's/_toc.html/.html/'`
	s=`grep TOPIC= $datafile`
	s=`echo $s | sed -e 's/"-->.*//'`
	topic=`echo $s | sed -e 's/^.*<!--TOPIC="//'`

	rm $i
	echo "<HTML>" > $i
	echo "" >> $i
	echo "<BODY BGCOLOR=\"#333399\" text=\"#ffffff\" link=\"#ffffff\" vlink=\"#cccccc\">" >> $i
	echo "" >> $i
	echo "<BR>" >> $i
	echo "" >> $i
	echo "<CENTER><H4>"$topic"</H4></CENTER>" >> $i
	echo "" >> $i
	echo "<SMALL>" >> $i
	echo "<UL>" >> $i
done


#
# Now process all the TOC tags
#
grep TOC= [A-Z]/*.html > /tmp/a

for i in $alphabet; do
	echo "	creating toc files under $i"
	pattern="^$i"
	grep $pattern /tmp/a > /tmp/b

	gawk -v letter=$i -v singlequote="'" -v tocfiles="$tocfiles" '
		BEGIN { 
			FS=":";
		}


		#store each of the TOC items into the corresponding toc file
		{
			numargs = split($1, topic, ".html")
			tocfile = topic[1] "_toc.html"
			exists=index(tocfiles, tocfile)
			if (exists != 0) 
			{
				numargs = split($2, t, "<!--TOC=\"")
				numargs = split(t[2], item, "\"-->")
				subitem = item[1];
				itemname = subitem;
				sub("\?", "", itemname);
				print "<LI><A HREF=\"../" topic[1] ".html#" itemname "\" TARGET=\"explanation\"><B>" subitem "</B></A>" >> tocfile
			}
		}


	' /tmp/b
done


#
# finished up the HTML stuffs for the toc file
#
for i in $tocfiles; do
	letter=`echo $i|cut -c1`
	echo "" >> $i
	echo "</UL>" >> $i
	echo "</SMALL>" >> $i
	echo "" >>$i
	echo "<CENTER><HR></CENTER>" >> $i
	echo "" >> $i
	echo "<CENTER><A HREF=\"index.html\"><SMALL><B>Return to subindex</B></SMALL></A></CENTER>" >> $i
	echo "</HTML>" >> $i
done

echo "	Postprocessing....."
chmod +w $files

echo "	Table of Contents Completed."
