#!/usr/bin/env perl

# Script to test-run T3 to generate test-suite on example test-classes.
# The script will also generate Nx Junit wrappers for replay.

# --------------------------------------------------------
# Few thing that may need to be re-configured
# --------------------------------------------------------

my $MYlaptop = 9 ;
my $UPV = 0 ;
my $deploy = $MYlaptop ;
#my $deploy = $UPV ;

# Need Java8 !

my $MYHOME = "/home/t2" ; 
my $JAVA = "/usr/lib/jvm/java-8-oracle/bin/java" ;
my $CPseparator = ":" ;

if ($deploy == $MYlaptop) { 
    $MYHOME = "." ; 
    $JAVA  = "/cygdrive/c/apps/Java/jdk1.8.0/bin/java" ;
    $CPseparator = ";" ;
}

# --------------------------------------------------------
# Should not change part
# --------------------------------------------------------

my $targetHome     = "./test" ;
my $targetClassDir = $targetHome . "/classes" ;

my $T3 = " Sequenic.T3.T3 "  ;
my $T3jar = $MYHOME . "/jars/T3v0.jar"  ;


my $NUMofTestClasses = 5 ;


my $T3params = "--multicore " 
                . "--nonviolating "
                . "--prefixlength=7 --suffixlength=0 "
                . "--msamples=100 "
                . "--trfile=T3suite "
                . "--splitsuite=" . $NUMofTestClasses . " "
                . "--savedir=" . $MYHOME . "/temp/testcases "
                . "--scandir=" . $targetClassDir ;
                
my $classpath = "\'" 
            . $T3jar . $CPseparator 
            . $targetClassDir
            . "\'" ;

# 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 ;
}      

# as replaceIt, but without replcing metachars:
sub replaceIt2 {
   my $str     = $_[0] ;
   my $find    = $_[1];
   my $replace = $_[2] ;
   $str =~ s/$find/$replace/g ;
   return $str ;
}
  

  
# For creating a Junit test-class wrapper
sub mkJunit {
    my $CUT = $_[0] ;
    my $CUTdashed = replaceIt($CUT,".","_") ;
    my $template  = $MYHOME . "/T3TestClassWrapper_TC_Template.java" ;  
    
    open (TemplateFile,"<",$template);
    
    # read the template Test-class:
    my $Test = "" ;
    while (<TemplateFile>) { $Test = $Test . $_ ; }
    close (TemplateFile); 
    
    # now, generate Nx test-classes
    my $k=0 ;
    while ($k < $NUMofTestClasses) {
       my $testclassName = "TEST_S" . $k ;
       my $testclass     = $MYHOME . "/temp/testcases/" . $testclassName . ".java" ;
       open (TestClassFile,">",$testclass) ;
       my $Instance = replaceIt2($Test, "xxxClass", $testclassName) ;  
       $Instance= replaceIt2($Instance,"xxxSuite", "T3suite_S" . $k) ;
       $Instance = replaceIt2($Instance,"xxxLog"  , $MYHOME . "/logs/REPLAY_" . $CUT . "_S" . $k) ;
       $Instance = replaceIt2($Instance,"xxxTargetDir"  , $MYHOME . "/temp/testcases/") ; 
       print TestClassFile $Instance ;
       close (TestClassFile) ;
       $k ++ ;
    }  
}
    
     
           
# to run T3:
sub runT3 {
    my $CUT = $_[0] ;
    my $LOG = $MYHOME  . "/logs/GEN_" . $CUT ;
    system(
     $JAVA . " -ea -cp " . $classpath
         . $T3 . " " 
         . $T3params 
         . " -lf " . $LOG 
         . ""
         . " " . $CUT
    ) ;
    # now generate Junit wrapper too, to prepare for replay:
    mkJunit($CUT) ;
}



sub mkTempDirs {
   my $DATADir = $MYHOME . "/temp/data" ;
   my $testcasesDir = $MYHOME . "/temp/testcases" ;
   if (!(-e $DATADir)) {
      system("mkdir -p " . $DATADir) ;
   }
   if (!(-e $testcasesDir)) {
      system("mkdir -p " . $testcasesDir) ;
   }
}

# now the runs:
mkTempDirs() ;
#runT3("SortedList.BuildSortedList") ; 
runT3("Triangle.Triangle") ; 
#runT3("Triangle.TriangleType") ; 

