#!/bin/sh
#
# generate_index_html
#
# Generates index.html files with a link to each file in the directory, and
# recursively for its sub-directories.
#
# Note: This script recursively calls itself, so it had better be in its
# own path!
#

INDEX=index.html.$HOST

trap 'rm -f $INDEX; exit 1' 1 2 3 13 15

    #
    # These are the standard locations for apache's icons
    #
img_txt="<img src=/icons/text.gif>"
img_dir="<img src=/icons/folder.gif>"
img_bak="<img src=/icons/back.gif>"

echo "<!-- This page was automatically generated by generate_index_html -->" \
	> $INDEX
echo "<HTML>" >> $INDEX
echo "<HEAD>" >> $INDEX
echo "<TITLE>The Mercury Project: Directory Listing</TITLE>" >> $INDEX
echo "</HEAD>" >> $INDEX
echo "<BODY>" >> $INDEX
echo "<H1>Directory listing</H1>" >> $INDEX
echo "<hr>" >> $INDEX
echo "$img_bak <a href=../>Parent Directory</a><br>" >> $INDEX

for file in * 
do
        #
	# Don't include the README or any of the index files
	#
    case $file in
        README|index.html*) 
	    ;;

	*)  if [ -d $file ]
            then
    	        (cd $file && generate_index_html)
                echo "$img_dir <a href=$file/>$file/</a><br>" >> $INDEX
            else
	        size=`ls -l $file | awk '{ print $5; }'`
	        sizekb=`expr $size / 1024`
                echo "$img_txt <a href=$file>$file</a> ($sizekb kilobytes)<br>" >> $INDEX
            fi
	    ;;
    esac
done

echo "<hr>" >> $INDEX

if [ -f README ]
then
    echo "<pre>" >> $INDEX
    cat README >> $INDEX
    echo "</pre>" >> $INDEX
fi
echo "</BODY>" >> $INDEX
echo "</HTML>" >> $INDEX

chmod a+r,g+w $INDEX
chgrp mercury $INDEX

mv $INDEX index.html

