#!/usr/bin/perl -w
use warnings;
use strict;
use List::Util qw( min max );

#/Volumes/Etana_tmp2/tmp_sadri/backup/sep_16/log_smartbench_traces_50_cpu_1650_mhz_100_mhz_dram_100_mhz_sampling8X_bankwise_on_test/out-backup
my $inputPath    = "../in/out-backup";
my $extractPath  = "../tmp";
my $samplingFile = "../in/sampling";

# Read the input file names and store it in an array
opendir (DIR, $inputPath) or die $!;

my @files;
my @sortetFiles;

while (my $file = readdir(DIR))
{
	push(@files, $file);
}

@sortetFiles = sort @files;


print "Cleanup\n";
system("rm -rf *.err *.out");
system("rm -rf ../tmp/*");
print "Load modules\n";


# Estimate sampeling numbers

open(SF,$samplingFile);

my @refreshRates;

while(<SF>)
{
	$_ =~ /(\d+)\.0/;
	my $rate = $1;
	push(@refreshRates, $rate);
}

print "Max. Rate:".max(@refreshRates)."\n";
print "Min. Rate:".min(@refreshRates)."\n";

my $counter = 0;
my $i       = 0;
# For each data package start one job!
foreach(@sortetFiles)
{
	my $file = $_;

	if($file =~ /(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)\.tgz/)
	{
		my $samples = $refreshRates[$i];
		system("bsub -W 00:05 ./temperatur.job.pl $file $counter $samples");
		$counter+=$samples;
		$i++;
		#if($i == 100)
		#{
		#	last;
		#}
	}
}

##ffmpeg -start_number 00000000 -i %08d.jpg -vcodec mpeg4 test.avi
