#!/bin/sh

if [ "$1" = "top" -o "$1" = "" ]; then
echo " "
echo "Preparing all tex-files in top level of the Manual"
files="$AGDROOT/Manual/MANUAL/*.tex"
for f in $files
do   
   core=`basename $f .tex`
   echo "preparing $core"
   cp $f $core.mkhtml
   perl -p -i.bak -e 's/\\</</g;s/\\>/>/g;' $core.mkhtml
   perl $AGDROOT/Manual/cmd/handle_refs.pl $core.mkhtml > $core.tex
done
\rm -f *.mkhtml *.mkhtml.bak
fi # $1 != low

if [ "$1" = "low" -o "$1" = "" ]; then
\rm -r -f extract
mkdir extract
cd extract
notangle -Rnamereplacementtable $AGDROOT/Manual/noweb/ext.nw \
  > namereplacementtable

if [ -x /usr/local/bin/grep ]; then
  grep_cmd=/usr/local/bin/grep
elif [ -x /usr/bin/grep ]; then
  grep_cmd=/usr/bin/grep
else
  grep_cmd=grep
fi

echo "Extending namereplacementtable by Manpage entries"
files="$AGDROOT/incl/AGD/*.h"
for f in $files
do
  core=`basename $f .h`
  # printf "$core "
  printf "."
  perl $AGDROOT/Manual/cmd/extract_mtype.pl $f >> namereplacementtable
done

echo " "
echo "Preparing all tex-files for AGD types"
files="$AGDROOT/incl/AGD/*.h"
for f in $files
do
  if $grep_cmd -q Manpage $f; then
   core=`basename $f .h`
   echo "preparing tex-file for $core"
   perl $AGDROOT/Manual/cmd/ext.pl HTMLext $f outfile=$core.tex \
   informational=no warnings=log indexing=yes includefile=yes
  fi
done
cd ..

fi # 

echo "calling latex MANUAL.tex"
echo " "

latex MANUAL.tex
bibtex MANUAL
latex MANUAL.tex
latex MANUAL.tex
dvips MANUAL

echo "please ensure that your path does not contain dots!!!"
echo "calling latex2html MANUAL.tex"
echo ""

\rm -rf MANUAL

if [ "$1" = "low" -o "$1" = "top" ]; then
  shift
fi

notangle -R.latex2html-init $AGDROOT/Manual/noweb/ext.nw \
  > .latex2html-init # this gives flags
latex2html MANUAL.tex $*

cd MANUAL
mv Index.html bindex.html
perl -p -i -e 's/Index\.html/bindex\.html/g;' *.html
exit

