#!/bin/bash

. lib.sh

COUNT=2500

wait_for_key() {
    [ "$INTERACTIVE" ] && read -p "Press a key to continue" || :
}

main() {
    if ! [ "$INTERACTIVE" ]; then echo "Skipping interactive test $0"; SKIPPED=1; return; fi

    sshgw cangw -A -s can0 -d can1
    latester -d can0 -d can1 -d can2 -c $COUNT $(traffic_and_length 2) -n len2
    wait_for_key
    latester -d can0 -d can1 -d can2 -c $COUNT $(traffic_and_length 4) -n len4
    wait_for_key
    latester -d can0 -d can1 -d can2 -c $COUNT $(traffic_and_length 6) -n len6
    wait_for_key
    latester -d can0 -d can1 -d can2 -c $COUNT $(traffic_and_length 8) -n len8
}

ADDITIONAL_PLOTS=detail

plot_cmds() {
    if [[ "$1" = detail ]]; then
	range=0.06:0.4
	echo "set logscale y"
    else
	range=0.05:100
	echo "set logscale xy"
    fi
    cat <<EOF
set title "Measurment precission: PC versus CANalyzer"
set termoption dashed
set grid xtics mxtics ytics mytics lw 1, lw 0.5
set xlabel "Total latency [ms]"
set ylabel "Latency profile [frames]"
plot [$range] [1:$COUNT] \\
          "len2-hist.txt" with lp lc 1 lt 1 title "PC: 2 byte messages", \\
          "len4-hist.txt" with lp lc 2 lt 1 title "PC: 4 byte messages", \\
          "len6-hist.txt" with lp lc 3 lt 1 title "PC: 6 byte messages", \\
          "len8-hist.txt" with lp lc 4 lt 1 title "PC: 8 byte messages", \\
          "canalyzer-2-hist.txt" with lp lc 1 lt 2 title "CANalyzer: 2 byte messages", \\
          "canalyzer-4-hist.txt" with lp lc 2 lt 2 title "CANalyzer: 4 byte messages", \\
          "canalyzer-6-hist.txt" with lp lc 3 lt 2 title "CANalyzer: 6 byte messages", \\
          "canalyzer-8-hist.txt" with lp lc 4 lt 2 title "CANalyzer: 8 byte messages"
EOF
}
    
test_end