#!/bin/bash



do_plot()
{
  fname=$1 ; shift
  ylabel=$1 ; shift
  what_to_plot1=$1 ; shift
  what_to_plot2=$1 ; shift
  clocks=$1 ; shift
  ( cat <<-
      set grid
      set title "Sieve of Eratosthenes Benchmark"
      set logscale x
      set mxtics 10
      set mytics 10
      set xlabel "sieve length in bytes (i.e. the max tested number / 8)"
      set key left Left
      set terminal postscript eps color solid "Helvetica-Bold" 
      set output "$fname"
      set ylabel "$ylabel"
      p \\

    if [ $to = end ] ; then
      echo -n "[:] [0:] "
    else
      echo -n "[:$to] [0:] "
    fi
    ct=0;
    for i in $* ; do 
      freq=$( echo $i | tr 'M-' '\n\n' | tail -2 | head -1 )
      echo -n \"$i\" using '($1):('$what_to_plot1
      if $clocks ; then
        echo -n "*1e-9*($freq*1e6)"
      fi
      echo  ')' title \"$i slow\" with lines, \\
      if [ $ct -eq 4 ] ; then   # to skip yellow color
        echo "log(0) notitle, \\"
      fi
      ct=$(( ct + 1 ))
      echo -n \"$i\" using '($1):('$what_to_plot2
      if $clocks ; then
        echo -n "*1e-9*($freq*1e6)"
      fi
      echo  ')' title \"$i fast\" with lines, \\
      if [ $ct -eq 4 ] ; then   # to skip yellow color
        echo "log(0) notitle, \\"
      fi
      ct=$(( ct + 1 ))
    done 
    echo "log(0) notitle"
    ) | gnuplot
  gv $fname &
  ps2pdf14 -sPAPERSIZE=a4 $fname &
}

to=end
if [ ."$1" = ."-t" ] ; then
  shift
  to=$1
  shift
fi

do_plot 2sieve_iter_times.eps "average inner loop step runtime [ns]" '1e3*$2/$4' '1e3*$3/$4' false $*
# do_plot 2average_runtime.eps "runtime per sieve length [ns]" '1e3*$2/($1*8)' '1e3*$3/($1*8)' false $*
do_plot 2sieve_iter_cycles.eps "avg # of CPU cycles of a single inner loop step" '1e3*$2/$4' '1e3*$3/$4' true $*
