#!/usr/bin/env perl

# Script to compile some sample target classes; these classes are located
# in $MYHOME/test

# --------------------------------------------------------
# Few thing that may need to be re-cofigured
# --------------------------------------------------------

my $MYlaptop = 9 ;
my $UPV = 0 ;
my $deploy = $MYlaptop ;
#my $deploy = $UPV ;

# Using Java7 to compile and run Junits 
my $MYHOME = "/home/t2" ; 
my $JAVAC = javac;
my $JAVA = java ;
my $CPseparator = ":" ;

if ($deploy == $MYlaptop) { 
    $MYHOME = "." ; 
    $JAVAC  = "/cygdrive/c/apps/Java/jdk1.7.0/bin/javac" ;
    $JAVA  = "/cygdrive/c/apps/Java/jdk1.7.0/bin/java" ;
    $CPseparator = ";" ;
}

# --------------------------------------------------------
# Should not change part
# --------------------------------------------------------

# replaceIt(s,a,b) replaces occurences of string a in string s, with b
sub replaceIt {
   my $str     = $_[0] ;
   my $find    = $_[1];
   my $replace = $_[2] ;
   # escape regex metachars if present:
   $find    = quotemeta $find; 
   $replace = quotemeta $replace; 
   $str =~ s/$find/$replace/g ;
   return $str ;
} 

my $classpath = "\'"
                . $MYHOME . "/jars/CommonT3v0.jar" 
                . $CPseparator
                . $MYHOME . "/jars/junit.jar" 
                . $CPseparator
                . $MYHOME . "/jars/hamcrest-core-1.3.jar"
                . $CPseparator
                . $MYHOME . "/temp/testcases/"
                . $CPseparator
                . $MYHOME . "/test/classes/"
                . "\'"  ;

sub compileJUnit {
   my $testClass = $MYHOME . "/temp/testcases/TEST_" . replaceIt($_[0],".","_") . ".java";
   print "\n>>> compiling " . $testClass ;
   system($JAVAC 
          . " -cp " . $classpath 
          . " " . $testClass) ;   
}

sub runReplay {
   compileJUnit($_[0]) ;
   my $testClass = "TEST_" . replaceIt($_[0],".","_") ;
   print "\n>>> running " . $testClass ;
   system($JAVA
          . " -cp " . $classpath 
          . " org.junit.runner.JUnitCore " . $testClass) ; 
}

#runReplay("SortedList.BuildSortedList") ;
#runReplay("Triangle.Triangle") ;
#runReplay("Triangle.TriangleType") ;
runReplay("S1") ;
runReplay("S2") ;
