#!/bin/sh

function die
{
  echo
  echo $1
  exit 1
}

needed_flags=0
needed_texi2html=0
needed_texinfo_tex=0
needed_include_texi=0

if [ -e Flags/usa.txt ]; then
    echo "Good, Flags are there."
else
    echo -n "Checking out Flags..."
    bk edit Flags >/dev/null 2>&1
    echo " Done."
    needed_flags=1
fi

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

if [ -e include.texi ]; then
    echo "Good, include.texi is there."
else
    echo -n "Creating include.texi..."
    bk edit ../configure.in >/dev/null 2>&1
    echo "@c This file was generated by test-make-manual" > include.texi
    echo -n "@set mysql_version " >> include.texi
    grep "AM_INIT_AUTOMAKE(mysql, " ../configure.in | \
    sed -e 's;AM_INIT_AUTOMAKE(mysql, ;;' -e 's;);;' >> include.texi
    echo -n "@set default_port " >> include.texi
    grep "MYSQL_TCP_PORT_DEFAULT=" ../configure.in | \
    sed -e 's;MYSQL_TCP_PORT_DEFAULT=;;' >> include.texi
    echo " Done."
    needed_include_texi=1
fi

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

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


echo -n "Running texi2html..."
/usr/bin/perl ./Support/texi2html -iso -number manual.texi

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


echo -n "Running texi2dvi..."
texi2dvi --batch --quiet manual.texi

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

needed_flags=0
needed_texi2html=0
needed_texinfo_tex=0
needed_include_texi=0

if [ $needed_flags ]; then
    bk clean Flags
fi

if [ $needed_texi2html ]; then
    bk clean Support/texi2html
fi

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

if [ $needed_include_texi ]; then
    rm -f include.texi
fi

[ -z $BROWSER ] && BROWSER=netscape 

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

$BROWSER file://`pwd`/manual_toc.html &
