#!/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 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.

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" >&2
  echo "$0 and report it to the GPC developers.)" >&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
}

testfile ()
{
  x="$1"
  if echo "$x" | grep '\.pas$' > /dev/null; then
    :
  else
    x="$x.pas"
  fi
  if [ -r "$x" ]; then
    if grep -i "program.*;" "$x" > /dev/null; 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/ *\*).*//;s/ *}.*//;p;}' "$x"`"
      PC_WITH_FLAGS="$PC $PFLAGS `sed -ne '/FLAG/{s/.*FLAG *//;s/ *\*).*//;s/ *}.*//;p;}' "$x"` \
        --unit-path=$SRCDIR --unit-path=$SRCDIR/../rts \
        --unit-path=$SRCDIR/../units --unit-path=$DEFAULT_UNIT_DIR \
        -I $DEFAULT_UNIT_DIR -I $SRCDIR -I $SRCDIR/../units --executable-path=."
      rm -f a.out 2>/dev/null
      echon "TEST	$xb:	"
      if [ "$xc" != "" ]; then
        { sh "$SRCDIR/$xc" "$PC_WITH_FLAGS -o a.out" "$x"; } 2>&1
      elif grep WRONG >/dev/null "$x"; then
        if $PC_WITH_FLAGS -o a.out "$x" 2> stderr.out || [ -f "a.out" ]; then
          echon "failed: "
          { ./a.out; } 2>&1
        elif grep -i -e "internal compiler error" -e "fatal signal" stderr.out > /dev/null; then
          echon "failed: "
          cat stderr.out
        else
          echo "OK"
        fi
      else
        if { $PC_WITH_FLAGS -o a.out "$x"; } 2>&1; then
          if [ -f "$xr" ]; then
            if [ -f "$xo" ]; then
              if [ -f "$xi" ]; then
                { sh "$xr" "$x" < "$xi"; } > testmake.tmp 2>&1
              else
                { sh "$xr" "$x"; } > testmake.tmp 2>&1
              fi
              if diff testmake.tmp "$xo"; then
                echo "OK"
              else
                echo "failed"
              fi
            else
              if [ -f "$xi" ]; then
                { sh "$xr" "$x" < "$xi"; } 2>&1
              else
                { sh "$xr" "$x"; } 2>&1
              fi
            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
        else
          echo "failed"
        fi
      fi
    fi;
    true
  else
    false
  fi
}

DEFAULT_UNIT_DIR="`"$PC" "$PFLAGS" --print-file-name=units`"
echo "GPC-TEST-BEGIN"
echo "=========================="
for f in `cd "$SRCDIR"; echo $*`; do
  found=n
  for x in "$SRCDIR"/$f; do
    testfile "$x" && found=y
  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 ...
