#!/usr/bin/env bash
#
# Network Performance Meter
# Copyright (C) 2009-2026 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@simula.no
#

# Bash options:
set -e

# Ensure that DCCP module is loaded:
/usr/sbin/modprobe dccp

rm -f ./*.vec ./*.sca

# ====== Start the passive side =============================================
netperfmeter 9000 -nodisplay &
SERVER_PID=$!
sleep 5

cleanup() {
   if [ "${SERVER_PID}" != "" ] ; then
      kill "${SERVER_PID}"
      SERVER_PID=
   fi
}
trap cleanup 0

netperfmeter localhost:9000 \
   -vector=vector.vec \
   -scalar=scalar.sca \
   -tcp  const10:const1452:const5:const1452 \
   -sctp const10:const1452:const5:const1452 \
   -dccp const10:const1452:const5:const1452 \
   -udp  const10:const1452:const5:const1452 \
   -runtime=10 \
   -nodisplay

# ====== Stop the passive side ==============================================
cleanup ${SERVER_PID}

# ====== Check results ======================================================
RESULTS="
   scalar-active.sca
   scalar-passive.sca
   vector-active-00000000-0000.vec
   vector-active-00000001-0000.vec
   vector-active-00000002-0000.vec
   vector-active-00000003-0000.vec
   vector-active.vec
   vector-passive-00000000-0000.vec
   vector-passive-00000001-0000.vec
   vector-passive-00000002-0000.vec
   vector-passive-00000003-0000.vec
   vector-passive.vec
"
for result in ${RESULTS} ; do
   if [ -s "${result}" ] ; then
      ls -l  "${result}"
   else
      echo >&2 "ERROR: ${result} does not exist or is empty!"
      exit 1
   fi
done

echo "Test passed!"
