#!/usr/bin/env perl
################################################################################
##
## The test script for running all Metronome tests.  Steps are:
##  -- Create the test directory based on full timestamp
##  -- Run a cvs checkout of the test scripts
##  -- Setup the environment based on a fresh Metronome checkout or workspace
##  -- Run the tests listed in the test_info file
##
## $Id: tester.pl,v 1.4 2008/11/17 21:06:25 nmi-devel Exp $
################################################################################

use warnings;
use strict;

use Getopt::Long;
use Cwd;

use vars qw/$OPT_WORKSPACE $OPT_NOTIFY $OPT_BASEDIR $OPT_CVSROOT $OPT_BRANCH/;

GetOptions (
    'workspace=s'      => \$OPT_WORKSPACE,
    'notify=s'         => \$OPT_NOTIFY,
    'basedir=s'        => \$OPT_BASEDIR,
    'cvsroot=s'		   => \$OPT_CVSROOT,
    'branch=s'		   => \$OPT_BRANCH,
    'help'             => \&usage,
);

if( ! defined( $OPT_NOTIFY ) ) {
	$OPT_NOTIFY = "uw-nmi-dev\@cs.wisc.edu\n";
}	
if( ! defined( $OPT_CVSROOT ) ) {
	$OPT_CVSROOT = "/nfs/cvs/nmi";
}
if( ! defined( $OPT_BASEDIR ) ) {
	$OPT_BASEDIR = "~/.tmp-trunk";
}	
if( defined( $OPT_BRANCH ) ) {
	$OPT_BRANCH = "-r$OPT_BRANCH";
} else {
	$OPT_BRANCH = "";
}		

## ---------------------------------------------
## Create the base test dir
## ---------------------------------------------

my $TIMESTAMP = `date "+%F_%T"`;
my $TESTDIR = "$OPT_BASEDIR/$TIMESTAMP";
chomp($TESTDIR);

mkdir("$TESTDIR") || die "Can't create testdir: $!\n";
chdir("$TESTDIR") || die "Can't cd to testdir: $!\n";
my $CVS_CMD = "cvs -d $OPT_CVSROOT co $OPT_BRANCH nwo/test";
system("$CVS_CMD");


## ---------------------------------------------
## Set up the env based on a metronome install
## ---------------------------------------------

## See if this is a workspace test
if( defined($OPT_WORKSPACE) ) {
    $ENV{'NMI_BIN'} = "$OPT_WORKSPACE/bin";
    $ENV{'NMI_LIB'} = "$OPT_WORKSPACE/lib";
    system("source $ENV{'NMI_BIN'}/config.sh");
} else {
    ## Check out a fresh copy of CVS and set up the env
    my $INSTALL_DIR = "$TESTDIR"; ## should be the base timestamp dir
    chdir("$INSTALL_DIR") || die "Can't cd to install_dir: $!\n";

    system("cvs -d $OPT_CVSROOT co $OPT_BRANCH NWO_ALL");
    chdir("nwo") || die "Can't cd to nwo: $!\n";
    system("perl Makefile.PL prefix=$INSTALL_DIR");
    system("make install");

    $ENV{'NMI_BIN'} = "$INSTALL_DIR/bin";
    $ENV{'NMI_LIB'} = "$INSTALL_DIR/lib";
    system(". $ENV{'NMI_BIN'}/config.sh");

    ## Finish the framework setup.
    system("mkdir $INSTALL_DIR/etc");
    ## TODO - Use sample nmi.conf
    system("cp /nmi/etc/nmi.conf $INSTALL_DIR/etc/");
}

## TODO - Make sure we are using the correct nmi_submit/nmi_version and lib dir
my $METRONOME_VERSION = `$ENV{'NMI_BIN'}/nmi_version`;
chomp($METRONOME_VERSION);
my $NMI_SUBMIT = "$ENV{'NMI_BIN'}/nmi_submit";
print "DEBUG: Metronome version is $METRONOME_VERSION\n";
print "DEBUG: nmi_submit is $NMI_SUBMIT\n";


## ---------------------------------------------
## Read in the tests and their params
## ---------------------------------------------

my %TESTS;
my %TEST_LIST;

chdir("$TESTDIR/nwo") || die "Can't cd to testdir: $!\n";
my $TEST_DEFINE = "$TESTDIR/nwo/test/test_info";

open(TE, "<$TEST_DEFINE") || die "can't open the tests file: $!\n";
while (<TE>) {

  my ($key, $val) = split ("=", $_);
  if ( defined $key && defined $val ){
    chomp($val);
    $key =~ s/ //g;
    $val =~ s/ //g;

    if ($key =~ /XX_/ ) {
      $key = substr($key, 3);
      $TEST_LIST{$key} = "$val";
    } else {
      $TESTS{$key} = "$val";
    }
  }
}
close TE;


## ---------------------------------------------
## Run the tests
## ---------------------------------------------

foreach my $MYTEST (keys %TEST_LIST) {

  mkdir("$TESTDIR/nwo/$MYTEST");
  chdir("$TESTDIR/nwo/$MYTEST");
  print "DEBUG: Running $MYTEST test...\n";

  ## check the test type and submit it in the appropriate way
  if ($TEST_LIST{$MYTEST} eq "gnu") {

    my $version_string = "$MYTEST" . "_version";
    my $url_string = "$MYTEST" . "_url";
    my $platform_string = "$MYTEST" . "_platforms";

    my $VERSION = $TESTS{$version_string};
    my $URL = $TESTS{$url_string};
    my $PLATFORMS = $TESTS{"$platform_string"};

    my $make_gnu_scripts = "$TESTDIR/nwo/test/make_nmi_build.rb";

    my $VERS_STRING = "$MYTEST-" . "$VERSION";

    my $CMD1 = "$make_gnu_scripts $VERS_STRING $URL $PLATFORMS $METRONOME_VERSION $OPT_NOTIFY";
    my $CMD2 = "$ENV{'NMI_BIN'}/nmi_submit job.nmi";

    my $rc1 = `$CMD1 2>&1`;
    my $rc2 = `$CMD2 2>&1`;

    print "DEBUG: $CMD1 returned status $rc1\n";
    print "DEBUG: $CMD2 returned status $rc2\n";

  } elsif ($TEST_LIST{$MYTEST} eq "condor") {

    ## Use the previous day's build tag for today's Metronome Condor build.  Not
    ## sure if this is the best idea, but it prevents the need for us to manually
    ## update the build tags as the old ones are scrubbed from the repository.  At
    ## least we'll see if we get the same results as the previous day's Condor run.
    my $TAG_STRING = `cat /space/cvs/nwo-build-tags.old`;
    my @TAG_ARRAY = split(" ", $TAG_STRING);

    my $platform_string = "$MYTEST" . "_platforms";
    my $PLATFORMS = $TESTS{$platform_string};

    my $CMD3 = "~cndrauto/bin/condor_nmi_submit --use_externals_cache --build --desc=Metronome_Test --tag=$TAG_ARRAY[1] --module=$TAG_ARRAY[0] --platforms=$PLATFORMS --notify=$OPT_NOTIFY";
    my $rc3 = `$CMD3 2>&1`;
    print "DEBUG: $CMD3 returned status $rc3\n";
  }
}

