#! /usr/bin/perl

# script to plot x y iperf graph from cooked files

# plot data
use Chart::Graph::Gnuplot qw(gnuplot);



if ( $#ARGV < 0 ) {
    &printHelp;
}


# defaults
$style = 'linespoints';
$outFile = 'graph.gif';
$xlog = 0;
$ylog = 0;


# parse through the args
for ( $i=0; $i<$#ARGV+1; $i++ ) {

    if( $ARGV[$i] =~ /^-(.*)/ ) {

	$switch = $1;

	# connection host
	if ( $switch eq 'f' ) {
	    $logFile = $ARGV[$i+1];
	}
	if ( $switch eq 'o' ) {
	    $outFile = $ARGV[$i+1];
	}
	elsif ( $switch eq 'x' ) {
	    $xaxis = $ARGV[$i+1];
	}
	elsif ( $switch eq 'y' ) {
	    $yaxis = $ARGV[$i+1];
	}
	elsif ( $switch eq 'k' ) {
	    $key = $ARGV[$i+1];
	}
	elsif ( $switch eq 't' ) {
	    $title = $ARGV[$i+1];
	}
	elsif ( $switch eq 's' ) {
	    $style = $ARGV[$i+1];
	}
	elsif ( $switch eq 'd' ) {
	    $dimension = $ARGV[$i+1];
	}
	elsif ( $switch eq 'xlog' ) {
	    $xlog = 1;
	}
	elsif ( $switch eq 'ylog' ) {
	    $ylog = 1;
	}
	elsif ( $switch eq 'h' ) {
	    &printHelp;
	}


    }


}


# default key
if ( $key eq '' ) {
    $key = $yaxis;
}


#print "$xaxis, $yaxis\n";


if ( $logFile =~ /.web100$/ ) {
    $web100 = 1;
   # print "web100 file $web100\n";
}


#print "$web100\n";

# config graph type
my ( $temp, $graphType ) = split /\./, $outFile;

if ( $title eq '' ) {
    $title = $outFile;
}

# config graph
if ( $dimension eq '' ) {
    $xdim = 1;
    $ydim = 1;
}
else {
    my ( $x, $y) = split /x/, $dimension;
    $xdim = $x / 640;
    $ydim = $y / 480;
}



#print "\n$outFile\n$temp\n$graphType\n";

sub printHelp {
print "Usage: graph.pl -f <cook_file> -x <x_axis> -y <y_axis> [-t <title>] \n";
exit;
}

@xarray;
@yarray;

$grab = 0;


# load the data
open ( TABLE, "<$logFile" );
while( <TABLE> ) {

    $line = $_;


    if ( $grab eq 1 ) {
	
	if( /^\D/ ) {
	    # get titles
	    @titles = split /\t/, $line;
	    
	    for( my $i=0; $i<$#titles+1; $i++) {
		
	#	print "t: $titles[$i]\n";
		
		if ( $titles[$i] eq $xaxis ) {
		    $xtab = $i;
		}
		if ( $titles[$i] eq $yaxis ) {
		    $ytab = $i;
		}
		
	    }
	    
	    if ( $xtab eq '' || $ytab eq '' ) {
		print STDERR "Axis not found - try:\n\n";
		foreach my $axis (@titles) {
		    print $axis . "\n";
		}

		exit(1);
	    }
	}
	
	if ( /^\d/ ) {
	    # only grab the tabs that we need andfill into array
	    my @array = split /\t/, $line;
	push ( @xarray, $array[$xtab] );
	    push ( @yarray, $array[$ytab] );
	}

	
    }



    if ( $web100 eq 1 ) {

#	print "IN web100!\nline: $line\n\n";

       	# only grab titles after a header
	if ( $line =~ /===========Monitoring Web100 Output===========/ ) {
	#   print "WEB100 header\n";
	   $grab = 1;
	}
	
    }
    else { 
	
	print "ELSE\n";
$grab = 1; 
	if( /^\D/ ) {
	    # get titles
	    @titles = split /\t/, $line;
	    
	    for( my $i=0; $i<$#titles+1; $i++) {
		
		print "t: $titles[$i]\n";
		
		if ( $titles[$i] eq $xaxis ) {
		    $xtab = $i;
		}
		if ( $titles[$i] eq $yaxis ) {
		    $ytab = $i;
		}
		
	    }
	    
	    if ( $xtab eq '' || $ytab eq '' ) {
		print STDERR "Axis not found\n";
		exit(1);
	    }
	}}


}


# fix the time
if ( $xaxis eq "CurrTime_(Delta)" ) {
    $xarray[0] = '0';
    for( $i = 1; $i < $#xarray + 1 ; $i++ ) {
	$xarray[$i] /= 1000000;

	$xarray[$i] +=  $xarray[$i-1];
    }
}


print "x: @xarray\ny: @yarray\n";

#print "$xdim $ydim";

gnuplot({"title" => $title,
	 #    "xrange" => "[:11]",
	 #    "yrange" => "[:45]",
	 "output file" => $outFile,
	 "output type" =>  $graphType,
	 "x-axis label" => $xaxis,
	 "y-axis label" => $yaxis,
	 "logscale x" => $xlog,
	 "logscale y" => $ylog,
	 "extra_opts" => join("\n", "set size $xdim,$ydim", "set grid", ),


     },
	# dataset 1
	[{"title" => $key,
	  "style" => $style,
               "using" => "1:2",
	  "type" => "columns"},
              [ @xarray ], # x
	 [ @yarray ], # y
	 ],
	);



system ( "ee $outFile" );
