#!/usr/bin/perl
# $Id: copyplot.pl,v 1.1 2012/12/17 13:19:58 sofc Exp $

use AFM;
use strict;

if (scalar(@ARGV) < 2)
{
    exit;
}


my $rig = shift;
my $test = shift;

my $dir = $AFM::publicdir.'rig'.$rig.'/'.$rig.'test'.$test.'/';
my $tempiv = $dir.'iv/tempvariation';
my $fig4iv = $dir.'iv/fig4iv';
my $figdrywet = $dir.'iv/drywet-iv';
my @lines = `cat $tempiv`;
my @fig4lines = `cat $fig4iv`;
my @drywet = `cat $figdrywet`; 
my $pngsourcedir = $dir.'iv/custom/png/';
my $pssourcedir = $dir.'iv/custom/ps/';
my $txtsourcedir = $dir.'iv/custom/txt/';
my $pngtargetdir = $dir.'png/';
my $pstargetdir = $dir.'ps/';
my $txttargetdir = $dir.'txt/';


if (!(-e($txttargetdir)))
{
    my $cmd = "/bin/mkdir $txttargetdir";
    system($cmd);
}

if (scalar(@lines) > 1)
{
    my $source = '';
    for my $st (@lines)
    {
	chomp $st;
	$source = $source."$st-";
    }
    $source = $source.'iv';
    my $command = '/bin/cp '.$txtsourcedir.$source.'.txt '.$txttargetdir.'iv-temp-var.txt';
    system($command);
    my $command = '/bin/cp '.$pssourcedir.$source.'.eps '.$pstargetdir.'iv-temp-var.ps';
    system($command);
    if (-e($pssourcedir.'iv-temp-var.ps'))
    {
	$command = '/usr/local/bin/ps2png '.$pssourcedir.'iv-temp-var.ps '.$pngtargetdir.'iv-temp-var.png';
	system($command);
    }
    $source = 'R_s_vs_I';
    for my $st (@lines)
    {
	chomp $st;
	$source = $source."_$st";
    }
    $command = '/bin/cp '.$pngsourcedir.$source.'.png '.$pngtargetdir.'R_s-temp-var.png';
    system($command);
    $command = '/bin/cp '.$txtsourcedir.$source.'.txt '.$txttargetdir.'R_s-temp-var.txt';
    system($command);
    $command = '/bin/cp '.$pssourcedir.$source.'.ps '.$pstargetdir.'R_s-temp-var.ps';
    system($command);

    my $source = 'ASR_vs_I';
    for my $st (@lines)
    {
	chomp $st;
	$source = $source."_$st";
    }
    $command = '/bin/cp '.$pngsourcedir.$source.'.png '.$pngtargetdir.'ASR-temp-var.png';
    system($command);
    $command = '/bin/cp '.$txtsourcedir.$source.'.txt '.$txttargetdir.'ASR-temp-var.txt';
    system($command);
    $command = '/bin/cp '.$pssourcedir.$source.'.ps '.$pstargetdir.'ASR-temp-var.ps';
    system($command);
}

if (scalar(@fig4lines) > 1)
{
    my $source = '';
    for my $st (@fig4lines)
    {
	chomp $st;
	$source = $source."$st-";
    }
    $source = $source.'iv';
    my $command = '/bin/cp '.$txtsourcedir.$source.'.txt '.$txttargetdir.'iv-fig4.txt';
    system($command);
    my $command = '/bin/cp '.$pssourcedir.$source.'.eps '.$pstargetdir.'iv-fig4.ps';
    system($command);
    if (-e($pssourcedir.'iv-fig4.ps'))
    {
	$command = '/usr/local/bin/ps2png '.$pssourcedir.'iv-fig4.ps '.$pngtargetdir.'iv-fig4.png';
	system($command);
    }
    $source = 'ASR_vs_I';
    for my $st (@fig4lines)
    {
	chomp $st;
	$source = $source."_$st";
    }
    $command = '/bin/cp '.$pngsourcedir.$source.'.png '.$pngtargetdir.'ASR-fig4.png';
    system($command);
    $command = '/bin/cp '.$txtsourcedir.$source.'.txt '.$txttargetdir.'ASR-fig4.txt';
    system($command);
    $command = '/bin/cp '.$pssourcedir.$source.'.ps '.$pstargetdir.'ASR-fig4.ps';
    system($command);

    $source = 'R_s_vs_I';
    for my $st (@fig4lines)
    {
	chomp $st;
	$source = $source."_$st";
    }
    $command = '/bin/cp '.$pngsourcedir.$source.'.png '.$pngtargetdir.'R_s-fig4.png';
    system($command);
    $command = '/bin/cp '.$txtsourcedir.$source.'.txt '.$txttargetdir.'R_s-fig4.txt';
    system($command);
    $command = '/bin/cp '.$pssourcedir.$source.'.ps '.$pstargetdir.'R_s-fig4.ps';
    system($command);
}

if (scalar(@drywet) > 1)
{
    my $source = '';
    for my $st (@drywet)
    {
	chomp $st;
	$source = $source."$st-";
    }
    $source = $source.'iv';
    my $command = '/bin/cp '.$txtsourcedir.$source.'.txt '.$txttargetdir.'iv-drywet.txt';
    system($command);
    my $command = '/bin/cp '.$pssourcedir.$source.'.eps '.$pstargetdir.'iv-drywet.ps';
    system($command);
    if(-e($pssourcedir.'iv-drywet.ps'))
    {
	$command = '/usr/local/bin/ps2png '.$pssourcedir.'iv-drywet.ps '.$pngtargetdir.'iv-drywet.png';
	system($command);
    }
    $source = 'R_s_vs_I';
    for my $st (@drywet)
    {
	chomp $st;
	$source = $source."_$st";
    }
    $command = '/bin/cp '.$pngsourcedir.$source.'.png '.$pngtargetdir.'R_s-drywet.png';
    system($command);
    $command = '/bin/cp '.$txtsourcedir.$source.'.txt '.$txttargetdir.'R_s-drywet.txt';
    system($command);
    $command = '/bin/cp '.$pssourcedir.$source.'.ps '.$pstargetdir.'R_s-drywet.ps';
    system($command);

    my $source = 'ASR_vs_I';
    for my $st (@drywet)
    {
	chomp $st;
	$source = $source."_$st";
    }
    $command = '/bin/cp '.$pngsourcedir.$source.'.png '.$pngtargetdir.'ASR-drywet.png';
    system($command);
    $command = '/bin/cp '.$txtsourcedir.$source.'.txt '.$txttargetdir.'ASR-drywet.txt';
    system($command);
    $command = '/bin/cp '.$pssourcedir.$source.'.ps '.$pstargetdir.'ASR-drywet.ps';
    system($command);
}

my $source = 'Air_var_H2flow*';
my $pssourcedir = $dir.'iv/ps/';
my $command = '/bin/cp '.$pssourcedir.$source.' '.$pstargetdir.'Air_var_H2flow.ps';
system($command);

$source = 'H_2_var_Airflow*';
$command = '/bin/cp '.$pssourcedir.$source.' '.$pstargetdir.'H_2_var_Airflow.ps';
system($command);

$source = 'iv-Air-H2flow*';
$command = '/bin/cp '.$pssourcedir.$source.' '.$pstargetdir.'iv-Air.ps';
system($command);

$source = 'iv-H_2-Airflow*';
$command = '/bin/cp '.$pssourcedir.$source.' '.$pstargetdir.'iv-H_2.ps';
system($command);
my @files = ('Air_var_H2flow','H_2_var_Airflow','iv-Air','iv-H_2','iv-temp-var');

for my $st (@files)
{
    my $source = $pstargetdir.$st.'.ps';
    my $target = $pngtargetdir.$st.'.png';
    if (-e($source))
    {
	my $cmd = "/usr/local/bin/ps2png $source $target";
	system($cmd);
    }
}
