#!/bin/ksh
# @(#)RELEASE VERSION Workshop Docs 4.0
#
# Automatically generate index HTML page for all product docs
#

# this script should be called with the result of running findyourself
if [[ -d $1 ]]
 then SCRIPT_ROOT=$1

HTML_DIR=html_docs
HTML_DIR_LOC=../lib/locale/${LC_ALL:-${LC_MESSAGES:-${LANG:-C}}}
DESCRIPTION_FILE=description
HTML_FILE=index.html

num_dirs=$(/usr/bin/ls -F ${SCRIPT_ROOT}/${HTML_DIR_LOC}/${HTML_DIR} | /usr/bin/grep / | /usr/bin/wc -l)

# check to see if there are any subdirectories in HTML_DIR, if
# there are at least 2, "shared" plus 1 other, then generate the index
# otherwise skip it and do nothing
#
if [ $num_dirs -gt 1 ]
then 

# Make a name safe to part of a url by escaping any forbidden characters
name2url()
{
	# hack: only recognizes '+' so far
	echo "$@" | /usr/bin/sed -e 's/+/%2B/g'
}

cat << \!
<HEAD>
<TITLE>Sun WorkShop Documentation</TITLE>

!

# Base is probably not required
# echo '<BASE HREF="'$SCRIPT_ROOT/$HTML_DIR_LOC/$HTML_DIR/$HTML_FILE'">'

cat << \!
</HEAD>

<! DO NOT EDIT THIS PAGE IN THIS LOCATION>
<! This HTML is generated automatically by the script create-html-index >
<! which is called during installation of the HTML documentation packages >

<BODY bgcolor="DEDEDE">

<table border=2 CELLPADDING=8 CELLSPACING=8>

<th colspan=2><img src="shared/WSdocbar.gif"></th><tr>
<th> Documentation Sets </th>
<th> Available Manuals </th>

!

cd ${SCRIPT_ROOT}/${HTML_DIR_LOC}/${HTML_DIR}
product_list="`/usr/bin/ls -1 */${DESCRIPTION_FILE} | /usr/bin/sort -f`"


for pd in $product_list
do
	product="${pd%%/*}"

        echo '<tr><td>'

	href="`name2url $product`"
	if [ -r ${product}/${HTML_FILE} ]
	then 
	     echo '<A HREF="'${href}/${HTML_FILE}'"> '`/usr/bin/head -1 ${product}/description`' </A><br>'
	else echo `/usr/bin/head -1 ${product}/description`
	fi
        echo '</td><td>'

	document_list="`/usr/bin/ls -1 ${product}/*/${DESCRIPTION_FILE} | /usr/bin/sort -r`"

	for doc in $document_list
	do
	  temp="${doc%/*}"
	  document="${temp#*/}"
	  if [ -r ${product}/${document}/${HTML_FILE} ]
	  then 
	    dhref="`name2url $document`"
	    echo '<A HREF="'${href}/${dhref}/${HTML_FILE}'"> '`/usr/bin/head -1 ${product}/${document}/description`' </A><br>'
	  else echo `/usr/bin/head -1 ${product}/${document}/description`'<br>'
	  fi
	done
	
echo '</td>'

done

cat << \!

</TABLE>
</BLOCKQUOTE>
<P>For information about how to order printed documentation sets or individual manuals, see the  <A HREF="http://sunexpress.usec.sun.com">SunExpress</A> Web site and choose your
country, the catalog, SunDocs, then WorkShop Books.
</P>
<P><A HREF="http://www.sun.com">Sun Microsystems</A> | <A HREF="http://www.sun.com/developer-products">WorkShop Developer Products</A></P>

<ADDRESS>
<em><H5><A HREF="shared/copyright.html">Copyright</A> 1996 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, Ca 94043-1100 USA.  All Rights Reserved</H5>
</ADDRESS>

</BODY>
</HTML>
!


fi

 else echo "Error: create-html-index: requires directory as argument."
fi
