# -*- shell-script -*-
# @configure_input@
#
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
# Free Software Foundation, Inc.
#
# This file is part of GNU Automake.
#
# GNU Automake is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# GNU Automake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with autoconf; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

# Defines for Automake testing environment.
# Tom Tromey <tromey@cygnus.com>

# Be Bourne compatible.
# (Snippet copied from configure's initialization in Autoconf 2.57.)
if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
  emulate sh
  NULLCMD=:
  # Zsh 3.x and 4.x performs word splitting on ${1+"$@"}, which
  # is contrary to our usage.  Disable this feature.
  alias -g '${1+"$@"}'='"$@"'
elif test -n "${BASH_VERSION+set}" && (set -o posix) >/dev/null 2>&1; then
  set -o posix
fi

# Ensure we are running from the right directory.
test -f ./defs || {
   echo "defs: not found in current directory" 1>&2
   exit 1
}

# If srcdir is not set, then we are not running from `make check', be verbose.
if test -z "$srcdir"; then
   VERBOSE=x
   # compute $srcdir.
   srcdir=`echo "$0" | sed -e 's,/[^\\/]*$,,'`
   test $srcdir = $0 && srcdir=.
fi

# Ensure $srcdir is set correctly.
test -f $srcdir/defs.in || {
   echo "$srcdir/defs.in not found, check \$srcdir" 1>&2
   exit 1
}

me=`echo "$0" | sed -e 's,.*[\\/],,;s/\.test$//'`

# See how redirections should work.  User can set VERBOSE to see all
# output.
test -z "$VERBOSE" && {
   exec > /dev/null 2>&1
}

# Make sure we override the user shell.
SHELL='@SHELL@'
export SHELL
test -z "$TEXI2DVI" && TEXI2DVI=`cd $srcdir && pwd`/texi2dvi

echo $PATH
# Some shells forget to export modified environment variables.
# (See note about `export' in the Autoconf manual.)
export PATH
# User can override various tools used.
test -z "$PERL" && PERL='@PERL@'

if test -n "$required"
then
  for tool in $required
  do
    # Check that each required tool is present.
    case $tool in
      tex)
        # No all versions of Tex support `--version', so we use
        # a configure check.
        test -n "@TEX@" || exit 77
	;;
      # Generic case: the tool must support --version.
      *)
	echo "$me: running $tool --version"
	( $tool --version ) || exit 77
	;;
    esac
  done
fi


# Always use an absolute srcdir.  Otherwise symlinks made in subdirs
# of the test dir just won't work.
case "$srcdir" in
 [\\/]* | ?:[\\/]*)
    ;;

 *)
    srcdir=`CDPATH=: && cd "$srcdir" && pwd`
    ;;
esac

chmod -R a+rwx testSubDir > /dev/null 2>&1
rm -rf testSubDir > /dev/null 2>&1
mkdir testSubDir

# Copy in some files we need.
cp $srcdir/../doc/texinfo.tex testSubDir || exit 1

cd ./testSubDir

# Build appropriate environment in test directory.
cat > input.texi << END
\input texinfo   @c -*-texinfo-*-
@setfilename input
@settitle input

@ifnottex
@node Top
@top input
@end ifnottex

END

unset TEXI2DVI_BUILD_MODE
unset TEXI2DVI_BUILD_DIRECTORY

echo "=== Running test $0"

# POSIX no longer requires 'egrep' and 'fgrep',
# but some hosts lack 'grep -E' and 'grep -F'.
EGREP='@EGREP@'
FGREP='@FGREP@'

# The amount we should wait after modifying files depends on the platform.
# For instance, Windows '95, '98 and ME have 2-second granularity
# and can be up to 3 seconds in the future w.r.t. the system clock.
sleep='sleep @MODIFICATION_DELAY@'

# The tests call `make -e' but we do not want $srcdir from the environment
# to override the definition from the Makefile.
testsrcdir=$srcdir
unset srcdir

# TEXI2DVI_run status [options...]
# --------------------------------
# Run texi2dvi with OPTIONS, and fail if does not exit with STATUS.
TEXI2DVI_run ()
{
  expected_exitcode=$1
  shift
  exitcode=0
  if test -n "$VERBOSE"; then
    set x --debug --verbose ${1+"$@"}
    shift
  fi
  which texi2dvi
  texi2dvi ${1+"$@"} 2>stderr >stdout || exitcode=$?
  cat stderr
  cat stdout
  test $exitcode = $expected_exitcode || exit 1
}

# TEXI2DVI_fail [options...]
# ---------------------------
# Run texi2dvi with OPTIONS, and fail if does not exit with failure.
TEXI2DVI_fail ()
{
  TEXI2DVI_run 1 ${1+"$@"}
}

# TEXI2DVI_pass [options...]
# ---------------------------
# Run texi2dvi with OPTIONS, and fail if does not exit with failure.
TEXI2DVI_pass ()
{
  TEXI2DVI_run 0 ${1+"$@"}
}

# list_files ()
# -------------
# Return the space separated sorted list of files in this directory,
# except the "invisible" ones.
list_files ()
{
  echo `for f in *
  do
    case $f in
      texinfo.tex | stdout | stderr);;
      *) echo $f;;
    esac
  done | sort`
}

# Turn on shell traces when VERBOSE is set.
if test -n "$VERBOSE"; then
  set -x
else
  :
fi
pwd
