#!/usr/bin/env perl
# MODULE:  $RCSfile: runTests.pl,v $
# AUTHOR: Come Raczy, Lukasz Szajkowski
#
# Copyright (c) 2008 Illumina
# This source file is covered by the "Illumina Public Source License"
# agreement and bound by the terms therein.
#
# Run the specified test scrip (Nightly, Experimental or Continuous).
# The current working directory changes to <base test directory> before
# running the tests.
# The source repositories are in <base test directory>
#
# export SHELL="/bin/bash"
# export MAILTO="user_name@illumina.com"
# export PATH="/home/user_name/bin:/bin:/usr/bin"
# export LIBS="/home/user_name/lib"
# export CPATH="/home/user_name/include"
# export LIBRARY_PATH="/home/user_name/lib"
# export LD_LIBRARY_PATH="/home/user_name/lib"
# export BOOST_ROOT="/home/user_name"
# export CVSROOT=":ext:user_name@genie01:/home/cvs"
# export CVS_RSH="ssh"
# export TEST_BASE="/home/user_name/GAPipelineTest"
# export GA_TEST_DATA="/lustre/isilon/Mondas_software/CASAVA_Releases/Test_Datasets/"
#

use Encode;

use warnings FATAL => 'all';
use strict;
use POSIX qw(strftime);

use File::Spec;
use IO::File;
use Sys::Hostname;
use Getopt::Long;
use XML::Simple;
my $help      = 0;
my $isVersion = 0;
my $Version   = '@CASAVA_VERSION_FULL@';

my $usage =
    "Usage: runTests.pl [options]\n"
  . "\t-m,--mode=STRING    - test scrip (Nightly, Experimental or Continuous)\n"
  . "\t-d,--dir=DIR        - current working directory changes to DIR. (The source repositories are in)\n"
  . "OPTIONAL (BEHAVIOUR)\n"
  . "\t-r,--tag            - cvs tag \n"
  . "\t--version           - prints version information\n"
  . "\t-h, --help          - prints this information\n";

my $mode = "";
my $dir  = "";
my $tag  = "";

my $result = GetOptions(
    "mode|m=s" => \$mode,
    "dir|d=s"  => \$dir,
    "tag|r=s"  => \$tag,
    "version"  => \$isVersion,
    "help|h"   => \$help
);

if ( $mode eq "" || $dir eq "" ) {
    die $usage;
}    # if

if ($help) {
    print $usage;
    exit(0);
}    # if

if ($isVersion) {
    print "$Version\n";
    exit(0);
}    # if

my $tag_str = "";
if ( $tag ne "" ) {
    $tag_str = " -r $tag ";
}
my $GA_TEST_BASE = $ENV{TEST_BASE};
chdir $GA_TEST_BASE
        or die "ERROR: couldn't change directory to $GA_TEST_BASE $!\n";
print "Changing directory to $GA_TEST_BASE\n";

my $curCir       = Cwd->getcwd();
$ENV{GA_DIR} = "$GA_TEST_BASE/Pipeline$mode/";
$ENV{CTEST_CVS_OPTIONS} = "$tag_str";       

if ( !-d $dir ) {
    die "Directory [$dir] does not exist\n$usage";
}

if ( $mode eq "Nightly" ) {
    if ( -d "$GA_TEST_BASE/PipelineNightly" ) {
        if (
            system("mv PipelineNightly PipelineNightly_`date +%y%m%d`.$$") !=
            0 )
        {
            die "ERROR: $!\n";
        }
    }

    if ( system(" cvs co -P $tag_str -d PipelineNightly CASAVA") == 0 ) {
        chdir "$GA_TEST_BASE/PipelineNightly"
        or die "ERROR: couldn't change directory to PipelineNightly $!\n";
        if ( system("ctest -S ./test/CTestNightly.cmake -V") != 0 )
        {    
            die "ERROR: $!\n";
        }
    }
    else {
        die "ERROR: $!\n";
    }
}
elsif ( $mode eq "Experimental" ) {
    if ( -d "PipelineExperimental" ) {
        if (system("mv PipelineExperimental PipelineExperimental_`date +%y%m%d`.$$") != 0)
        {    
            die "ERROR: $!\n";
        }
    }
    if ( system("cvs checkout -P -d PipelineExperimental -kk CASAVA") == 0 )
    {
        chdir "$GA_TEST_BASE/PipelineExperimental"
            or die "ERROR: couldn't change directory to PipelineExperimental $!\n";
        mkdir "build";
        chdir "$GA_TEST_BASE/PipelineExperimental/build";
        system("../configure --prefix=$GA_TEST_BASE/PipelineExperimental/build");
        chdir "$GA_TEST_BASE/PipelineExperimental";
        if (system("ctest -S $GA_TEST_BASE/PipelineExperimental/test/CTestExperimental.cmake -V") != 0)
        {    
            die "ERROR: $!\n";
        }
    }
    else {
        die "ERROR: $!\n";
    }
}
elsif ( $mode eq "Continuous" ) {
    if ( !-d "PipelineContinuous" ) {
        if (system("cvs co -P $tag_str -d PipelineContinuous CASAVA") != 0){
            die "ERROR: $!\n";
        } 
        chdir "$GA_TEST_BASE/PipelineContinuous"
        or die "ERROR: couldn't change directory to PipelineContinuous $!\n";
    }
    else {
        chdir "$GA_TEST_BASE/PipelineContinuous" 
        or die "ERROR: couldn't change directory to PipelineContinuous $!\n";
        if (system("cvs $tag_str update -PdA") != 0){
            die "ERROR: $!\n";            
        }
    }
    print "Changing directory to $GA_TEST_BASE/PipelineContinuous\n";
    chdir "$GA_TEST_BASE/PipelineContinuous"
        or die "ERROR: couldn't change directory to $GA_TEST_BASE/PipelineContinuous $!\n";
    if (system("ctest -S $GA_TEST_BASE/PipelineContinuous/test/CTestContinuous.cmake -V") != 0){
           die "ERROR: $!\n";                    
    }
}
else {
    die "ERROR: unsupported mode [$mode]\n$usage";
}

__END__

