#!/usr/bin/perl -w

use strict;
my $script_dir; BEGIN { use Cwd qw/ abs_path /; use File::Basename; $script_dir = dirname(abs_path($0)); push @INC, $script_dir; }
use Getopt::Long;

if (!defined $ENV{HOST}){
my $host=`hostname`;
chomp($host);
$ENV{HOST}=$host;
}

############################################################
my @tests = qw (
  basic-dict
  basic-ngt
  quantize-lm
  prune-lm
  compile-lm
  build-lm
  build-lm-sublm
  build-lm-sublm2
  interpolate-lm
  interpolate-vs-compile
  interpolate-lm-estimate
  tlm-arpa
  tlm-smoothing
  tlm-checkprob
  tlm-mixture
  plsa
  plsa2
);

my @qsubtests = qw (
  build-lm-qsub
);

if (@qsubtests){
my $cmd=&getQsubCmd();

if (!defined($cmd)){
	print STDERR "Regression tests (@qsubtests) can not run on $ENV{HOST}\nbecause SGE is not installed\n\n"; 
}else{
	push @tests, @qsubtests;
}
}

###########################################################

use IrstLMRegressionTesting;
use File::Temp qw ( tempfile );
use POSIX qw ( strftime );

my $test_dir;
my $BIN_TEST = $script_dir;
my $data_dir;

GetOptions("data-dir=s" => \$data_dir,
          ) or exit 1;

$data_dir = IrstLMRegressionTesting::find_data_directory($BIN_TEST, $data_dir);

my $test_run = "$BIN_TEST/run-single-test.pl --data-dir=$data_dir";
$test_dir = $script_dir . "/tests";
$test_run .= " --test-dir=$test_dir" if $test_dir;

print "Data directory: $data_dir\n";

print "Running tests: @tests\n\n";

print STDOUT "TEST NAME\tSTATUS\tPATH TO RESULTS\n";
my $lb = "---------------------------------------------------------------------------------------------------------\n";
print $lb;

my $fail = 0;
my @failed;
foreach my $test (@tests) {
  my $cmd = "$test_run --test=$test";
  my ($res, $output, $results_path) = do_test($cmd);

  print STDOUT "$test\t$res\t$results_path\n";

  if ($res eq 'FAIL') {
    print "$lb$output";
    print "PATH to RESULTS: $results_path\n";
    print "$lb";
    $fail++;
    push @failed, $test;
  } else {
    if ($output =~ /TOTAL_WALLTIME\s+result\s*=\s*([^\n]+)/o) {
      print "\t\tTiming statistics: $1\n";
    }
  }
}

my $total = scalar @tests;
my $fail_percentage = int(100 * $fail / $total);
my $pass_percentage = int(100 * ($total-$fail) / $total);
print "\n$pass_percentage% (".($total-$fail)."/$total) of the tests passed.\n";
print "$fail_percentage% ($fail/$total) of the tests failed.\n";
if ($fail_percentage>0) { print "\nPLEASE INVESTIGATE THESE FAILED TESTS: @failed\n"; }

sub do_test {
  my ($test) = @_;
  my $o = `$test 2>&1`;
  my $res = 'PASS';
  $res = 'FAIL' if ($? > 0);
  my $od = '';
  if ($o =~ /RESULTS AVAILABLE IN: (.*)$/m) {
    $od = $1;
    $o =~ s/^RESULTS AVAIL.*$//mo;
  }
  return ($res, $o, $od);
}

sub getQsubCmd {
        my $a =`which qsub | head -1 | awk '{print \$1}'`;
        chomp($a);
        if ($a && -e $a){ return $a; }
        else{ return undef; }
}

