#! /usr/bin/perl

# script to run two other scripts together
# and then poll the results from the run into a 2d table of values

########################################################################
#
# History
#   31-07-02: initial release
#
########################################################################


require 'ping-cook.pl';


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

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

	$switch = $1;

	
	if ( $switch eq 'd' ) {
	    $destip = $ARGV[$i+1];
	}
	elsif ( $switch eq 'n' ) {
#	    @pktss = split( /,/, $ARGV[$i+1] );
	    $pkts = $ARGV[$i+1];
	}
	# packet intervals
	elsif ( $switch eq 'i' ) {
	    @intervals = split( /,/, &grab_input ( $ARGV[$i+1] ));
	}
	elsif ( $switch eq 's' ) {
	    @sizes = split( /,/, &grab_input( $ARGV[$i+1]) );
	}	
	elsif ( $switch eq 'f' ) {
	    $outdirectory = $ARGV[$i+1];
	}
	elsif ( $switch eq 'h' ) {
	    &printHelp;
	}

    }


}


sub printHelp{
    print "Usage: do_packetsize-vs-interval.pl -d <sink> [-c <prime cache number>] [-n <number of pkts>] [-f <directory for logs>] [-s <s1,s2,s3...>(bytes)] [-i <i1,i2,i3...>(msec)]\n";
exit;
}

# go through an put default values

if ( $destip eq '' ) { &printHelp; }
if ( $dest_port eq '' ) { $dest_port = 14233; }
if ( $pkts eq '' ) { $pkts = 1000; }
if ( $#intervals eq -1 ) {
    @intervals = ('10', '20', '30', '40', '50'); 
}
if ( $cache = '' ) { $cache = 0; }
if ( $#sizes eq -1  ) { 
    @sizes = ( '100', '200', '300', '400', '500', '600' , '700', '800', '900', '1000', '1100', '1200' , '1300', '1400' ); 
}


# form sizes into a comma separated list
$size = "@sizes";
$size =~ s/\s+/,/g;
#print 'sizes: .' . $size . '.' ."\n";
#print "int: @intervals\n";


# check directory for output
unless ( $outdirectory =~ /\/$/ ) {
    $outdirectory = $outdirectory . '/';
}
my @dir = split /\//, $outdirectory;

for( my $j=0; $j<=$#dir; $j++) {

    my $next = $dir[$j+1];
    if( $j < $#dir ) {
	$dir[$j+1] = $dir[$j] . '/' . $next;
    }
    `mkdir $dir[$j]`;
 #   print "mkdir $dir[$j]\n";
}


@files;
$n=0;


my @allsizes = split (/,/, $size);
if ( $#allsizes > 4 ) {
    $sizelog = sprintf( $allsizes[0] . '-to-' . $allsizes[$#allsizes] );
}



# write out to a tmp file and repeat... poll log files created and create 2d table
foreach my $interval (@intervals) {

    
    $files[$n] = 'ping-summary_' . "n$pkts" . '_i' . $interval . '_s' . $sizelog . '.log';

    $files[$n] = $outdirectory . $files[$n];

    system("./do_packetsize.pl -d $destip -c $cache -n $pkts -i $interval -s $size -f $files[$n]");

    $n++;

} 



# parse the files and create a hash table of values
my %values = &parse_values( @files );


# repeat for each variable and create readable filename for each


# generate a table
# list of vars
@variables = &remove_default_titles( &get_titles( $files[0]));
print "vars: @variables\n";

#get the source 
$source = `hostname`; chomp($source);

# generate comma sep list of parameters
# convert intervals to secs
foreach my $int (@intervals) {
    $int /= 1000;
}
my $int_param = &convert_array_to_cvs (@intervals);
# convert size to +8
foreach my $si (@sizes) {
    $si += 8;
}
my $size_param = &convert_array_to_cvs( @sizes );

print "Generating tables in $outdirectory...\n";

foreach my $var (@variables) {

    my $table_output = $outdirectory . $source . '_' . $destip . '_n' . $pkts . '_' . $var .  '.log';

    # adjust to read in the user defined intervals and packet sizes
    &write_variable_table( $var, $int_param, $size_param, $table_output , %values );

}


exit;


sub convert_array_to_cvs 
{
    my @array = @_;
    my $line = "@_";
    $line =~ s/\s/,/g;
    return $line;
}


sub get_titles
{
    my ( $filename ) = @_;

    @vars;

    open ( FILE, "<$filename" );
    while( <FILE> ) {

	#my $line = chomp( $_ );

	if ( /^\D/ ) {
	    @vars = split( /\t/, $_ );
	}  
    }
    
    foreach my $v (@vars) {
	chomp($v);
    }

    return @vars;
}

sub remove_default_titles
{

    my @vars = @_;

    for ( my $i=0; $i<=2; $i++ ) {
	shift(@vars);
    }

    return @vars;
}


sub parse_values
{

    my( @input_files ) = @_;
    
    # need to summarise the log files and create table
    print "@input_files\n";
    
    $web100 = 0;
    %result;
  
    foreach my $f (@input_files) {
	
	open( FILE, "<$f" );
	while ( <FILE> ) {
	    
	    # get the lines interval and packet sizes to base table on
	    
	    my $line = $_;
	    chomp($line);
	    
	    # check if its an web100 file
	    if ( $web100 eq 1 ) {
		
		# only grab titles after a header
		if ( $line =~ /===========Monitoring Web100 Output===========/ ) {
		    #   print "WEB100 header\n";
		    $web100 = 0;
		}
		next;
		
	    }
	    
	    # start grabbing the data
	    if ( $grab eq 1 ) {
		
		if ( /^\d/ ) {
		    
		    # find the x tabs 
		    # only grab the tabs that we need and fill into array
		    my @array = split /\t/, $line;
		    
		    for( my $i=0; $i<=$#titles; $i++ ) {
			
			$result{$array[$interval]}{$array[$packetsize]}{$titles[$i]} = $array[$i];
			
			if ( $debug eq 1 ) {
			    print "t:$titles[$i] , i:$array[$interval] , p:$array[$packetsize] is $array[$i]\t\t";
			    print "==>$result{$array[$interval]}{$array[$packetsize]}{$titles[$i]}\n";
			}
		    }
		    
		}
		
		$line_number++;
	    }
	    
	    
	    # if it's a normal file...
	    if ( $web100 eq 0 ) {
		
		if( /^\D/ ) {
		    
		    # get a list of titles to check arguments against
		    @titles = split /\t/, $line;
		
		    # go through each title and find out the tab positions
		    for( my $i=0; $i<$#titles+1; $i++) {
			
			# find the x tab position
			if ( $debug ) { print " $i: $titles[$i]\n"; }
			if ( $titles[$i] eq 'interval' ) {
			    $interval = $i;
			}
			
			if ( $titles[$i] eq 'packet_size' ) {
			    $packetsize = $i;
			}
			
			# build a hash of values
			$title{$titles[$i]} = $i;
		    }
		    
		}	
		
      		#    print "interval: $interval, packetsize: $packetsize\n";
		
		$grab = 1; 
		
	    }
	    
	}
	close( FILE );
			
    }

     return %result;

} # parse_values



# write a file

sub write_variable_table {

    my ( $variable, $x_in, $y_in , $output_file, %hash ) = @_;

    my @x = split /,/, $x_in;
    my @y = split /,/, $y_in;

    print " $output_file\n";
    open( OUT , ">$output_file" );

    # write the table
    # print first line (the intervals)
    print OUT "\t";
    foreach my $int (@x) {
	print OUT "$int\t";
    }
    print OUT "\n";
    
    foreach my $size (@y) {
	print OUT "$size\t";
	foreach my $int (@x) {
	    print OUT "$hash{$int}{$size}{$variable}\t";
	}
	print OUT "\n";
    }
    
    close (OUT);

    return 0;
}
