#! /bin/sh
#
# This script makes a single HTML page (collection) from the information in
# a directory.
#
# The files that may be present are
# title.txt 
# assign.txt
# routines.txt
# hintsspec.txt
# solution.lst
# output.txt
# related.lst
# profile.alog
# profile.lst
# profile.ps
# trace.txt
#
# This script creates the files:
#    main.html - contains Frame information for contents
#        access.html - Quick access list
#        title.html  - Title (put in banner)
#        assign.html - Assignment
#        solution.html - Solution with links to manual pages
#        output.html   - Sample output
#        hints.html  - Hints
#
mapfiles="-map /home/MPI/mpich/www.index"
#mapfiles="-map /home/MPI/mpich/util/mpis.cit"
exercises="exercises"
Exercises="Exercises"
Assignment="Assignment"
Solution="A Solution"
SolutionSrc="Download Solution"
assign="assign.txt"
#
# Get command-line info on the relationship of this page to other pages
for arg in "$@" ; do
    case "$arg" in 
	-next=*)
	next=`echo a$arg | sed 's/a-next=//'` ;;
	-prev=*)
	prev=`echo a$arg | sed 's/a-prev=//'` ;;
	-parent=*)
	parent=`echo a$arg | sed 's/a-parent=//'` ;;
	-root=*)
	rootloc=`echo a$arg | sed 's/a-root=//'` ;;
	-echo) set -x ;;
	-reftxt=*)
	reftxt=`echo a$arg | sed 's/a-reftxt=//'`
	if [ ! -s "$reftxt" ] ; then reftxt=""
	fi
	;;
	-map=*)
	mapfile=`echo a$arg | sed 's/a-map=//'`
	if [ -s "$mapfile" ] ; then mapfiles="$mapfiles -map $mapfile"
	else 
	    echo "file $mapfile not found"
	fi
	;;
	-example)
	exercises="examples"
	Exercises="Examples"
	Assignment="Description"
	Solution="Sample Code"
	SolutionSrc="Download Sample Code"
	assign="descrip.txt"
	;;
	*) echo "Unrecognized arg $arg"
	   exit 1 ;;
    esac
done
#
#if [ -x /home/gropp/textfilt/examples/mapnames ] ; then
#    mapfilter="/home/gropp/textfilt/examples/mapnames $mapfiles"
if [ -x /home/gropp/bin/sun4/mapnames ] ; then
    mapfilter="/home/gropp/bin/sun4/mapnames $mapfiles"
    #echo "mapfilter is $mapfilter"
else
    mapfilter="cat -"
fi
#
# Check for essential files first
missing=""
for file in title.txt $assign solution.lst ; do
    if [ ! -s $file ] ; then
	missing="$missing $file"
    fi
done
if [ -n "$missing" ] ; then 
    echo "File(s) $missing are missing!"
    exit 1
fi
# Check for optional files
if [ -z "$nowarn" ] ; then
    for file in hintspec.txt related.lst routines.txt \
		profile.alog profile.lst notes Makefile ; do
	if [ ! -s $file ] ; then
	    if [ -z "$parent" -o $file != related.lst ] ; then 
		echo "Optional file $file is missing"
	    fi
	fi
    done
fi
#
title=`cat title.txt`
/bin/rm -f title.html
cat > title.html <<EOF
<TITLE>$title</TITLE>
<BODY BGCOLOR="FFFFFF">
<TABLE BORDER=0 WIDTH="100%"><TR>
EOF
if [ -n "$prev" ] ; then
    cat >> title.html <<EOF
<TD WIDTH="5%"><A HREF="$prev/main.html" TARGET="_top">Prev</A></TD>
EOF
fi
if [ -n "$next" ] ; then
    cat >> title.html <<EOF
<TD WIDTH="5%"><A HREF="$next/main.html" TARGET="_top">Next</A></TD>
EOF
fi
if [ -n "$parent" ] ; then 
    cat >> title.html <<EOF
<TD WIDTH="5%"><A HREF="$parent/main.html" TARGET="_top">Up</A></TD>
EOF
fi
cat >> title.html <<EOF
<TD ALIGN="CENTER">
<FONT SIZE=+1>
$title</FONT>
</TD>
</TR>
</TABLE>
</BODY>
EOF
#
# Write the main.html file
/bin/rm -f main.html
cat > main.html <<EOF
<TITLE>$title</TITLE>

<!-- I'd like to do this to exactly fit the table in the frame source! -->
<BASE TARGET="_top">
<FRAMESET ROWS="50,*">
<FRAME SRC="title.html" NORESIZE MARGINWIDTH="1" SCROLLING="NO">
<FRAMESET COLS="200,*">
<FRAME SRC="access.html" NORESIZE MARGINWIDTH="1">
<FRAME SRC="assign.html" NORESIZE MARGINWIDTH="1" NAME="mainwindow">
</FRAMESET>
</FRAMESET>

<NOFRAMES>
<BODY BGCOLOR="FFFFFF">
Your browser doesn't support frames.  You'll need to get one.
</BODY>
</NOFRAMES>
EOF
#
# Write the access file
/bin/rm -f access.html
cat > access.html <<EOF
<BODY BGCOLOR="FFFFFF">

<H2>Quick Access</H2>

<MENU>
<LI> <A HREF="assign.html" TARGET="mainwindow">$Assignment</A>
<LI> <A HREF="solution.html" TARGET="mainwindow">$Solution</A>
<LI> <A HREF="solution.txt" TARGET="mainwindow">$SolutionSrc</A>
EOF
if [ -f output.txt ] ; then 
cat >> access.html <<EOF
<LI> <A HREF="output.html" TARGET="mainwindow">Output</A>
EOF
fi

if [ -f notes ] ; then 
cat >> access.html <<EOF
<LI> <A HREF="notes.html" TARGET="mainwindow">Run Script</A>
EOF
fi

if [ -f comments.txt ] ; then
cat >> access.html <<EOF 
<LI> <A HREF="comments.html" TARGET="mainwindow">Comments</A>
EOF
fi

if [ -s Makefile ] ; then
cat >> access.html <<EOF
<LI> <A HREF="Makefile.html" TARGET="mainwindow">Makefile</A>
EOF
fi

if [ -s hintspec.txt ] ; then 
    cat >> access.html <<EOF
<LI> <A HREF="hints.html" TARGET="mainwindow">Hints</A>
EOF
    /bin/rm -f hints.html
    cat - hintspec.txt <<EOF | $mapfilter > hints.html 2>/dev/null
<BODY BGCOLOR="FFFFFF">
EOF
    cat >> hints.html <<EOF
</BODY>
EOF
fi
#
if [ -s related.lst ] ; then
    cat >> access.html <<EOF
<LI> <A HREF="related.html" TARGET="mainwindow">Related $exercises</A>
EOF
    /bin/rm -f related.html
    cat > related.html <<EOF
<BODY BGCOLOR="FFFFFF">
<MENU>
EOF
    for file in `cat related.lst` ; do 
	rtitle=`cat $file/title.txt`
	cat >> related.html <<EOF
<LI> <A HREF="$file/main.html" TARGET="_top">$rtitle</A>	
EOF
	echo "Processing directory ... $file"
	(cd $file ; /home/MPI/class/mpiexmpl/maint/makepage -parent=".." \
	-root=$rootloc )
    done
    cat >> related.html <<EOF
</MENU>
</BODY>
EOF
fi
#
# This relies on 
# # A start on getting nupshot invoked on alog files
# application/nupshot            alog
# in the .mime.type file and
# application/nupshot; /usr/local/mpi/bin/nupshot %s
# in the .mailcap file
if [ -s profile.alog ] ; then
    if [ ! -s .htaccess ] ; then
        # Note in principle the .htaccess is needed only at the
        # root (say in src).
        echo "You need an .htaccess file containing"
        echo "Addtype application/x-nupshot .alog"
        echo "(See http://www.cs.wisc.edu/docs/mime.html)"
    fi
    cat >> access.html <<EOF
<LI> <A HREF="profile.alog">Profile</A>
<BR><FONT SIZE=-1>requires nupshot</FONT>
EOF
fi
if [ -s profile.lst ] ; then 
    # This allows a group of profiles, with separate titles
    # The file format is filename title
    /bin/rm -f profile.html
    echo '<LI> <A HREF="profile.html" TARGET="mainwindow">List of profiles</A>' >> access.html
    echo '<BODY BGCOLOR="FFFFFF">' >> profile.html
    echo "<MENU>" >> profile.html
    sed -e 's%\([^ ]*\) *\(.*\)$%<LI><A HREF="\1">\2</A>%g' profile.lst >> \
	profile.html
    echo "</MENU>" >> profile.html
    echo '</BODY>' >> profile.html
fi

cat >> access.html <<EOF
<HR>
<LI> <A HREF="$rootloc/contents.html" TARGET="mainwindow">List of $Exercises</A>
EOF
#
# Generate refs to MPI by default, but allow user to provide
if [ -z "$reftxt" ] ; then
    cat >> access.html <<EOF
<HR>
<LI> <A HREF="http://www.mcs.anl.gov/mpi/mpi-report-1.1/mpi-report.html" TARGET="mainwindow">MPI Standard 1.1</A>
<LI> <A HREF="http://www.mcs.anl.gov/mpi/indexold.html" TARGET="mainwindow">MPI Home Page</A>
EOF
else
    cat $reftxt >> access.html
cat >> access.html <<EOF
</MENU>
</BODY>
EOF
fi
#
# Write the various files
# assign (Assignment)
cat >assign.html <<EOF
<TITLE>$title</TITLE>
<BODY BGCOLOR="FFFFFF">
EOF
cat $assign | $mapfilter >> assign.html 2>/dev/null
if [ -s routines.txt ] ; then 
    cat >> assign.html <<EOF
<P>
You may want to use these MPI routines in your solution:<BR>
EOF
    cat routines.txt | $mapfilter >> assign.html 2>/dev/null
fi
cat >> assign.html <<EOF
</BODY>
EOF
#
# output
if [ -s output.txt ] ; then 
    cat >output.html <<EOF
<TITLE>Output</TITLE>
<BODY BGCOLOR="FFFFFF">
<PRE>
EOF
cat output.txt | $mapfilter >> output.html 2>/dev/null
cat >> output.html <<EOF
</PRE>
</BODY>
EOF
fi
#
# "notes"
if [ -s notes ] ; then 
    cat >notes.html <<EOF
<TITLE>Sample Run Script</TITLE>
<BODY BGCOLOR="FFFFFF">
<PRE>
EOF
cat notes | $mapfilter >> notes.html 2>/dev/null
cat >> notes.html <<EOF
</PRE>
</BODY>
EOF
fi
#
if [ -s Makefile ] ; then 
    cat >Makefile.html <<EOF
<TITLE>Makefile</TITLE>
<BODY BGCOLOR="FFFFFF">
<PRE>
EOF
cat Makefile | $mapfilter >> Makefile.html 2>/dev/null
cat >> Makefile.html <<EOF
</PRE>
</BODY>
EOF
fi
#
# solution
/bin/rm -f solution.txt
/bin/rm -f solution.html
cat > solution.html <<EOF
<TITLE>A Solution to $title</TITLE>
<BODY BGCOLOR="FFFFFF">
<PRE>
EOF
#
# This needs to change <,  >, and & into the &lt;, &gt;, and &amp; 
# before invoking the mapfilter
# We also concatenate the source files into solution.txt to simplify
# downloading
for file in `cat solution.lst` ; do
    cat $file | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' | \
	$mapfilter >> solution.html 2>/dev/null
    cat $file >> solution.txt
done
cat >> solution.html <<EOF
</PRE>
</BODY>
EOF
#
# Comments
if [ -f comment.txt ] ; then 
    /bin/rm -f comments.html
    cat > comments.html <<EOF
<TITLE>Comments on $title</TITLE>
<BODY BGCOLOR="FFFFFF">
EOF
cat comment.txt | $mapfilter >> comments.html 2>/dev/null
cat >> comments.html <<EOF
</BODY>
EOF
fi
