DATA=$1.out
PROG=$1
PIX=$2.gif
if [ "$2" = "" ]; then PIX=$1.gif; fi

cat >.plotperl <<"EOF"
$ln=0; $lx=-1000;
while (<>) {
if(/(\d+)\s+R\s+(\d+)\s+(\d+)/) {
    $x = $1/1000; $y = -0.1; $y1 = $y+0.02;  $y2 = $y + 0.05;
    if ($x-$lx<2) { $ln=($ln+1)%3; } else { $ln=0; }
    $y-=$ln*0.05; $lx=$x; $lb=$3;
    $lb=int($3/1000)."k" if ($lb>=10000);
    print "set label '$2:$lb' at $x,graph $y center\n";
    print "set arrow from $x,graph $y1 to $x,graph $y2\n";
}
}
EOF
perl .plotperl $PROG > .plot

cat >>.plot <<EOF
set y2t 
set grid
set bmargin 6
set key spacing 1.4 box
set data style linespoints
set term gif transparent tiny size 320,240 xffffff x000000 x404040 xc00000 x009000 x0000ff \\
	xc09000 xc000c0 x0090c0 x804020
set out "$PIX"
set label "time [s]" at graph -0.02,graph -0.05 right
set ylabel "rate [Bps]"
#set y2label "time [ms]"
set ytics nomirror
set noy2tics

plot '$DATA' using 1:3 title 'WWW rate',\\
	'$DATA' using 1:7 title 'SMTP rate',\\
	'$DATA' using 1:11 title 'Agency B rate',\\
	'$DATA' using 1:(\$3+\$7) title 'Agency A rate',\\
	'$DATA' using 1:(\$3+\$7+\$11) title 'total rate'
EOF
#	'$DATA' using 1:4 axes x1y2 title 'flow 0 delay',\\
#	'$DATA' using 1:8 axes x1y2 title 'flow 1 delay',\\
#	'$DATA' using 1:12 axes x1y2 title 'flow 2 delay'
#EOF

gnuplot .plot
xv $PIX
