#! /bin/sh 
#
# I set the tabs to 4 in this file.
#
# extracts the topics and creates each of the index files under
# each alphabet: A/index.html, B/index.html, etc.
#
# Each entry will appear in the appropriate index file if it has the format
#	<!--TOPIC="ABRAHAM"-->
#	<!--TOPIC="IBRAHAM"-->
# will cause ABRAHAM to appear as a topic in A/index.html
# and IBRAHIM to appear as a topic in I/index.html
#
# 		<!--"AAD; AD"-->
# will have AAD; AD appearing as a topic in A/index.html
#
# see the file maintenance.notes for more details


echo "	Creating Index"
echo "	Initializing....Please Wait...."

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

if [ "$*" != "" ]; 
then
	alphabet=$*
else
	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"
fi

grep TOPIC= [A-Z]/*.html > /tmp/a
for i in $alphabet; do
	echo "	Creating $i/index.html"
	pattern="TOPIC=\\\"$i"
	sort -t= +1 -2 /tmp/a > /tmp/b
	grep $pattern /tmp/b > /tmp/c


	gawk -v letter=$i -v singlequote="'" '
		BEGIN { 
			FS=":";
			alphabet=" ABCDEFGHIJKLMNOPQRSTUVWYZ ";
			pos = index(alphabet,letter);
			prevletter=substr(alphabet,pos-1,1);
			nextletter=substr(alphabet,pos+1,1);
			print "<HTML>"
			print ""
			print "<HEAD>"
			print "<TITLE>Comparative Index to Islam</TITLE>"
			print ""
			print "<META NAME=\"description\" CONTENT=\"Comparative Index to Islam\">"
			print "<META NAME=\"keywords\" CONTENT=\"Comparative,Index,Islam,Quran,Torah,Taurat,Injil,Gospel,Bible,Hadiths,Sunna,New Testament,Old Testament,Traditions,Muslim,Muslims,Christian,Christians,Jews,Judaism,Christianity\">"
			print "<META NAME=\"distribution\" CONTENT=\"global\">"
	
			print "</HEAD>"
			print ""
			print "<BODY BGCOLOR=\"#333399\" text=\"#ffffff\" link=\"#ffffff\" vlink=\"#ffffff\">"
			print ""
			print "<A NAME=\"" letter "\">"
			print "<BR>"
			print "<TABLE ALIGN=CENTER>"
			print "<TR>"
			if (prevletter != " ")
			{
				print "<TD ALIGN=\"center\" WIDTH=\"20%\"><A HREF=\"../" prevletter "/index.html\"><IMG SRC=\"../Images/left.gif\" ALT=\"PREVIOUS\" BORDER=0></A></TD>"
			}
			else
			{
				print "<TD ALIGN=\"CENTER\" WIDTH=\"20%\">&nbsp;</TD>"
			}
			print "<TD ALIGN=\"center\" WIDTH=\"60%\"><FONT SIZE=+1><B>" letter "</B></FONT></TD>"
			if (nextletter != " ")
			{
				print "<TD ALIGN=\"CENTER\" WIDTH=\"20%\"><A HREF=\"../" nextletter "/index.html\"><IMG SRC=\"../Images/right.gif\" ALT=\"NEXT\" BORDER=0></A></TD>"
			}
			else
			{
				print "<TD ALIGN=\"left\" WIDTH=\"20%\"></TD>"
			}
			print "</TR>"
			print "</TABLE>"
			print ""
			print "<TABLE>"
		}


		{
			file = $1;
			numargs = split($2, t, "TOPIC=\"");
			numargs = split(t[2], topic, "\"-->");
	
			numargs = split(file, t, ".html");
			tocfile = t[1] "_toc.html";
			tocexist = system("[ ! -f " tocfile " ]");
			bullet = "<TR VALIGN=\"TOP\"><TD>"
			onclick = "";
			if (tocexist)
			{
				bullet = bullet "<IMG SRC=\"../Images/closeddoc.gif\"></TD>"
				onclick = " onClick=" singlequote "parent.index.location.href=\"../" tocfile "\"" singlequote
			}
			else
			{
				bullet = bullet "<SMALL><B>&#167;</B></TD>"
			}
			print bullet "<TD><SMALL><A HREF=\"../" file "\" target=\"explanation\"" onclick "><B>" topic[1] "</B></A></TD></TR>"
			print ""
		}


		END {
			print "</TABLE>"
			print ""
			print ""
			print "<P><HR><P>"
			print ""
			print "<CENTER><A HREF=\"../index.html\" target=\"_top\"><SMALL><B>Return to main index</B></SMALL></A></CENTER>"
			print ""
			print "<P>"
			print ""
			print "</BODY>"
			print "</HTML>"
		}
	' /tmp/c > $INDEXDIR/$i/index.html
done
echo "	Index Created."
