#! /bin/sh
# Copyright (C) 2011-2025 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 <https://www.gnu.org/licenses/>.

# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.

# This file is maintained in Automake, please report
# bugs to <bug-automake@gnu.org> or send patches to
# <automake-patches@gnu.org>.

scriptversion=2025-06-18.21; # UTC

# Make unconditional expansion of undefined variables an error.  This
# helps a lot in preventing typo-related bugs.
set -u

me=tap-driver.sh

fatal ()
{
  echo "$me: fatal: $*" >&2
  exit 1
}

usage_error ()
{
  echo "$me: $*" >&2
  print_usage >&2
  exit 2
}

print_usage ()
{
  cat <<END
Usage:
  tap-driver.sh --test-name NAME --log-file PATH --trs-file PATH
                [--expect-failure {yes|no}] [--color-tests {yes|no}]
                [--enable-hard-errors {yes|no}] [--ignore-exit]
                [--diagnostic-string STRING] [--merge|--no-merge]
                [--stderr-prefix STRING] [--comments|--no-comments]
                [--] TEST-COMMAND
The '--test-name', '--log-file' and '--trs-file' options are mandatory.

Report bugs to <bug-automake@gnu.org>.
GNU Automake home page: <https://www.gnu.org/software/automake/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
END
}

# TODO: better error handling in option parsing (in particular, ensure
# TODO: $log_file, $trs_file and $test_name are defined).
test_name= # Used for reporting.
log_file=  # Where to save the result and output of the test script.
trs_file=  # Where to save the metadata of the test run.
expect_failure=0
color_tests=0
merge=0
stderr_prefix=
ignore_exit=0
comments=0
diag_string='#'
while test $# -gt 0; do
  case $1 in
  --help) print_usage; exit $?;;
  --version) echo "$me (GNU Automake) $scriptversion"; exit $?;;
  --test-name) test_name=$2; shift;;
  --log-file) log_file=$2; shift;;
  --trs-file) trs_file=$2; shift;;
  --color-tests) color_tests=$2; shift;;
  --expect-failure) expect_failure=$2; shift;;
  --enable-hard-errors) shift;; # No-op.
  --merge) merge=1;;
  --no-merge) merge=0;;
  --stderr-prefix) stderr_prefix=$2; shift;;
  --ignore-exit) ignore_exit=1;;
  --comments) comments=1;;
  --no-comments) comments=0;;
  --diagnostic-string) diag_string=$2; shift;;
  --) shift; break;;
  -*) usage_error "invalid option: '$1'";;
  esac
  shift
done

# Quadrigraph substitutions for `--stderr-prefix'.  Note that the empty
# substitution MUST be done last, otherwise `@%@&t@:@' will become `#', not
# `@%:@'.
for q_r in '@%:@ #' '@&t@ '; do
  q=${q_r%% *} # quadrigraph
  r=${q_r#* } # replacement
  while true; do
    case $stderr_prefix in
    *"$q"*) stderr_prefix=${stderr_prefix%%"$q"*}$r${stderr_prefix#*"$q"};;
    *) break;;
    esac
  done
done

# Prefixes each line of its stdin with the first argument and writes the result
# to stdout.  If the final line of stdin is non-empty and does not end with a
# terminating newline, a newline is added.
prefix_lines() {
  # Implementation note: This function is used to prefix the test script's
  # stderr lines.  Preserving the order of the test script's stdout and stderr
  # lines is important for debugging, so this function is sensitive to input and
  # output buffering.  A shell loop is used to prefix the lines instead of
  # `$AM_TAP_AWK' (which would probably be more efficient) because `mawk'
  # aggressively buffers its input (except with the `-Winteractive' command-line
  # option), which would defeat the purpose of the `--merge' option.  `sed' or
  # `perl' could be used instead of a shell loop, but those would add a
  # dependency to this script.

  # <https://stackoverflow.com/a/6399568> explains `IFS='.  The `||' check
  # ensures that stdin's final line is written to stdout even if it is missing a
  # terminating newline.
  while IFS= read -r line || test -n "$line"; do
    # `printf' is preferred over `echo' because `echo' might process backslash
    # escapes or behave unexpectedly if its argument looks like an option.
    printf %s\\n "$1$line"
  done
}

test $# -gt 0 || usage_error "missing test command"

case $expect_failure in
  yes) expect_failure=1;;
    *) expect_failure=0;;
esac

if test $color_tests = yes; then
  init_colors='
    color_map["red"]="