#!/bin/sh

# Script to run the GPC test suite
#
# Copyright (C) 1996-2000 Free Software Foundation, Inc.
#
# Authors: Frank Heckenbach <frank@pascal.gnu.de>
#          Matthias Klose <doko@cs.tu-berlin.de>
#          Peter Gerwinski <peter@gerwinski.de>
#
# This file is part of the GNU Pascal Library. The GNU Pascal
# Library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# The GNU Pascal Library 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; see the file COPYING.LIB.
# If not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

if [ -z "$PC" ]; then
  PC=gpc
fi

if [ -z "$PFLAGS" ]; then
  PFLAGS="--automake -g -O3 -Wall -Werror"
fi

if [ -z "$SRCDIR" ]; then
  SRCDIR=.
fi

# Find a good echo. !:-/
if [ "`echo -n foo; echo bar`" = "foobar" ]; then
  echomode=1
elif [ "`echo 'foo\c'; echo bar`" = "foobar" ]; then
  echomode=2
elif [ "`echo -e 'foo\c'; echo bar`" = "foobar" ]; then
  echomode=3
else
  echo "Cannot find a way to echo without newline. :-(" >&2
  echo "(If you know one for this system, please add it in $0.)" >&2
  exit 1
fi

echon ()
{
  if [ $echomode = 1 ]; then
    echo -n "$*"
  elif [ $echomode = 2 ]; then
    echo "$*\c"
  elif [ $echomode = 3 ]; then
    echo -e "$*\c"
  fi
}

echo "GPC-TEST-BEGIN"
echo "=========================="
set -f
for f in $*; do
  set +f
  found=n
  for x in "$SRCDIR"/$f; do
    if ! echo "$x" | grep '\.pas$' > /dev/null; then
      x="$x.pas"
    fi
    if [ -r "$x" ]; then
      found=y
      if grep -i "Program.*;" > /dev/null $x ; then
        xb=`basename $x`
        x1="$SRCDIR"/`echo $xb|sed -e 's/\.[^.]*$//'`
        xr="$x1.run"
        xi="$x1.in"
        xo="$x1.out"
        xc=`sed -ne '/COMPILE-CMD:/{s/.*COMPILE-CMD: *//;s/ *\(\*)\|}\).*//;p;}' $x`
        PC_WITH_FLAGS="$PC $PFLAGS `sed -ne '/FLAG/{s/.*FLAG *//;s/ *\(\*)\|}\).*//;p;}' $x` \
          --unit-path=$SRCDIR --unit-path=$SRCDIR/../rts --unit-path=$SRCDIR/../units \
          -I $SRCDIR -I $SRCDIR/../units --executable-path=."
        rm -f a.out 2>/dev/null
        echon "TEST	$xb:	"
        if [ "$xc" != "" ]; then
          $SRCDIR/$xc "$PC_WITH_FLAGS -o a.out" "$x" 2>&1
        elif grep WRONG >/dev/null $x ; then
          $PC_WITH_FLAGS -o a.out $x 2> /dev/null
          if [ -f "a.out" ] ; then
            echon "failed: "
            ./a.out 2>&1
          else
            echo "OK"
          fi
        else
          $PC_WITH_FLAGS -o a.out $x 2>&1
          if [ -f $xr ]; then
            if [ -f $xi ] ; then
              $xr $x < $xi 2>&1
            else
              $xr $x 2>&1
            fi
          else
            if [ -f "a.out" ] ; then
              if [ -f $xo ]; then
                if [ -f $xi ] ; then
                  ./a.out $x < $xi > testmake.tmp 2>&1
                else
                  ./a.out $x > testmake.tmp 2>&1
                fi
                if diff testmake.tmp $xo; then
                  echo "OK"
                else
                  echo "failed"
                fi
              else
                if [ -f $xi ] ; then
                  ./a.out $x < $xi 2>&1
                else
                  ./a.out $x 2>&1
                fi
              fi
            else
              echo "failed"
            fi
          fi
        fi
      fi
    fi
  done
  if [ $found = n ]; then
    echo "$f: No such file" >&2
  fi
done
echo "=========================="
echo "GPC-TEST-END" # be sure that GPC-TEST-END starts in a new line ...
