#!/bin/bash

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

function syntax() {
	echo Syntax: $0 [-l] [-s style] [-e exclude_pattern]... [top-level_directory]
}

function do_short() {
	echo short $THIS >&2
        $sedir/bin/short -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 le:s: c
do
	case $c in
		e)	if [ -n "$exclude" ]
			then
				exclude=$exclude\|$OPTARG
			else
				exclude=$OPTARG
			fi
			;;
		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=/usr/doc/smarteiffel/html
PARENT=..

	(
	  cd $TOP
	  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=${TOP}/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`

if [ -f ./loadpath.se ]
then
	LPLIST=`cat ./loadpath.se`
fi

if [ -z "$LPLIST" ]
then
	if [ `ls ./*.e | wc -l` -gt 0 ]
	then
	    LPLIST=.
	fi
fi

if [ -z "$local_only" ]
then
    seclass=`grep '^lib: .*/loadpath.se' /etc/serc | cut -f2 -d\ `
    secdir=`dirname $seclass`
    for f in  `cat $seclass`
    do
	if [ `echo $f | cut -c1` = / ]
	then
	    LPLIST="$LPLIST $f"
	else
	    LPLIST="$LPLIST $secdir/$f"
	fi
    done
fi

if [ -z "$LPLIST" ]
then
    echo "No classes found" >&2
    exit 1
fi

again=TRUE
while [ $again = TRUE ]
do
	X=
	again=FALSE
	for f in $LPLIST
	do
		if eval [ -d $f ]
		then
			X="$X `eval echo $f`"
		else
			again=TRUE
			Y=`eval cat $f`
			X="$X `eval echo $Y`"
		fi
	done
	LPLIST="$LPLIST `echo $X`"
done

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

if [ -n "$exclude" ]
then
	LIST=`(for f in $LPLIST
	         do 
					echo $f 
				done)|  eval grep -Ev \'$exclude\'`
fi

(
for f in `eval ls $LIST | grep '\.e$'`
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"
