#!/bin/bash

# Walk through a library of Eiffel classes and build an interconnecting
# set of HTML pages

function syntax() {
	echo Syntax: $0 [-a ace_file] [-l] [-s style] [-h html_directory] [top-level_directory]
}

function do_short() {
	echo short $THIS >&2
        /usr/bin/short $ACE -case_insensitive -${STYLE} ${THIS} |
            sed -e 's/="_CLASSREF">\([A-Z][A-Z_0-9]*\)</="\1.html">\1</g' |
	    eval sed    -e \'s/_THISCLASS/${THIS}/g\' \
		-e \'s/_DATE/${TODAY}/g\' \
		-e \'s/_PREVCLASS/${PREV}/g\' \
		-e \'s/_NEXTCLASS/${NEXT}/g\' >$outf

	echo '<LI><A HREF="'${THIS}'.html">'${THIS}'</A>' >>${INDEX} ;
}

STYLE=html1

while getopts a:lh:s: c
do
	case $c in
		a)	ACE=$OPTARG
			;;
		h)	HTMLDIR=$OPTARG
			;;
		l)	local_only=local
			;;
		s)
			STYLE=$OPTARG
			;;
		*)
			syntax
			exit 1
			;;
	esac
done

export OPTIND
while [ $OPTIND -gt 1 ]
do
	shift
	OPTIND=`expr $OPTIND - 1`
done

TODAY=`date +%d\ %B\ %Y`
TOP=$1

if [ -z "$TOP" ]
then
    TOP=.
fi

PARENT=$HTMLDIR/..

	(
	  cd $HTMLDIR
	  cp /usr/share/doc/smarteiffel/html/up.jpeg up.jpeg
	  cp /usr/share/doc/smarteiffel/html/prev.jpeg prev.jpeg
	  cp /usr/share/doc/smarteiffel/html/next.jpeg next.jpeg
	)

INDEX=${HTMLDIR}/index.html

echo "Building HTML index of the Eiffel class library..."
cat >${INDEX} << EOI
<HTML>
<HEAD>
<TITLE>Eiffel Library Index</TITLE>
</HEAD>
<BODY>
EOI
if [ -f $PARENT/index.html ]
then
	cat >${INDEX} << EOI
<A HREF="${PARENT}/index.html"><IMG SRC="${PARENT}/up.jpeg" ALT="Up" ALIGN=bottom WIDTH="40" HEIGHT="40"></A> Top
<BR><BR>
EOI
fi
cat >${INDEX} << EOI
<HR>
<H1>Eiffel Library Index</H1>
<H2>Classes inherited by every class</H2>
Every class inherits ANY by default, unless an inheritance clause is
included in it. Any class specified by an inheritance clause will itself
inherit from ANY, and so ANY and its ancestors are effectively inherited
by every class, apart from features which are specifically undefined or
redefined. 
<p>
ANY inherits from PLATFORM, and PLATFORM in turn inherits from GENERAL.
GENERAL is the only class which has no parent. 
<p>
Therefore all the features of all these classes are available to every class
in your application. 
<H2>List of classes</H2>
These are the ${local_only} library classes available:                                        
<p>                                                                             
<ul COMPACT> 
EOI

TMPFIL=`mktemp /tmp/selib2html.XXXXXX`

LPLIST=$(find $TOP -name '*.e')

LIST=`(for f in $LPLIST
			do
				echo $f
			done) | sort -u`

(
for f in $LIST
do
    CLASS=`basename $f .e | tr '[a-z]' '[A-Z]'`
    echo $CLASS
done
) | sort -u >$TMPFIL

FIRST=`head -1 $TMPFIL`
LAST=`tail -1 $TMPFIL`

THIS=""
NEXT=""
for CLASS in `cat $TMPFIL`
do
    if [ -z "$THIS" ]
    then
	THIS=$CLASS
        outf=${TOP}/${CLASS}.html
	PREV=$LAST
    else
	NEXT=${CLASS}
	do_short
	PREV=$THIS
	THIS=$CLASS
	outf=${TOP}/${CLASS}.html
    fi
done

NEXT=${FIRST}
do_short

cat >>${INDEX} << EOI
</ul>
EOI
if [ -z "$local_only" ]
then
	cat >>${INDEX} << EOI

<h2>Class ANY</h2>
ANY does not possess any new features of its own. It inherits all the
features of <A HREF="PLATFORM.html">PLATFORM</A> and of
<A HREF="GENERAL.html">GENERAL</A>.
<p>
If you wanted to make some new features that were available to all your
classes, you could insert them in ANY (either by writing your own class or,
less advisedly, by editing /usr/lib/smarteiffel/lib_std/any.e) and thus
cause them to be inherited by every other class (including other base
library classes).
<BR>
EOI
fi
cat >>${INDEX} << EOI
<BR>
EOI
if [ -f $PARENT/index.html ]
then
	cat >>${INDEX} << EOI
<HR>
<BR>
<A HREF="${PARENT}/index.html"><IMG SRC="${PARENT}/up.jpeg" ALT="Up" ALIGN=bottom WIDTH="40" H
EIGHT="40"></A> Top
<BR>
EOI
fi
cat >>${INDEX} << EOI
<BR><HR><BR>
<I>Generated by selib2html on $TODAY</I>
</BODY>
</HTML>
EOI

rm -f $TMPFIL
echo "... HTML build completed"
