#!/bin/sh

# Script to summarize the results of a GPC Test Suite run
#
# Copyright (C) 1998-2003 Free Software Foundation, Inc.
#
# Authors: Matthias Klose <doko@cs.tu-berlin.de>
#          Frank Heckenbach <frank@pascal.gnu.de>
#
# This file is part of GNU Pascal.
#
# GNU Pascal 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 Pascal 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 GNU Pascal; see the file COPYING. If not, write to the
# Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

# Find a good awk.
if test -z "$AWK"; then
  for AWK in gawk nawk awk ""; do
    if type "$AWK" 2>&1 | grep 'not found' > /dev/null 2>&1; then
      :
    else
      break
    fi
  done
fi
if test -z "$AWK"; then
  echo 'No awk found.' >&2
  exit 1
fi

FFLUSH='fflush("/dev/stdout")'  # useful if the output of this script is piped to another process ...
echo dummy | "$AWK" "{ $FFLUSH }" > /dev/null 2>&1 || FFLUSH=""  # ... but not all awks support it

"$AWK" '
function count () {
  if (lines == "") return;
  num++;
  if (lines ~ /^TEST\t[a-z0-9_.~-]*:\tOK$/)
    num_ok++
  else
    {
      if (lines ~ /^TEST\t[a-z0-9_.~-]*:\tSKIPPED[: \t]/)
        num_skipped++
      else
        num_wrong++
      printf "%s\n", lines
      '"$FFLUSH"'
    }
  lines = ""
}
function summary () {
  if (doit)
    print "Missing end indicator."
  if (!doit && !doend)
    print "Nothing was run."
  else if (num == 0)
    print "No tests were run."
  else
    {
      print ""
      print "# of GPC tests          " num
      print "# of GPC tests passed   " num_ok
      print "# of GPC tests skipped  " num_skipped
      print "# of GPC tests failed   " num_wrong
    }
}
BEGIN                          { num = num_ok = num_skipped = num_wrong = 0 }
num == 0 && /\r$/              { cr = 1 }
cr                             { sub ("\r+$", "") }
/^\1#progress/                 { print; '"$FFLUSH"'; next }
!start && !doit && /^Testing / { print; getline; print }
/^GPC-TEST-BEGIN$/             { start = 1; next }
/^==========================$/ { if (start) doit = 1; else doend = 1; start = 0; next }
                               { start = 0 }
/^GPC-TEST-END$/ && doend      { doit = 0 }
!doit                          { next }
/^TEST/                        { count(); lines = $0; next }
                               { lines = lines "\n" $0 }
END                            { sub ("\n$", "", lines); count(); summary() }
'
