#! /bin/sh
#
# Assemble the C and Fortran versions of the basic course
#
next=
prev=
parent=
do_contents=1
is_first=0
is_last=0
srclist=
makepage=/home/MPI/class/mpiexmpl/maint/makepage
#makepage=`pwd`/maint/makepage
#if [ ! -x "$makepage" ] ; then
#    makepage=/ump/icase/home25/gropp/trial/byoc/maint
#fi
#
# srctree allows us to have several exercise "groups"
srctree=src
#
assign_list="hellow bcast ring pi fairness exchange jacobi collect jacobicmpl endlist"
rootloc="file:/home/MPI/class/mpiexmpl"
#
Exercises="Exercises"
#
lang="/C"
nextdir="../../"
#
# 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=//'` ;;
	-src=*)
	srctree=`echo a$arg | sed 's/a-src=//'` ;;
	-srclist=*)
	assign_list=`echo a$arg | sed 's/a-srclist=//'` 
	;;
#	-srcdir=*)
#	srctree=`echo a$arg | sed 's/a-srcdir=//'` 
#	;;
	-title=*)
	src_title=`echo a$arg | sed 's/a-title=//'` 
	;;
	-maintitle=*)
	MainTitle=`echo a$arg | sed 's/a-maintitle=//'` 
	;;
	-lang=*)  
	# C/, Fortran/, or <empty>
	lang=`echo a$arg | sed 's/a-lang=//'` 
	if [ -z "$lang" ] ; then nextdir="../" ; fi
	;;
	-echo) set -x ; example="$example $arg" ;;
	-nocontents|-no_contents) do_contents=0 ;;
	-contents) do_contents=1 ;;
	-islast|-is_last) is_last=1 ;;
	-isfirst|-is_first) is_first=1 ;;
	-example)
	Exercises="Examples"
	example="$example -example"
	;;
	-reftxt=*|-map=*)
	# Pass these args to makepage
	example="$example $arg"
	;;
	*) echo "Unrecognized arg $arg"
	   exit 1 ;;
    esac
done

case "$srctree" in 
    src)
    assign_list="hellow bcast ring pi fairness exchange jacobi collect jacobicmpl endlist"
    src_title="Simple Parallel Data Structures"
    is_first=1
    ;;
    src2)
    assign_list="io ioserv endlist"
    src_title="Master/slave"
    ;;
    src3)
    assign_list="memcpy pingpong buflimit barrier vector 3way circulate irecv jacobi endlist"
    src_title="Performance tuning MPI"
    is_last=1
    ;;
    *)
    if [ -z "$assign_list" ] ; then 
        echo "Unknown source tree $srctree"
        exit 1
    else 
        assign_list="$assign_list endlist"
    fi
    ;;
esac

if [ -z "$MainTitle" ] ; then
    MainTitle="MPI $Exercises"
fi
if [ $do_contents = 1 -a $is_first = 1 ] ; then
	    /bin/rm -f contents.html
	    cat >> contents.html <<EOF
<TITLE>$MainTitle</TITLE>
<BODY BGCOLOR="FFFFFF">
EOF
fi

#
# Make the contents list.  This assumes that the src trees are built in 
# order.  If one is updated, then the contents.html file will need to be
# updated by hand.
#
if [ $do_contents = 1 ] ; then 
    echo "<H2>$src_title</H2>" >> contents.html
    if [ -s $srctree/runs.html ] ; then
	cat >> contents.html <<EOF
<A HREF="$rootloc/$srctree/runs.html">Results</A> of running all of the 
exercises in this section are available.	
<P>
EOF
    fi
    echo "<UL>" >> contents.html
fi
#
for file in $assign_list ; do
    if [ ! -s $srctree/$file${lang}/title.txt ] ; then
        continue
    fi
    title_text=`cat $srctree/$file${lang}/title.txt`
    if [ $do_contents = 1 ] ; then 
	cat >> contents.html <<EOF
<LI> <A HREF="$rootloc/$srctree/$file${lang}/main.html" TARGET="_top">$title_text</A>
EOF
        if [ -s $srctree/$file${lang}/related.lst ] ; then
            echo "<UL>" >> contents.html
            for rfile in `cat $srctree/$file${lang}/related.lst` ; do
	        title_text=`cat $srctree/$file${lang}/$rfile/title.txt`
		cat >> contents.html <<EOF
<LI> <A HREF="$rootloc/$srctree/$file${lang}/$rfile/main.html" TARGET="_top">$title_text</A>
EOF
	    done
	    echo "</UL>" >> contents.html
	fi
    fi
done
if [ $do_contents = 1 ] ; then 
    echo "</UL>" >> contents.html
    if [ "$is_last" = 1 ] ; then 
        echo "</BODY>" >> contents.html
    fi
fi
#
# Make the individual pages
for file in $assign_list ; do
    oldprev=$prev
    prev=$next
    next=$file
    if [ -d $srctree/$prev${lang} ] ; then 
	echo "Processing directory $srctree/$prev${lang}"
	if [ -n "$oldprev" ] ; then 
  	    prevarg="-prev=${nextdir}$oldprev${lang}"
        fi
        if [ ! -d $srctree/$next${lang} ] ; then
	    nextarg=
	else
	    nextarg="-next=${nextdir}$next${lang}"
	fi
        (cd $srctree/$prev${lang} ; \
        $makepage $nextarg $prevarg -root=$rootloc $example )
    fi
done
