#!/usr/bin/perl
# $Id: iv-plot-report,v 1.1 2006/03/29 07:18:33 skoch Exp $
my $VERSION = 0.2;
my $PROGRAM = 'iv-plot-report';

use AFM;
use strict;
my $usezero = 1;
my $debug = 0;
for my $teststr (@ARGV) {
    if ( $teststr =~ /--version/ ) {
        print "$PROGRAM is: $VERSION\n";
        exit;
    }
    if ( $teststr =~ /--debug/ )
    {
	$debug = 1; 
    }
    if ( $teststr =~ /--nozero/)
    {
	$usezero = 0;
    }
}
if ( scalar(@ARGV) < 4 ) {
    print "Usage: $PROGRAM rignr testnr [iv_curvenr \"label\"]\n";
    exit;
}

my $rigno = 0;
if ( scalar(@ARGV) > 0 ) 
{
    $rigno = $ARGV[0];
    $rigno =~ /(\d+)/;
    $rigno = $1;
}
&check_run($rigno);
my $testno = 0;
if ( scalar(@ARGV) > 1 ) 
{
    my $ttestno = $ARGV[1];
    $ttestno =~ /(\d+)/;
    $testno = $1;
}
my @ivnumbers = ();
my @ivlabels = ();

if ( scalar(@ARGV) > 3 ) {
    for (my $i = 2 ; $i < $#ARGV ; $i += 2 ) {
	if ($ARGV[$i] =~ /^\d+$/)
	{ 
	    print "using iv-curve ",$ARGV[$i], "  with label \'$ARGV[$i+1]\'\n" if($debug);
	    push ( @ivnumbers, $ARGV[$i] );
	    push ( @ivlabels,  $ARGV[ $i + 1 ] );
	}
    }
}



my $string = '';
for (my $x = 0; $x < scalar(@ivnumbers); $x++)
{
    $string = $string . " $ivnumbers[$x] \"$ivlabels[$x]\"";
}
print $string,"\n" if ($debug);

my $cmdstr = "/usr/local/bin/plot_iv_curves $rigno $testno $string";
if ($debug)
{
    $cmdstr = $cmdstr.' --debug';
}
print $cmdstr,"\n" if ($debug);
system($cmdstr);
$cmdstr = "/usr/local/bin/rplot.pl $rigno $testno $string";
if (!$usezero)
{
    $cmdstr = $cmdstr.' --nozero';
}
if ($debug)
{
    $cmdstr = $cmdstr . ' --debug';
}
print $cmdstr,"\n" if ($debug);
system($cmdstr); 
