#!/usr/bin/perl -w
use warnings;
use strict;
use File::Copy;

my $file = shift;
my $id = shift;
my $samples = shift;
my $inputPath   = "/gu2/jungma/thermal/in/out-backup";
my $extractPath = "/gu2/jungma/thermal/tmp/$id";

print "Create TMP path... ";
system("mkdir $extractPath");
print " done\n";

print "Extract... ";
system("tar -xf $inputPath/$file -C $extractPath");
print " done\n";

print "Preprocessing... ";
system("sed '1d' $extractPath/out/output_core_die_full.txt  > $extractPath/die0.txt");
system("sed '1d' $extractPath/out/output_mem_die_1_full.txt > $extractPath/die1.txt");
system("sed '1d' $extractPath/out/output_mem_die_2_full.txt > $extractPath/die2.txt");
system("sed '1d' $extractPath/out/output_mem_die_3_full.txt > $extractPath/die3.txt");
system("sed '1d' $extractPath/out/output_mem_die_4_full.txt > $extractPath/die4.txt");
print " done\n";

print "Gnuplot...";
open( my $GP, '|-', 'gnuplot' );
print $GP "
	reset
	set terminal pngcairo size 500,500 enhanced font 'Verdana,10'
	
	set border linewidth 0
	unset key
	unset colorbox
	unset tics
	set lmargin screen 0.0
	set rmargin screen 1.0
	set tmargin screen 1.0
	set bmargin screen 0.0
	
	set pm3d map
	set pm3d interpolate 2,2
	set cbrange [318.0:388.0]

	#set palette rgbformulae  22,13,-31
	set palette defined ( 0 \"blue\", 3 \"green\", 4 \"yellow\", 5 \"red\", 6 \"black\" )
	
	set output '$extractPath/die0.png'
	splot '$extractPath/die0.txt' matrix every ::1
	
	set output '$extractPath/die1.png'
	splot '$extractPath/die1.txt' matrix every ::1
	
	set output '$extractPath/die2.png'
	splot '$extractPath/die2.txt' matrix every ::1
	
	set output '$extractPath/die3.png'
	splot '$extractPath/die3.txt' matrix every ::1
	
	set output '$extractPath/die4.png'
	splot '$extractPath/die4.txt' matrix every ::1";
close($GP);
print " done\n";

print "Wait for files... ";
while (1) { last if -e "$extractPath/die0.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die1.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die2.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die3.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die4.png";  print "."; sleep 1;}
print " done\n";

print "Composite ...";
system("composite -gravity center constants/thermal_model_core.png $extractPath/die0.png $extractPath/die0b.png");
system("composite -gravity center constants/thermal_model_dram_0_1.png $extractPath/die1.png $extractPath/die1b.png");
system("composite -gravity center constants/thermal_model_dram_2_3.png $extractPath/die2.png $extractPath/die2b.png");
system("composite -gravity center constants/thermal_model_dram_4_5.png $extractPath/die3.png $extractPath/die3b.png");
system("composite -gravity center constants/thermal_model_dram_6_7.png $extractPath/die4.png $extractPath/die4b.png");
print " done\n";

print "Wait for files... ";
while (1) { last if -e "$extractPath/die0b.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die1b.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die2b.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die3b.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die4b.png";  print "."; sleep 1;}
print " done\n";

print "Convert ...";
system("convert $extractPath/die0b.png -alpha set -background none -shear 0x-40 -rotate 60  +repage -crop 1048x485+0+205 $extractPath/die0c.png");
system("convert $extractPath/die1b.png -alpha set -background none -shear 0x-40 -rotate 60  +repage -crop 1048x485+0+205 $extractPath/die1c.png");
system("convert $extractPath/die2b.png -alpha set -background none -shear 0x-40 -rotate 60  +repage -crop 1048x485+0+205 $extractPath/die2c.png");
system("convert $extractPath/die3b.png -alpha set -background none -shear 0x-40 -rotate 60  +repage -crop 1048x485+0+205 $extractPath/die3c.png");
system("convert $extractPath/die4b.png -alpha set -background none -shear 0x-40 -rotate 60  +repage -crop 1048x485+0+205 $extractPath/die4c.png");
print " done\n";

print "Wait for files... ";
while (1) { last if -e "$extractPath/die0c.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die1c.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die2c.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die3c.png";  print "."; sleep 1;}
while (1) { last if -e "$extractPath/die4c.png";  print "."; sleep 1;}
print " done\n";

my $outFileName = sprintf("%08d", $id);

print "Produce Output ...";
system("convert -size 1048x1680 xc:black $extractPath/die0c.png -geometry +0+1200 -composite $extractPath/die1c.png -geometry +0+900 -composite $extractPath/die2c.png -geometry +0+600 -composite $extractPath/die3c.png -geometry +0+300 -composite $extractPath/die4c.png -composite $extractPath/a$outFileName.jpg");

while (1) { last if -e "$extractPath/a$outFileName.jpg"; print "."; sleep 1;}
system("convert $extractPath/a$outFileName.jpg -pointsize 80 -fill white -gravity northeast -draw \"text 20,20 '".$samples."ms'\" $extractPath/b$outFileName.jpg");

while (1) { last if -e "$extractPath/b$outFileName.jpg"; print "."; sleep 1;}
system("convert $extractPath/b$outFileName.jpg constants/legend/legend4.png +append ../out/$outFileName.jpg");

print " done\n";

print "Produce Samples ...";
for(my $i = 0; $i < $samples-1; $i++)
{
	$id++;
	my $outFileNameCopy = sprintf("%08d", $id);
	copy("../out/$outFileName.jpg","../out/$outFileNameCopy.jpg");
}
print " done\n";

# cleanup
print "Cleanup ... ";
while (1) { last if -e "/gu2/jungma/thermal/out/$outFileName.jpg"; print "."; sleep 1;}
system("rm -rf $extractPath");
print " done\n";

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