#!/usr/bin/env perl

use strict;
use warnings;
use English;

print <<WARNING;

My job is to run tryfile over all the tests in roast/.
And since STD.pm is slow at the moment, this is going to take 
too much time.

You have been warned ;-)
You can press CTRL-C when you feel bored.

WARNING

my $dir_to_test = shift @ARGV || '../roast';
my $pkgdir = (-e "$dir_to_test/packages") ? "$dir_to_test/packages" :
    "$dir_to_test/roast/packages";
$ENV{PERL6LIB} = "lib:$pkgdir:.";
my $fail = 0;
my $success = 0;
my @files = sort `find $dir_to_test -name '*.t'`;
chomp(@files);
$OUTPUT_AUTOFLUSH = 1;

my $total = 0+@files;
my @failed;
for my $file (@files) {
    (my $short = $file) =~ s/(\.\.\/)+//;
    print $short, "\n";
    system "./tryfile $file >tryfile.out";
    if ($CHILD_ERROR) {
        push(@failed,$short);
        $fail++;
        # catch those pesky CTRL-Cs
        if($CHILD_ERROR &= 127) {
            print "CTRL-C detected... bye bye\n";
            last;
        }
    }
    else {
	    $success++;
    }
}

printf "Passed $success/$total, %6.2f%%\n", $success/$total * 100;
if (@failed) {
    print "Failed tests:\n";
    for my $file (@failed) {
    	print "$file\n";
    }
}
