#!/usr/bin/perl

# perl script that calls loop extraction on every trace in the
#  standard location for full traces
# For each trace, 
#    create a directory to hold the extracted loops,
#    gunzip the trace and pipe it to the java statement that runs
#     runs the extraction program, passing it the just-created
#     directory to put the extracted loops into

$BAADRootDir = $ARGV[0];

print "$BAADRootDir\n";

   #Set where the extraction program directory is
$extractorDir =  "$BAADRootDir/1__Development/0__Code_Dev/".
 "2__DepQ_loop_paper/LoopExtractor/dist";

# String used to run extractor
$extractCmd = "java -jar $extractorDir/LoopExtractor.jar";
 
$tracesBaseDir = "$BAADRootDir/1__Development/2__Traces_and_Runs".
"/8__Traces_shared/full_traces_Fmt4";

$loopsBaseDir = "$BAADRootDir/1__Development/".
 "2__Traces_and_Runs/8__Traces_shared/loop_files/full_loop_traces";



# grab all file names in current directory
# Expecting to be in the directory with all the trace files to be
#  processed
print "tracesDir: $tracesBaseDir\n";
print "loopsDir: $loopsBaseDir\n";
$dired = `ls $tracesBaseDir`;
@fileNames = split("\n", $dired);

@fileNames = ("","");

# filter filenames so have only trace files
#  then extract benchmark name and architecture name from file name
foreach $fileName (@fileNames)
 { print "filename: $fileName\n";
   if ($fileName  =~ /trace_(\w+)_X0_(\w+)\.gz/)
    { $traceName  = $fileName;
      $benchName  = $1;
      print "benchmark: $benchName\n";
      $tracePath = "$tracesBaseDir/$fileName";
      
      # make directory to hold extracted loops
      $loopDir = "$loopsBaseDir/$benchName";
      print "loopDir:  $loopDir\n";
      `mkdir -p $loopDir`;
      
      # run the extraction program
      print `zcat $tracePath | $extractCmd $loopDir`;
      `gzip $loopDir/*.trace`;
    }
 }

