#!/bin/bash

script_dir=$(dirname $(realpath $0))

usage() {
  echo "Usage: $0  |  " 1>&2;
  exit 1;
}
usage() {
  cat << ENDUSAGE
Usage: $0 [-I if] SERVER | TCPDUMP [TRACE_OPTS]
   SERVER: [-bt] [(-x | -X) POLL] [-g INTERVAL]
   TCPDUMP: -d NB_PACKETS [-i INTERVAL]
   TRACE_OPTS: (-T LATENCY_THRESHOLD -G) | -E LATENCY_THRESHOLD
ENDUSAGE
1>&2;

  exit 1;
}

# Default options
interface="eth0"
server_options="-a -p 99"
make_opts=""
ip="10.100.21."
tcpdump_interval=1000000

while getopts "b:htx:X:d:i:g:I:T:GE:" opt; do
  case "${opt}" in
    b )
        use_rtt=1
        board_name=${OPTARG}
        ;;
    d )
        use_tcpdump=1
        nb_packets=${OPTARG}
        ;;
    i )
        tcpdump_interval=${OPTARG}
        ;;
    g )
        interval=${OPTARG}
        server_options+=" -g -i $interval"
        use_histogram=1
        ;;
    h )
        usage
        exit 1
        ;;
    t )
        server_options+=" -t"
        ;;
    x )
        server_options+=" -x ${OPTARG}"
	make_opts=" -e WITH_XDP=1"
        ;;
    I )
        interface+="${OPTARG}"
      ;;
    X )
        server_options+=" -x ${OPTARG}"
	make_opts=" -e WITH_GIT_XDP=1"
        ;;
    T )
        server_options+=" -T ${OPTARG}"
      ;;
    G )
        server_options+=" -G"
      ;;
    E )
        enable_xdp_events=1
        server_options+=" -T ${OPTARG}"
      ;;
    * )
        usage
      ;;
  esac
done

shift $((OPTIND-1))

server_options+=" -f $interface"

if [ -n "${use_rtt}" ]; then
  if [ $board_name != "emerald" ] && [ $board_name != "onyx" ] && [ $board_name != "slate" ]; then
   usage
  fi 
  board_ip=$(cat /etc/hosts | grep $board_name | awk '{print $1}')
  server_options+=" -b $board_ip"
fi

if [ -z "${use_histogram}" ]; then
  server_options+=" -v"
else
  output="server_i${interval}"
fi

if [ -n "${use_tcpdump}" ]; then

  echo "tcpdump -c $nb_packets -i $interface -w tmp.pcap -tt --time-stamp-precision=nano udp port 50000";
  tcpdump -c $nb_packets -i $interface -w server_stats_tmp.pcap -tt --time-stamp-precision=nano udp port 50000;

  echo "tshark -r tmp.pcap --disable-protocol dcp-etsi --disable-protocol dcp-pft -t e -E separator=, -T fields -e frame.number -e frame.time_epoch -e data.data > tmp.out";
  tshark -r server_stats_tmp.pcap --disable-protocol dcp-etsi --disable-protocol dcp-pft -t e -E separator=, -T fields -e frame.number -e frame.time_epoch -e data.data > server_stats_tmp.out;

  echo "txtime-stats.py -f server_pcap_stats -i $tcpdump_interval";
  $script_dir/txtime-stats.py -f server_stats_tmp.out -i $tcpdump_interval;
else

  echo "make $make_opts server";
  cd $script_dir/../packet-exchange/build;make $make_opts server;cd $script_dir

  if [ -n "${enable_xdp_events}" ]; then
    tracecmd_events="-e irq -e sched -e xdp"
    echo "trace-cmd record $tracecmd_events $script_dir/../packet-exchange/build/server $server_options";
    trace-cmd record $tracecmd_events $script_dir/../packet-exchange/build/server $server_options;
  elif [ -z "${use_histogram}" ]; then
    echo "server $server_options";
    $script_dir/../packet-exchange/build/server $server_options;
  else
    echo "server $server_options > $output; mv $output ~/";
    $script_dir/../packet-exchange/build/server $server_options > $output;
    mv $output ~/;
  fi
fi