#!/usr/bin/perl
# RCS $Id: test-trans,v 1.7 2002/03/07 17:07:59 dcs0mpw Exp $
# Compile and run the fermat2 transformation test scripts
#

BEGIN {
  $FermaT = $ENV{'FermaT'} || "/usr/local/fermat2";
  $FermaT =~ s/"//g;
  $ds = "/"; $ds = "\\" if ($^O eq "MSWin32");
  unshift(@INC, "$FermaT${ds}config");
}

use fermat;

($myname = $0) =~ s|(.*/)*||;	# strip path component from name
$Usage = "Usage: $myname [-scm|-gambit]\n";

$| = 1; # Unbuffered output

# Directory where the scripts are stored:
$dir = "$FermaT${ds}test${ds}trans";
chdir $dir or die "Can't chdir to $dir: $!\n";
# Transformation failure indication:
$failpatt = 'no such|failed|error|segmentation|supposed|undefined|terminated|died|\!\!\!';
$opt = "@ARGV";

opendir(DIR, $dir) or die "Can't open test directory: $dir: $!\n";
@files = sort(grep(/\.wsl$/, readdir(DIR)));
closedir(DIR);

$len = max(map(length, @files)) + 3;

@fail = ();
for $file (@files) {
  print "$file" . "." x ($len - length($file));
  $result = run_wsl($opt, $dir, $file);
  if ($result !~ /$failpatt/io) {
    print "ok\n";
  } else {
    print "FAILED! ---\n";
    push(@fail, $file);
    print join("\n", grep { /$failpatt/io } split(/\n/, $result)), "\n";
  }
}
if (@fail) {
  print "The following files failed: ", join(" ", @fail), "\n";
} else {
  print "All files passed!\n";
}

exit (0);

sub max(@) {
  my(@args) = @_;
  my($res);
  die "max requires at least one argument!" if ($#args < 0);
  $res = $args[0];
  for (@args) {
    $res = $_ if ($_ > $res);
  }
  return($res);
}
