#!/usr/bin/perl
# $Id: remake_latex_report,v 1.11 2015/10/09 10:57:13 sqko Exp $
use strict;
use warnings;
use RFC::Rig;
use RFC::Main;
use LATEX;
use File::Copy;
chdir;
my $VERSION = 3.0;
my $PROGRAM = 'remake_latex_report';
my $type = &get_cv('GNUPLOT','output_type');
$type = 'pdf' unless ($type);
if ( scalar(@ARGV) < 1 ) 
{
    print
'Usage: make_report [otional --version] $rig [optional: $testno --debug --inter]'.",\n".'where $rig is the rig number';
    print '$testno is the test number'."\n".'if an other test than the current is to be processed',
      "\n";
    exit 1;
}
my $debug = 0;
my $test = 0;
my $edit = 0;
my $plotonly = 0;
my $do = 1;
my $my_tex_file = '';
my $remake = 0;
for (my $x = 0; $x < scalar(@ARGV);$x++)
{
    my $string = $ARGV[$x];
    if ($string =~ /--version/)
    {
	print "$PROGRAM is: $VERSION\n";
	exit 0;
    }
    if ($string =~ /--debug/)
    {
	$debug = 1;
    }
    if ($string =~ /--test/)
    {
	$test = 1;
    }
    if (($string =~ /--file/) && ($x < (scalar(@ARGV)-1)))
    {
	$my_tex_file = $ARGV[$x+1];
    }
    if ($string =~ /--recompile/)
    {
	$remake = 1;
    }
    if ($string =~ /--help/)
    {
	print "\n\n",'remake_latex_report compiles the latex report for the test given by the arguments:',"\n\n",'   remake_latex_report $rig $test [opt --file $file]',"\n\n",'If the --file argument is given the file specified is used instead of generating a new one using make_latex_report.',"\n",'The output files are the same as for make_latex_report and are located the same place',"\n\n";
	exit;
    }
}


my $rig = RFC::Rig->new($ARGV[0]);

$rig->check_run;
my $testno = $rig->session;
if (scalar(@ARGV) > 1) 
{
    my $string = $ARGV[1];
    $string =~ /(\d+)/;
    my $ttest = $1;
    if (defined($ttest))
    {
	$testno = $ttest+0;
    }
}

my $dir = &get_path;
my $cell = &get_cell;
my $texfilename = $rig->name.'test'.$testno.'_'.$cell;
my $texfile = '/home/rig'.$rig->name.'/'.$texfilename.'.tex';
my $shorttexfile = '/home/rig'.$rig->name.'/'.$texfilename.'_short.tex';
if ($my_tex_file eq '')
{
    my $command = '';
    my $f;
    if ($remake)
    {
	$f = &get_path . $texfilename.'.tex';
	my $string = '';
	if (-e $f)
	{
	    open MYIN,'<',$f or die "Could not open $f $!";
	    while (my $s = <MYIN>)
	    {
		$string .= $s;
	    }
	    close MYIN;
	}
	$f =~ s/$texfilename\.tex/manual_info.txt/;
	my $ostr = '';
	if ($string && $string =~ /\%start(.*)\%end/)
	{
	    $ostr = $1;
	}
	open OFH, '>', $f or die "Could not open ouput file $f\n";
	print OFH $ostr;
	close(OFH);
	$command = "/usr/local/bin/make_latex_report ".$rig->name." $testno --remake > $texfile";
	print $command,"\n" if ($debug);
	system($command);
	$command = "/usr/local/bin/make_latex_report ".$rig->name." $testno --remake --short > $shorttexfile";
	print $command,"\n" if ($debug);
	system($command);
    }
    else
    {
	$command = "/usr/local/bin/make_latex_report ".$rig->name." $testno > $texfile";
	print $command,"\n" if ($debug);
	system($command);
	$command = "/usr/local/bin/make_latex_report ".$rig->name." $testno --remake --short > $shorttexfile";
	print $command,"\n" if ($debug);
	system($command);
    }
    unlink($f) if (-e($f));
}
else
{
    $texfile = $my_tex_file;
    $shorttexfile = $my_tex_file;
}

print "\n#########  Compiling reports ##########\n\n";
    
    my $pdf = '';
my $shortpdf = '';
if ($type eq 'ps')
{
    $pdf = &LATEX::compile_doc_ps($texfile,1);
    $shortpdf = &LATEX::compile_doc_ps($shorttexfile,1);
}
else
{
    $pdf = &LATEX::compile_doc($texfile,1);
    $shortpdf = &LATEX::compile_doc($shorttexfile,1);
}
my $publicreport = '/home/rig'.$rig->name.'/'.$rig->name.'test'.$testno.'/'.$texfilename.'.'.$type;
my $shortpublicreport = '/home/rig'.$rig->name.'/'.$rig->name.'test'.$testno.'/'.$texfilename.'_short.'.$type;
move($pdf,$publicreport) or die &errorlog("Could not move file $pdf to $publicreport!");
move($shortpdf,$shortpublicreport) or die &errorlog("Could not move file $shortpdf to $shortpublicreport!");

my $publictex = '/home/rig'.$rig->name.'/'.$rig->name.'test'.$testno.'/'.$texfilename.'.tex';
copy ($texfile,$publictex) or die &errorlog("Could not copy file $texfile to $publictex!");
my $shortpublictex = '/home/rig'.$rig->name.'/'.$rig->name.'test'.$testno.'/'.$texfilename.'_short.tex';
copy ($shorttexfile,$shortpublictex) or die &errorlog("Could not copy file $shorttexfile to $shortpublictex!");
$publictex = $RFC::Header::publicdir.'rig'.$rig->name.'/'.$rig->name.'test'.$testno.'/'.$texfilename.'.tex';
move ($texfile,$publictex) or die &errorlog("Could not move file $texfile to $publictex!");
$shortpublictex = $RFC::Header::publicdir.'rig'.$rig->name.'/'.$rig->name.'test'.$testno.'/'.$texfilename.'_short.tex';
move ($shorttexfile,$shortpublictex) or die &errorlog("Could not move file $shorttexfile to $shortpublictex!");
exit;

sub get_path
{
    my $dir = $RFC::Header::publicdir.'rig'.$rig->name.'/'.$rig->name.'test'.$testno.'/';
    return $dir;
}


sub get_cell
{
    my $file = &get_path.'info.txt';
    open MYIN,'<',$file or die "Could not open file $file $!";
    my $name = '';
    while (my $s = <MYIN>)
    {
	if ($s =~ /(?:Cell|Sample) number\s+:\s+([\w\-]+)/i)
	{
	    $name = $1;
	    last;
	}
    }
    close MYIN;
    return $name;
}
