#!/bin/sh

bitlen=$1; shift
testlist=$@

if (tail -n 1 $0 >/dev/null 2>&1); then
  tail='tail -n'
else
  tail='tail -'
fi
if (head -n 1 $0 >/dev/null 2>&1); then
  head='head -n'
else
  head='head -'
fi

wrln () { echo "$1"; echo "$1" >> $file_bench; }
wr () { echo $n "$1$c"; echo $n "$1$c" >> $file_bench; }

confdir=../config
testdir=../src/test
execdir=.
if test -f /bin/rm ; then RM=/bin/rm; else RM=rm ; fi
if sh -c 'test -x /bin/sh' 2>&-; then x=-x; else x=-r; fi
(echo "hi there\c" ; echo " ") > echotmp
if grep c echotmp >/dev/null 2>&1 ; then n=-n; c=; else n=; c='\c'; fi
$RM -f echotmp

. $confdir/version
file_bench=Bench-$VersionMajor.$VersionMinor.$patch
if (touch $file_bench 2> /dev/null); then
  SUF=
else
  if test ! $x $execdir/gp.exe; then
    echo "build gp.exe first" >&2; exit 1
  fi
  if (echo | gp --test -q 2> /dev/null); then
    gp=gp
  else
    gp="command /c gp"
  fi
  if (expr 2 \* 2 2> /dev/null); then exprbug=0; else exprbug=1; fi
  SUF="exe"; file_bench=Bench.$patch
fi
$RM -f $file_bench

dotestSUF=${dotestSUF:-"sta dyn"}
for arg in $dotestSUF; do
  case "$arg" in
    sta|dyn) if test $x $execdir/gp-$arg; then SUF="$SUF $arg"; fi ;;
  esac
done
file_test=gp.out
for suf in $SUF; do eval time$suf=0 files$suf=; done
for testdata in $testlist; do
  O=$IFS;IFS=_;set $testdata;IFS=$O;testname=$1;testmul=$2
  file_in=$testdir/in/$testname
  file_out=$testdir/$bitlen/$testname
  if test ! -r $file_out; then file_out=$testdir/32/$testname; fi
  if test ! -r $file_out; then touch $file_out; fi
  if test ! -r $file_in; then
    wrln "Error! Missing file, can't test $testname"
    exit 1
  fi

  testmul=${testmul:-1000}
  wr "* Testing $testname $c"
  for suf in $SUF; do
    wr "	for gp-$suf..$c"
    case "$suf" in
      exe) file_diff=$testname.dif;;
      *)   file_diff=$testname-$suf.dif
           gp=$execdir/gp-$suf;;
    esac
    $gp -q --test < $file_in > $file_test 2>&1
    diff -c $file_out $file_test > $file_diff
    pat=`grep "^[-+!] " $file_diff | grep -v "Total time"`
    time=`${tail}2 $file_test | sed -n 's,.*Total time spent: \(.*\),\1,p'`
    if test -n "$time"; then
      eval t='$'time$suf
      if test -n "$exprbug"; then
        t=`expr $time \'\*\' $testmul / 1000 + $t`
      else
        t=`expr $time '*' $testmul / 1000 + $t`
      fi
      eval time$suf=$t
    fi
    if test -z "$pat"; then
      wr "TIME=$time";
    else
      eval BUG$suf=BUG
      eval files$suf=\"'$'files$suf $file_diff\"
      wr "BUG [${time:-0}]"
    fi
  done
  wrln
done
$RM -f $file_test
BUG=
for suf in $SUF; do
  B=`eval echo '$'BUG$suf`; BUG="$BUG$B"
  t=`eval echo '$'time$suf`
  if test -n "$B"; then B=' [BUG]'; fi
  wrln "+++$B Total bench for gp-$suf is $t"
done
if test -z "$BUG"; then exit 0; fi

pwd=`pwd`
wrln
wrln "PROBLEMS WERE NOTED. The following files list them in diff format: "
wrln "Directory: $pwd"
bugp=; buge=;
for suf in $SUF; do
  B=`eval echo '$'BUG$suf`; BUG="$BUG$B"
  if test -n "$B"; then
    flist=`eval echo '$'files$suf`
    for f in $flist; do
      wrln "	$f"
      case "$f" in
        *program*)  bugp="$suf$bugp";;
      esac
    done
  fi
done

case "x$bugp" in
  x)  file_diff=;;
  xsta|xdyn)
      end=" (gp-$bugp only)"
      file_diff=program-$bugp.dif;;
  xexe)
      end=" (gp-$bugp)"
      file_diff=program.dif;;
  x*) end=""
      file_diff=program-sta.dif;;
esac
if test -n "$file_diff"; then
len=`wc -l $file_diff | sed -e 's/ *\([0-9]*\).*/\1/'`
pat=`${head}4 $file_diff|${tail}1|grep "112,122"`
sys0190=`grep "^+ SYS0190\>" $file_diff`

if test $len -eq 51  -a  -n "$sys0190"; then
  len=49			# more verbose error message on OS/2
fi
if test $len -eq 49  -a  -n "$pat"; then
cat << EOT
NOTE: the problem noted in 'program' is that install() does not work in your
configuration$end, so you will not be able to use this feature. Otherwise you
can safely ignore the above warning.
EOT
fi
fi
exit 1


syntax highlighted by Code2HTML, v. 0.9.1