#!/usr/bin/env perl

use strict;
use warnings;

use Term::ANSIColor;
use Cwd qw/getcwd/;
use File::Copy;
use File::Basename;

my $problem = basename getcwd;
my $author = shift;
my $source_name = $problem . "_" . $author;

die colored("Usage ./check_solution.pl author", 'red') if !defined($author);

system("g++ -O3 $source_name.cpp -o $source_name &>/dev/null") == 0 or die "Can't compile $source_name.cpp";

for my $test (<./tests/*>) {
    next if ($test =~ /\.a$/);
    copy "$test", "$problem.in";
    if (system("./$source_name &>/dev/null") == 0) {
        my $output = `java -Xmx256M -Xss64M -cp ../../../lib/testlib4j.jar:. ru.ifmo.testlib.CheckerFramework Check $test $problem.out $test.a`;
		if ($? == 0) {
            print colored("OK on test $test\n", 'green');
        } else {
            print colored("FAIL on test $test\n$output", 'red');
        }
    } else {
        print colored("RE on test $test\n", 'red');
    }
}
