#!/bin/sh

if [ -z $R ] ; then
R=000
fi

if [ -z $MBIT ] ; then
	MBIT="0.6Mbit stream"
fi

T="pngcairo size 1000,600 enhanced font \"LiberationSerif,20\""
EXT=png
#T="pdfcairo"
#EXT=pdf

if [ -z "$FOLDERS" ] ; then
  FOLDERS="wowza nginx mist adobe"
fi

if [ -z "$NAMES" ] ; then
  NAMES="Wowza Nginx Mist Adobe"
fi

FLDS=( $FOLDERS )
NAMS=( $NAMES )

FN=`ls ${FLDS[0]}/run${R}_*.csv 2> /dev/null`
if [ -z $FN ] ; then
  exit
fi
FILE=`basename $FN`

FN=`ls ${FLDS[0]}/run${R}_*.times 2> /dev/null`
TIMES=`basename $FN`
VIEWERS=`echo $FILE | cut -d '_' -f 2`
TECH=`echo $FILE | cut -d '_' -f 3`
SECS=`echo $FILE | cut -d '.' -f 1 | cut -d '_' -f 4`
TIME=$(( $SECS / 60 ))m

FILES=()
TIME_FN=()

for i in `seq $(( ${#FLDS[@]} - 1 )) -1 0`; do
  FN=`ls ${FLDS[$i]}/run${R}_*.csv 2> /dev/null`
  if [ -z $FN ] ; then
    exit
  fi
  FILES[$i]="${FLDS[$i]}/`basename $FN`"
  FN=`ls ${FLDS[$i]}/run${R}_*.times 2> /dev/null`
  TIME_FN[$i]="${FLDS[$i]}/`basename $FN`"
done
#   FN=`ls ${FLDS[0]}/run${R}_*.times 2> /dev/null`
#   VNAME="TIMES_${i}"
#   ${!VNAME}=`basename $FN`
#   VNAME="VIEWERS_${i}"
#   ${!VNAME}=`echo $FILE | cut -d '_' -f 2`
#   VNAME="TECH_${i}"
#   ${!VNAME}=`echo $FILE | cut -d '_' -f 3`
#   SECS=`echo $FILE | cut -d '.' -f 1 | cut -d '_' -f 4`
#   VNAME="TIME_${i}"
#   ${!VNAME}=$(( $SECS / 60 ))m
# 
# done

COMMON_DATA="${TIME}, ${MBIT}, ${VIEWERS} ${TECH}"
COMMON_FILENAME="${TECH}_${VIEWERS}_${TIME}"

PLOT1=""
PLOT2=""
PLOT3=""
PLOT4=""
PLOT5=""
PLOT6=""
PLOT7=""
for i in `seq $(( ${#FLDS[@]} - 1 )) -1 0`; do
  if [ -n "${PLOT1}" ] ; then
    PLOT1="${PLOT1}, "
    PLOT2="${PLOT2}, "
    PLOT3="${PLOT3}, "
    PLOT4="${PLOT4}, "
    PLOT5="${PLOT5}, "
    PLOT6="${PLOT6}, "
    PLOT7="${PLOT7}, "
  fi
  
  sort --field-separator=',' --key=4g < "${TIME_FN[$i]}" > "${TIME_FN[$i]}_sorted"
  VC=`cut -f 4 -d ',' < "${TIME_FN[$i]}" | awk "(int(\\\$1) >= ${SECS}000-5000) {sum++} END{print sum}"`
  SC=`cut -f 4 -d ',' < "${TIME_FN[$i]}" | awk "(int(\\\$1) >= ${SECS}000-30000) {sum++} END{print sum}"`
  
#smooth CPU and network
  PLOT6="${PLOT6}'${FLDS[$i]}/${FILE}' using 1:4:(1) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})' smooth acsplines"
  PLOT7="${PLOT7}'${FLDS[$i]}/${FILE}' using (\$1-dx):(d(\$1,\$5/131072.0)):(1) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'  smooth acsplines"
  
#actual CPU and network
  PLOT1="${PLOT1}'${FILES[$i]}' using 1:4 with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'"
  PLOT4="${PLOT4}'${FILES[$i]}' using (\$1-dx):(d(\$1,\$5/131072.0)) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'"

#memory - no need for smoothing, it's already pretty smooth
  PLOT2="${PLOT2}'${FILES[$i]}' using 1:(\$3/1024.0) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'"
#total upload - same here, no need to smooth anything over
  PLOT3="${PLOT3}'${FILES[$i]}' using 1:(\$5/134217728.0) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'"
#and buffer times. Smoothing makes no sense here. Don't.
  PLOT5="${PLOT5}'${TIME_FN[$i]}_sorted' using (\$4-${SECS}000)/1000 with linespoints lw 1 title '${NAMS[$i]} (${VC}, ${SC})'"
done

gnuplot << EOF
set terminal ${T}
set datafile separator ","
set key on under center

#set style fill solid 0.25 border -1
#set style boxplot outliers pointtype 7
#set style data boxplot
#set boxwidth  0.5
set pointsize 0.2
set arrow from 0,-5 to ${VIEWERS}-1,-5 nohead

set title "Available buffer time, ${COMMON_DATA} clients"
set output '${TECH}_${VIEWERS}_${TIME}_buffers.${EXT}'
set format y "%gs"
set ylabel "Buffer size (above line = success)"
set xlabel "Each point represents a single connection"
unset xtics
plot ${PLOT5}
unset style
unset xlabel
unset arrow

set timefmt "%s"
set xtics
set xdata time
#set xlabel "Time (mm:ss)"
set format x "%M:%S"
set grid

set title "CPU usage, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_cpu.${EXT}'
set yrange [0:*<100]
set format y "%.0f %%"
set ylabel "CPU use"
plot ${PLOT1}

set title "Memory usage, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_mem.${EXT}'
set yrange [0<*<0:0<*]
set ytics auto
set format y "%g MiB"
set ylabel "Memory use"
plot ${PLOT2}

set title "Network upload, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_upload.${EXT}'
set yrange [0<*<0:0<*]
set ytics auto
set format y "%g Gbit"
set ylabel "Total uploaded data"
plot ${PLOT3}

# derivative function.  Return 1/0 for first point, otherwise delta y or (delta y)/(delta x)
d(x,y) = (\$0 == 1) ? (x1 = x, y1 = y, 1/0) : (x2 = x1, x1 = x, y2 = y1, y1 = y, (y1-y2)/(x1-x2))

set title "Network speed, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_netspeed.${EXT}'
set yrange [0<*<0:0<*]
set ytics auto
set format y "%g Mbps"
set ylabel "Upload speed"
dx=0.5
plot ${PLOT4}

set title "Smoothed CPU usage, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_cpu_smooth.${EXT}'
set yrange [0:*<100]
set format y "%.0f %%"
set ylabel "CPU use"
plot ${PLOT6}

set title "Smoothed network speed, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_netspeed_smooth.${EXT}'
set yrange [0<*<0:0<*]
set ytics auto
set format y "%g Mbps"
set ylabel "Upload speed"
dx=0.5
plot ${PLOT7}

EOF


for i in `seq $(( ${#FLDS[@]} - 1 )) -1 0`; do
  rm -f "${FLDS[$i]}/${TIMES}_sorted"
done