#!/bin/bash
# Generates a jgraph file from the ttcp throughput results for medianet
#
# Usage: arguments come in twos.  The first of each pair is the title of
#   of the curve, and the second is the TTCP throughput file.  The idea
#   is to have one curve for each memory-management configuration.

TMPFILE=/tmp/thruput$$
CRUNCH=./crunch
HIST=./hist
LINETYPES="solid dashed dotted longdash dotdash dotdotdash dotdotdashdash"
MARKTYPES="circle box diamond triangle x cross circle box"
FILLTYPES="0 1 .5 0 1 .5 0 1"

function usage {
  echo "usage: $0 [title throughput-results-file]+"
  exit 1
}

# nth s n
#   where s is a space-separated string
#         n is an index into the string
function nth {
  echo $1 | awk "{ print \$$2; }"
}

# process each input file
linetype=1

echo "newgraph"
echo "  xaxis log label : packet size (bytes)"
echo "  yaxis min 0 label : throughput (Mb/s)"
echo

while [ $# != 0 ]; do
  # get input files
  if [ $# -lt 2 ]; then
    usage
  fi
  TITLE=$1
  shift
  TTCPFILE=$1
  shift

  # curve premable
  echo "newcurve"
  echo "  label : $TITLE"
  echo "  marktype `nth \"$MARKTYPES\" $linetype`"
  echo "  linetype `nth \"$LINETYPES\" $linetype`"
  echo "  fill `nth \"$FILLTYPES\" $linetype`"
  echo -n "  pts "

  # output the points
  $CRUNCH < $TTCPFILE | $HIST | sort -n | awk '{ printf ("%s %s ",$1,$4); }'
  echo
  echo

  linetype=`expr 1 + $linetype`
done

rm -f $TMPFILE