# -*- shell-script -*-
# @configure_input@
#
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
# 2005, 2006, 2007, 2008, 2009, 2011 Free Software Foundation, Inc.
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

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

# Be more Bourne compatible.
# (Snippet copied from configure's initialization in Autoconf 2.64)
DUALCASE=1; export DUALCASE # for MKS sh
if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
  emulate sh
  NULLCMD=:
  # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which
  # is contrary to our usage.  Disable this feature.
  alias -g '${1+"$@"}'='"$@"'
  setopt NO_GLOB_SUBST
else
  case `(set -o) 2>/dev/null` in *posix*) set -o posix;; esac
fi

# A single whitespace character.
sp=' '
# A tabulation character.
tab='	'
# A newline character.
nl='
'

# As autoconf-generated configure scripts do, ensure that IFS
# is defined initially, so that saving and restoring $IFS works.
IFS=$sp$tab$nl

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

if test -z "$srcdir"; then
   # compute $srcdir.
   srcdir=`echo "$0" | sed -e 's,/[^\\/]*$,,'`
   test "$srcdir" = $0 && srcdir=.
else :; fi

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

# The name of the current test (without the `.test' suffix).
# Test scripts can override it if they need to (but this should
# be done carefully, and *before* including ./defs).
if test -z "$me"; then
  me=`echo "$0" | sed -e 's,.*[\\/],,;s/\.test$//'`
elif env | grep '^me=' >/dev/null; then
  echo "$0: variable \`me' is set in the environment: this is unsafe" >&2
  exit 99
fi

# Check that the environment is properly sanitized.
for var in required parallel_tests; do
  if env | grep "^$var=" >/dev/null; then
    echo "$me: variable \`$var' is set in the environment:" \
         "this is unsafe" >&2
    exit 99
  fi
done
unset var

# This might be used in testcases checking distribution-related features.
# Test scripts are free to override this if they need to.
distdir=$me-1.0

APIVERSION='@APIVERSION@'
PATH_SEPARATOR='@PATH_SEPARATOR@'

# Make sure we override the user shell.
SHELL='@SHELL@'
export SHELL
# User can override various tools used.
MAKE=${AM_TESTSUITE_MAKE-${MAKE-'make'}}
test -z "$PERL" && PERL='@PERL@'
test -z "$AUTOCONF" && AUTOCONF="@am_AUTOCONF@"
test -z "$AUTOM4TE" && AUTOM4TE="@am_AUTOM4TE@"
test -z "$AUTORECONF" && AUTORECONF="@am_AUTORECONF@"
test -z "$AUTOHEADER" && AUTOHEADER="@am_AUTOHEADER@"
test -z "$AUTOUPDATE" && AUTOUPDATE="@am_AUTOUPDATE@"
test -z "$MISSING" && MISSING=`pwd`/../lib/missing

# This is a hack to seamlessly support the infamous "autoconf wrappers",
# that might dispatch different autoconf versions depending on the name
# of the input files and/or the command-line options used.  See:
# <http://lists.gnu.org/archive/html/automake/2011-12/msg00039.html>
# FIXME: in the long run, the better fix will be to convert our testsuite
# to use `configure.ac' instead of `configure.in' as autoconf input.
AUTOCONF="$AUTOCONF -B /no/such/dir"
AUTOM4TE="$AUTOM4TE -B /no/such/dir"
AUTORECONF="$AUTORECONF -B /no/such/dir"

# Use -Werror because this also turns some Perl warnings into error.
# (Tests for which this is inappropriate should use -Wno-error.)
test -z "$ACLOCAL" && ACLOCAL="aclocal-$APIVERSION -Werror"

# See how Automake should be run.  We put --foreign as the default
# strictness to avoid having to create lots and lots of files.  A test
# can override this by specifying a different strictness.  Use -Wall
# -Werror by default.  Tests for which this is inappropriate
# (e.g. when testing that a warning is enabled by a specific switch)
# should use -Wnone or/and -Wno-error
test -z "$AUTOMAKE" && AUTOMAKE="automake-$APIVERSION --foreign -Werror -Wall"

PATH="`pwd`$PATH_SEPARATOR$PATH"
echo "$PATH"
# Some shells forget to export modified environment variables.
# (See note about `export' in the Autoconf manual.)
export PATH

# We use a trap below for cleanup.  This requires us to go through
# hoops to get the right exit status transported through the signal.
# So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
# Turn off errexit here so that we don't trip the bug with OSF1/Tru64
# sh inside this function.
Exit ()
{
  set +e
  (exit $1)
  exit $1
}

# Print warnings (e.g., about skipped and failed tests) to this file
# number.  Override by putting, say:
#   stderr_fileno_=9; export stderr_fileno_; exec 9>&2;
# in the definition of AM_TESTS_ENVIRONMENT.
# This is useful when using automake's parallel tests mode, to print the
# reason for skip/failure to console, rather than to the *.log files.
: ${stderr_fileno_=2}

# Copied from Gnulib's `tests/init.sh'.
warn_ () { echo "$@" 1>&$stderr_fileno_; }
fail_ () { warn_ "$me: failed test: $@"; Exit 1; }
skip_ () { warn_ "$me: skipped test: $@"; Exit 77; }
fatal_ () { warn_ "$me: hard error: $@"; Exit 99; }
framework_failure_ () { warn_ "$me: set-up failure: $@"; Exit 99; }

# cross_compiling
# ---------------
# Tell whether we are cross-compiling.  This is especially useful to skip
# tests (or portions of them) that requires a native compiler.
cross_compiling ()
{
  test x"$host_alias" != x
}

# So that we can force the use of correct gcc, g++ etc., consistently
# with cross-compilation settings.
if cross_compiling; then
  am__tool_prefix="$host_alias-"
else
  am__tool_prefix=
fi

for tool in : $required
do
  # Check that each required tool is present.
  case $tool in
    :) ;;
    bison|yacc)
      # Since bison is required, we pick YACC for ./configure.
      YACC='bison -y'
      export YACC
      echo "$me: running bison --version"
      ( bison --version ) || exit 77
      ;;
    flex|lex)
      # Since flex is required, we pick LEX for ./configure.
      LEX=flex
      export LEX
      echo "$me: running flex --version"
      flex --version || exit 77
      ;;
    cc|c++|fortran|fortran77)
      echo "$me: dummy requirement '$tool', no check done"
      ;;
    bzip2)
      # Do not use --version, bzip2 still tries to compress stdin.
      echo "$me: running bzip2 --help"
      ( bzip2 --help ) || exit 77
      ;;
    cl)
      CC=cl
      export CC
      echo "$me: running $CC -?"
      ( $CC -? ) || exit 77
      ;;
    etags)
      # Exuberant Ctags will create a TAGS file even
      # when asked for --help or --version.  (Emacs's etags
      # does not have such problem.)  Use -o /dev/null
      # to make sure we do not pollute the tests/ directory.
      echo "$me: running etags --version -o /dev/null"
      ( etags --version -o /dev/null ) || exit 77
      ;;
    GNUmake)
      # Use --version AND -v, because SGI Make doesn't fail on --version.
      # Also grep for GNU because newer versions of FreeBSD make do
      # not complain about `--version' (they seem to silently ignore it).
      echo "$me: running $MAKE --version -v | grep GNU"
      ( $MAKE --version -v | grep GNU ) || exit 77
      ;;
    gcc)
      # When gcc is required, export `CC=gcc' so that ./configure
      # always use it.  This is important only when the user
      # has defined CC in his environment, otherwise ./configure will
      # prefer gcc to other compilers.
      CC=${am__tool_prefix}gcc
      export CC
      echo "$me: running $CC --version"
      ( $CC --version ) || exit 77
      ;;
    gcj)
      GCJ=${am__tool_prefix}gcj
      export GCJ
      echo "$me: running $GCJ --version"
      ( $GCJ --version ) || exit 77
      ( $GCJ -v ) || exit 77
      ;;
    g++)
      CXX=${am__tool_prefix}g++
      export CXX
      echo "$me: running $CXX --version"
      ( $CXX --version ) || exit 77
      ;;
    gfortran)
      FC=${am__tool_prefix}gfortran
      export FC
      echo "$me: running $FC --version"
      $FC --version || skip_ "GNU Fortran compiler not available"
      echo "$me: running $FC -v"
      $FC -v || skip_ "botched installation for GNU Fortran compiler"
      case " $required " in
        *\ g77\ *) ;;
        *) F77=$FC; export F77;;
      esac
      ;;
    g77)
      F77=${am__tool_prefix}g77
      export F77
      echo "$me: running $F77 --version"
      $F77 --version || skip_ "GNU Fortran 77 compiler not available"
      echo "$me: running $F77 -v"
      $F77 -v || skip_ "botched installation for GNU Fortran 77 compiler"
      case " $required " in
        *\ gfortran\ *) ;;
        *) FC=$F77; export FC;;
      esac
      ;;
    icc)
      CC=icc
      export CC
      # There is no way to ask *only* the compiler's version.
      # This tool always wants to do something (by default
      # it will try link *nothing* and complain it cannot find
      # main(); funny).  Use -help so it does not try linking anything.
      echo "$me: running $CC -V -help"
      ( $CC -V -help ) || exit 77
      ;;
    javac)
      # The Java compiler from JDK 1.5 (and presumably earlier versions)
      # cannot handle the `-version' option by itself: it bails out
      # telling that source files are missing.  Adding also the `-help'
      # option seems to solve the problem.
      echo "$me: running javac -version -help"
      javac -version -help || exit 77
      ;;
    lib)
      AR=lib
      export AR
      # Attempting to create an empty archive will actually not
      # create the archive, but lib will output its version.
      echo "$me: running $AR -out:defstest.lib"
      $AR -out:defstest.lib || skip_ "Microsoft \`lib' utility not available"
      ;;
    makedepend)
      echo "$me: running makedepend -f-"
      ( makedepend -f- ) || exit 77
      ;;
    makeinfo-html)
      # Make sure makeinfo understands --html.
      echo "$me: running makeinfo --html --version"
      ( makeinfo --html --version ) || exit 77
      ;;
    non-root)
      # Skip this test case if the user is root.
      # We try to append to a read-only file to detect this.
      priv_check_temp=priv-check.$$
      touch $priv_check_temp || exit 1
      chmod a-w $priv_check_temp || exit 1
      (echo foo >> $priv_check_temp) >/dev/null 2>&1
      overwrite_status=$?
      rm -f $priv_check_temp
      test $overwrite_status = 0 && exit 77
      ;;
    native)
      cross_compiling && skip_ "doesn't work in cross-compile mode"
      ;;
    python)
      # Python doesn't support --version, it has -V
      echo "$me: running python -V"
      ( python -V ) || exit 77
      ;;
    ro-dir)
      # Skip this test case if read-only directories aren't supported
      # (e.g., under DOS.)
      ro_dir_temp=ro_dir.$$
      mkdir $ro_dir_temp || exit 1
      chmod a-w $ro_dir_temp || exit 1
      (: > $ro_dir_temp/probe) >/dev/null 2>/dev/null
      create_status=$?
      rm -rf $ro_dir_temp
      test $create_status = 0 && exit 77
      ;;
    rst2html)
      # Try the variants that are tried in check.am.
      while :; do
	for r2h in $RST2HTML rst2html rst2html.py; do
	  echo "$me: running $r2h --version"
	  $r2h --version && break 2
	done
	exit 77
      done
      ;;
    runtest)
      # DejaGnu's runtest program. We rely on being able to specify
      # the program on the runtest command-line. This requires
      # DejaGnu 1.4.3 or later.
      echo "$me: running runtest --version"
      (runtest SOMEPROGRAM=someprogram --version) || exit 77
      ;;
    tex)
      # No all versions of Tex support `--version', so we use
      # a configure check.
      test -n "@TEX@" || exit 77
      ;;
    texi2dvi-o)
      # Texi2dvi supports `-o' since Texinfo 4.1.
      echo "$me: running texi2dvi -o /dev/null --version"
      ( texi2dvi -o /dev/null --version ) || exit 77
      ;;
    xsi-shell)
      # Try some XSI features.
      echo "$me: trying some XSI constructs"
      ( _am_dummy="a/b/c"
	test "${_am_dummy##*/},${_am_dummy%/*},${_am_dummy#??}"${_am_dummy%"$_am_dummy"}, \
		= c,a/b,b/c, \
	&& eval 'test $(( 1 + 1 )) -eq 2 \
	&& test "${#_am_dummy}" -eq 5' ) || exit 77
      ;;
    # Generic case: the tool must support --version.
    *)
      echo "$me: running $tool --version"
      ( $tool --version ) || exit 77
      ;;
  esac
done


# 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

curdir=`pwd`
testSubDir=$me.dir
test ! -d $testSubDir || {
  find $testSubDir -type d ! -perm -700 -exec chmod u+rwx {} ";"
  rm -rf $testSubDir
}
mkdir $testSubDir

sh_errexit_works=@sh_errexit_works@
if test "$sh_errexit_works" = yes; then
  trap 'exit_status=$?
    set +e
    cd "$curdir"
    case $exit_status,$keep_testdirs in
    0,)
      find $testSubDir -type d ! -perm -700 -exec chmod u+rwx {} ";"
      rm -rf $testSubDir
      ;;
    esac
    test "$signal" != 0 &&
      echo "$me: caught signal $signal"
    echo "$me: exit $exit_status"
    exit $exit_status
  ' 0
  for signal in 1 2 13 15; do
    trap 'signal='$signal'; { Exit 1; }' $signal
  done
fi
signal=0

# Copy in some files we need.
for file in install-sh missing depcomp; do
   cp "$srcdir/../lib/$file" "$testSubDir/$file" || Exit 1
done

cd ./$testSubDir

# Build appropriate environment in test directory.  Eg create
# configure.in, touch all necessary files, etc.
# Don't use AC_OUTPUT, but AC_CONFIG_FILES so that appending
# still produces a valid configure.ac.  But then, tests running
# config.status really need to append AC_OUTPUT.
{
  echo "AC_INIT([$me], [1.0])"
  if test x"$parallel_tests" = x"yes"; then
    echo "AM_INIT_AUTOMAKE([parallel-tests])"
  else
    echo "AM_INIT_AUTOMAKE"
  fi
  echo "AC_CONFIG_FILES([Makefile])"
} >configure.in

# Unset some make-related variables that may cause $MAKE to act like
# a recursively invoked sub-make.  Any $MAKE invocation in a test is
# conceptually an independent invocation, not part of the main
# 'automake' build.
unset MFLAGS MAKEFLAGS AM_MAKEFLAGS MAKELEVEL
unset __MKLVL__ MAKE_JOBS_FIFO                     # For BSD make.
unset DMAKE_CHILD DMAKE_DEF_PRINTED DMAKE_MAX_JOBS # For Solaris dmake.
# Unset verbosity flag.
unset V
# Also unset variables that will let `make -e install' divert
# files into unwanted directories.
unset DESTDIR
unset prefix exec_prefix bindir datarootdir datadir docdir dvidir
unset htmldir includedir infodir libdir libexecdir localedir mandir
unset oldincludedir pdfdir psdir sbindir sharedstatedir sysconfdir
# Unset variables that might change the "make distcheck" behaviour.
unset DISTCHECK_CONFIGURE_FLAGS AM_DISTCHECK_CONFIGURE_FLAGS
# Also unset variables that control our test driver.  While not
# conceptually independent, they cause some changed semantics we
# need to control (and test for) in some of the tests to ensure
# backward-compatible behavior.
unset TESTS_ENVIRONMENT AM_TESTS_ENVIRONMENT
unset DISABLE_HARD_ERRORS
unset AM_COLOR_TESTS
unset TESTS
unset TEST_LOG_COMPILER
unset TEST_LOGS
unset RECHECK_LOGS
unset VERBOSE

echo "=== Running test $0"

# We might need extra macros, e.g., from Libtool or Gettext.
case " $required " in *\ libtool*) . ../libtool-macros.dir/get.sh;; esac
case " $required " in *\ gettext*) . ../gettext-macros.dir/get.sh;; esac

testaclocaldir='@abs_top_srcdir@/m4'

# 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

# An old timestamp that can be given to a file, in "touch -t" format.
# The time stamp should be portable to all file systems of interest.
# Just for fun, choose the exact time of the announcement of the GNU project
# in UTC; see <http://www.gnu.org/gnu/initial-announcement.html>.
old_timestamp=198309271735.59

# is_newest FILE FILES
# --------------------
# Return false if any file in FILES is newer than FILE.
# Resolve ties in favor of FILE.
is_newest ()
{
  is_newest_files=`find "$@" -prune -newer "$1"`
  test -z "$is_newest_files"
}

# using_gmake
# -----------
# Return success if $MAKE is GNU make, return failure otherwise.
# Caches the result for speed reasons.
using_gmake ()
{
  case $am__using_gmake in
    yes)
      return 0;;
    no)
      return 1;;
    '')
      # Use --version AND -v, because SGI Make doesn't fail on --version.
      # Also grep for GNU because newer versions of FreeBSD make do
      # not complain about `--version' (they seem to silently ignore it).
      if $MAKE --version -v | grep GNU; then
        am__using_gmake=yes
        return 0
      else
        am__using_gmake=no
        return 1
      fi;;
    *)
      fatal_ "invalid value for \$am__using_gmake: '$am__using_gmake'";;
  esac
}
am__using_gmake="" # Avoid interferences from the environment.

# AUTOMAKE_run status [options...]
# --------------------------------
# Run Automake with OPTIONS, and fail if automake
# does not exit with STATUS.
AUTOMAKE_run ()
{
  expected_exitcode=$1
  shift
  exitcode=0
  $AUTOMAKE ${1+"$@"} >stdout 2>stderr || exitcode=$?
  cat stderr >&2
  cat stdout
  test $exitcode = $expected_exitcode || Exit 1
}

# AUTOMAKE_fails [options...]
# ---------------------------
# Run Automake with OPTIONS, and fail if automake
# does not exit with STATUS.
AUTOMAKE_fails ()
{
  AUTOMAKE_run 1 ${1+"$@"}
}

commented_sed_unindent_prog='
  /^$/b                    # Nothing to do for empty lines.
  x                        # Get x<indent> into pattern space.
  /^$/{                    # No prior x<indent>, go prepare it.
    g                      # Copy this 1st non-blank line into pattern space.
    s/^\(['"$tab"' ]*\).*/x\1/   # Prepare x<indent> in pattern space.
  }                        # Now: x<indent> in pattern and <line> in hold.
  G                        # Build x<indent>\n<line> in pattern space, and
  h                        # duplicate it into hold space.
  s/\n.*$//                # Restore x<indent> in pattern space, and
  x                        # exchange with the above duplicate in hold space.
  s/^x\(.*\)\n\1//         # Remove leading <indent> from <line>.
  s/^x.*\n//               # Restore <line> when there is no leading <indent>.
'

# unindent [input files...]
# -------------------------
# Remove the "proper" amount of leading whitespace from the given files,
# and output the result on stdout.  That amount is determined by looking
# at the leading whitespace of the first non-blank line in the input
# files.  If no input file is specified, standard input is implied.
unindent ()
{
  if test x"$sed_unindent_prog" = x; then
    sed_unindent_prog=`printf '%s\n' "$commented_sed_unindent_prog" | sed -e "s/  *# .*//"`
  fi
  sed "$sed_unindent_prog" ${1+"$@"}
}
sed_unindent_prog="" # Avoid interferences from the environment.

# Turn on shell traces.
set -x

pwd
