#!/bin/sh
# See LICENSE file for copyright and license details.

command -v gnuplot >/dev/null || {
	echo gnuplot is not installed 1>&2
	exit 1
}

datafile=`mktemp`
plotfile=`mktemp`
trap "rm -f $datafile $plotfile" EXIT
sbm -c 1 -t > "$datafile"
(
cat << 'EOF'
set multiplot layout 1, 2
set autoscale
set grid
set xlabel "time / s"
set ylabel "Mbps"
datafile=system("echo $DATAFILE")
plot datafile using ($1/1000/1000) with lines title "Rx", \
     datafile using ($2/1000/1000) with lines title "Tx"
set xlabel "time / s"
set ylabel "pps"
plot datafile using ($3) with lines title "Rx", \
     datafile using ($4) with lines title "Tx"
unset multiplot
pause 1
reread
EOF
) > "$plotfile"
DATAFILE="$datafile" gnuplot "$plotfile" &

while :; do
	sbm -c 1 -t >> "$datafile"
done
