#!/bin/sh

# Script to summarize the results of a GPC Test Suite run
#
# Copyright (C) 1998-2002 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()'  # 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" '
BEGIN {
    num = 0;
    num_ok = 0;
    num_wrong = 0;
    num_skipped = 0;
    start = 0;
    cr = 0;
    doend = 0;
    doit = 0;
    name = "";
    lines = "";
    first = 1;
}
first && /\r$/ {
    cr = 1;
}
cr {
    sub ("\r+$", "");
}
/^GPC-TEST-BEGIN$/ {
    start = 1;
    next;
}
/^==========================$/ {
    if (start)
      {
        start = 0;
        doit = 1;
      }
    else
      doend = 1;
    next;
}
/^GPC-TEST-END$/ && doend {
    doit = 0;
    next;
}
doit == 0 {
    next;
}
function eval () {
    if (lines == "")
      return;
    num++;
    ok = 0;
    if (lines ~ /^TEST\t[a-z0-9_.~-]*:\tOK$/)
      {
        ok = 1;
        num_ok++;
      }
    else if (lines ~ /^TEST\t[a-z0-9_.~-]*:\tSKIPPED[: \t]/)
      num_skipped++;
    else
      num_wrong++;
    if (!ok)
      {
        printf "%s\n", lines;
        '"$FFLUSH"'
      }
    lines = "";
}
/^TEST/ {
    eval();
    first = 0;
    lines = $0;
    next;
}
{
    lines = lines "\n" $0
}
END {
    eval();
    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;
      }
}
'
