#! /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;
$web100 = 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' ) {

	    my $yaxis_arg = $ARGV[$i+1];

	    if ( $yaxis_arg =~ /,/ ) {
		@yaxis = split/,/, $yaxis_arg;
	    }
	    else {
		@yaxis[0] = $yaxis_arg;
	    }
	    print "yaxis: @yaxis\n";

	}
	elsif ( $switch eq 'k' ) {
	    $key_arg = $ARGV[$i+1];
	    if ( $key_arg =~ /,/ ) {
		@key = split/,/, $key_arg;
	    }
	    else {
		@key[0] = $key_arg;
	    }
	 #   print "yaxis: @yaxis\n";

	}
	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;
    $key = 'blah';
}


#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>,<y_axis>...} [-t <title>] [-d <xdimxydim>] [-k <key>]\n";
    exit;
}

@xarray;
@yarrays;

$grab = 0;

@ytab;

$line_number = 0;

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

    $line = $_;

    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";
	    $web100 = 0;
	}
	next;
	
    }


    if ( $grab eq 1 ) {
	
	
	if ( /^\d/ ) {
	    # only grab the tabs that we need andfill into array
	    my @array = split /\t/, $line;
	    push ( @xarray, $array[$xtab] );
	
	#    print "ytabs: @ytab\n";
    
	    for( my $j=0; $j<$#yaxis+1 ; $j++ ) {


#		print "adding y$j: $array[$ytab[$j]]\t";
		$yarrays[$j][$line_number] = $array[$ytab[$j]];
		    
	#	push ( @yarrays[$j], $array[$ytab[$j]] );
		

		}
	    
#	    print "\n";
	}
	
	$line_number++;
    }
    
    
       
    if ( $web100 eq 0 ) {
	
	print "Getting titles\n";

	if( /^\D/ ) {
	    # get titles
	    @titles = split /\t/, $line;
	    
	    for( my $i=0; $i<$#titles+1; $i++) {
		
		print "$i: $titles[$i]\n";
		
		if ( $titles[$i] eq $xaxis ) {
		    $xtab = $i;
		}

		for( my $k=0; $k<$#yaxis+1 ; $k++ ) {
		    
		#    print "    $yaxis[$k]\t";
		    if ( $titles[$i] eq $yaxis[$k] ) {
			$ytab[$k] = $i;
		    }
		}

	#	print "  yaxis numbers: @ytab\n";

	    }
	    
	    if ( $xtab eq '' ) {
		print STDERR "Axis $xaxis (x) not found, try\n";
		&printTitles;
		exit(1);
	    }
	    if ( $#ytab != $#yaxis ) {
		print STDERR "Axis y not found, try\n";
		&printTitles;
		exit(1);
	    }



	}
	

	$grab = 1; 

    }
}



sub printTitles {

    foreach my $t (@titles) {
	print "$t\n";
    }

}





# 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\n"; #y: @yarray\n";

#rint "yarrays: size=$#yarrays\n";

	
if ( $#key >= 0 ) {
    @keys = @key;
}
else {
    @keys = @yaxis;
}

for( my $i=0; $i<=$#yarrays ; $i++ ) {

   print "y$i: @{@yarrays->[$i]}\n";
   
   $plot{$i} = [{"title" => $keys[$i], "style" => $style, "using" => "1:2", "type" => "columns"}, [ @xarray ], [ @{@yarrays->[$i]} ], ];

}


#print "size: $#xarray, $#{@yarrays->[0]}";

$ylabel = sprintf( "@yaxis" );

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

#determine how many graphs we have
if ( $#yarrays eq 0 ) {
    gnuplot(\%gnuplot_options,
	    $plot{0}
	    );
}
elsif  ( $#yarrays eq 1 ) 
{
    gnuplot(\%gnuplot_options,
	    $plot{0}, $plot{1}
	    );
}
elsif  ( $#yarrays eq 2 ) {

    gnuplot(\%gnuplot_options,
	    # dataset 1
	    $plot{0}, $plot{1}, $plot{2}
	    );
}
elsif  ( $#yarrays eq 3 ) {

    gnuplot(\%gnuplot_options,
	    $plot{0}, $plot{1}, $plot{2}, $plot{3}
	    );
}
elsif  ( $#yarrays eq 4 ) {

    gnuplot(\%gnuplot_options,
	    # dataset 1
	    $plot{0}, $plot{1}, $plot{2}, $plot{3}, $plot{4}
	    );
}
elsif  ( $#yarrays eq 5 ) {

    gnuplot(\%gnuplot_options,
	    $plot{0}, $plot{1}, $plot{2}, $plot{3}, $plot{4}, $plot{5}
	    );
}
elsif  ( $#yarrays eq 6 ) {
    gnuplot(\%gnuplot_options,
	    $plot{0}, $plot{1}, $plot{2}, $plot{3}, $plot{4}, $plot{5}, $plot{6}
	    );
}
elsif  ( $#yarrays eq 7 ) {
    gnuplot(\%gnuplot_options,
	    $plot{0}, $plot{1}, $plot{2}, $plot{3}, $plot{4}, $plot{5}, $plot{6}, $plot{7}
	    );
}


system ( "ee $outFile" );
