eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

# $Id: dyn_plot 13612 1998-07-29 18:23:08Z levine $
#
# Plots two lines, with min-max ranges, from two DYN.LCL.tbl files.
#
# The first three lines above let this script run without specifying the
# full path to perl, as long as it is in the user's PATH.
# Taken from perlrun man page.

$usage="usage: $0 <first .tbl> <second .tbl>\n";

########
######## Process command line args.
########
while ( $#ARGV >= $[  &&  $ARGV[0] =~ /^-/ ) {
    if ( $ARGV[0] eq '-?' ) {
        print "$usage";
        exit;
    } else {
        print STDERR "$0:  unknown option $ARGV[0]\n";
        die $usage;
    }
    shift;
}

die "$usage" unless $#ARGV == 1;
$tbl1 = $ARGV[0];
$tbl2 = $ARGV[1];

########
######## Plot separately for each number of suppliers.
########
&plot (1);


sub extract {
  my ($input, $suppliers, $output) = (@_);

  open (INPUT, "$input")  ||  die "$0: unable to open $input\n";
  open (OUTPUT, "> $output")  ||  die "$0: unable to open $output\n";

  while (<INPUT>) {
    if (/^$suppliers (\d+) ([\d.]+) ([\d.]+) (\d+) (\d+)/) {
      print OUTPUT "$1 $4 $2 $3\n";
    }
  }

  close OUTPUT;
  close INPUT;
}


sub plot {
  my ($suppliers) = (@_);

  &extract ("$tbl1", $suppliers, "tmpS$suppliers-rms");
  &extract ("$tbl2", $suppliers, "tmpS$suppliers-muf");

  open (GNUPLOT, "| gnuplot")  ||  die "$0: unable to open gnuplot\n";
  print GNUPLOT "set xlabel 'Number of Consumers'\n";
  print GNUPLOT "set ylabel 'Latency, usec'\n";
  print GNUPLOT "set terminal postscript eps color\n";
  print GNUPLOT "set output 'DYN.plot-S$suppliers.eps'\n";
  print GNUPLOT "plot " .
                "'tmpS$suppliers-rms' title 'RMS' w lines, " .
                "'tmpS$suppliers-rms' using (\$1-0.05):2:3:4 " .
                  "notitle w errorbars, " .
                "'tmpS$suppliers-muf' title 'MUF' w lines, " .
                "'tmpS$suppliers-muf' using (\$1+0.05):2:3:4 " .
                  "notitle w errorbars\n";
  close GNUPLOT;

  unlink "tmpS$suppliers-rms", "tmpS$suppliers-muf";
}
