#!/bin/sh

needed_texi2html=0
needed_texinfo_tex=0

if [ -z $BROWSER ]; then
    BROWSER=netscape 
    echo "BROWSER not set, using $BROWSER"
fi

die ()
{
    echo
    echo $1
    cleanup
    exit 1
}

cleanup ()
{
    echo "Cleaning up..."
    if [ $needed_texi2html ]; then
        bk clean Support/texi2html
    fi

    if [ $needed_texinfo_tex ]; then
        bk clean Support/texinfo.tex
    fi

    for file in \
        faq.aux faq.cp  faq.cps faq.dvi  \
        faq.fn  faq.fns faq.ky  faq.html \
        faq.pg  faq.toc faq.tp  faq.vr   \
        faq.info faq_toc.html faq.log \
        faq*.html;
    do
        rm -f $file
    done
        
}


if [ -e Support/texi2html ]; then
    echo "Good, texi2html is there."
else
    echo -n "Checking out texi2html..."
    bk edit Support/texi2html >/dev/null 2>&1
    echo " Done."
    needed_texi2html=1
fi

if [ -e Support/texinfo.tex ]; then
    echo "Good, texinfo.tex is there."
else
    echo -n "Checking out texinfo.tex..."
    bk edit Support/texinfo.tex >/dev/null 2>&1
    echo " Done."
    needed_texinfo_tex=1
fi

echo -n "Running makeinfo..."
makeinfo --no-split -I . faq.texi

if [ $? != 0 ]; then
    die "faq has errors - fix before you commit"
else
    echo " Looks good."
fi


echo -n "Running texi2html..."
/usr/bin/perl ./Support/texi2html -menu -iso -number -split_chapter -ref_num faq.texi

if [ $? != 0 ]; then
    die "faq has errors - fix before you commit"
else
    echo " Looks good."
fi


echo -n "Running texi2dvi..."
texi2dvi --batch faq.texi > texi2dvi-faq.out

if [ $? != 0 ]; then
    die "faq has errors - fix before you commit (saved in texi2dvi.out)"
else
    rm texi2dvi-faq.out
    echo " Looks good."
fi

echo
echo
echo "Please examine your modifications in \`faq.html'."
echo
echo "If you would like to use a different browser, set the 'BROWSER' environment"
echo "variable." 
echo

$BROWSER file:`pwd`/faq_toc.html

echo "-- Press Enter to Continue --"
read junk
cleanup
