#!/usr/bin/env perl
#
# This script runs a set of tests under various combinations of compiler
# options, to see if there are issues with common/important combinations.
#
# by default: run tests on release/examples directory with
#             combinations of compiler options listed in the file
#             ./OPT_TESTING_FLAGS
#
# -cron: check out a new copy of chapel, make it, run tests as above,
#        reading options from test/OPT_TESTING_FLAGS in the checkout,
#        and mail results to test failures mailing list ; on Saturdays
#        run tests on every directory and on Sundays, do nothing
#
# -all: run tests on almost all test/ subdirectories, not just
#       release/examples
#

use Cwd 'abs_path';
use File::Basename;

# Mailing lists.
$failuremail = "chapel-test-results-regressions\@lists.sourceforge.net chapel_cronmail\@cray.com";
$allmail = "chapel-test-results-all\@lists.sourceforge.net chapel_cronmail_all\@cray.com";

while (@ARGV) {
  $flag = shift @ARGV;
  if ($flag eq "--cron" || $flag eq "-cron") {
    $cron = 1;
  } elsif ($flag eq "--all" || $flag eq "-all") {
    $all = 1;
  } elsif ($flag eq "--hello" || $flag eq "-hello") {
      $hello = 1;
  } else {
    die "usage: start_opt_test [--cron] [--all]\n";
  }
}

$cwd = abs_path(dirname(__FILE__));
$chplhomedir = abs_path("$cwd/../..");
$chpltestdir = "$chplhomedir/test";
$ENV{'CHPL_HOME'} = $chplhomedir;
if ($cron) {
    `cd $chplhomedir && make all`;
}

@OPTS = `cat $chpltestdir/OPT_TESTING_FLAGS`;

if ($all) {
  $dir = "arrays associative classes demo dispatch distributions domains expressions functions io modules multilocale no_infer npb parallel param parsing portability puzzles reductions release/examples sparse spec statements studies trivial types users";
} elsif ($hello) {
    $dir = "release/examples/hello.chpl";
} else {
  $dir = "release/examples";
}

`rm -f .opt_testing.tmp`;

foreach $opt (@OPTS) {
    chomp $opt;
    system("echo \"\" | tee -a $chpltestdir/.opt_testing.tmp");
    system("echo \"OPTIMIZATION FLAGS: $opt\" | tee -a $chpltestdir/.opt_testing.tmp");
    system("echo \"\" | tee -a $chpltestdir/.opt_testing.tmp");
    system("cd $chpltestdir && ../util/start_test $dir --compopts \"$opt\" | tee -a .opt_testing.tmp");
}

@lines = `cat $chpltestdir/.opt_testing.tmp`;

foreach $line (@lines) {
    if ($line =~ /\[Error/) {
        $nerrort2++;
    }
}

if ($cron && (!exists($ENV{"CHPL_TEST_NOMAIL"}) or grep {$ENV{"CHPL_TEST_NOMAIL"} =~ /^$_$/i} ('','\s*','0','f(alse)?','no?'))) {
    $nerror = $nerrort2/2;
    if ($nerror == 0) {
        open(FILE, "| mail -s \"Cron $nerror/$nerror (chap02:opts tests)\" $allmail $ENV{'USER'}\@cray.com");
    } else { 
        open(FILE, "| mail -s \"Cron $nerror/$nerror (chap02:opts tests)\" $failuremail $allmail");
    }
} else {
    open(FILE, ">&STDOUT");
    if (exists($ENV{"CHPL_TEST_NOMAIL"})) {
        print FILE "CHPL_TEST_NOMAIL: No mail\n";
    }
}

$nerror = 0;
$first = 1;
foreach $line (@lines) {
    chomp $line;
    if ($line =~ /OPTIMIZATION\ FLAGS\:\ (.*)/) {
        print FILE "$1\n";
    } elsif ($line =~ /\[Test Summary/) {
        print FILE "\n";
        $show = 1;
    } elsif ($line =~ /\[END/) {
        print FILE "\n";
        $show = 0;
    } elsif ($show) {
        if ($line =~ /\[Error/) {
            $nerror++;
        }
        print FILE "$line\n";
    }
}

print FILE "Total Errors: $nerror\n";

if ($cron) {
    close(FILE);
}
