#!/usr/bin/env bash
# ==========================================================================
#         _   _      _   ____            __ __  __      _
#        | \ | | ___| |_|  _ \ ___ _ __ / _|  \/  | ___| |_ ___ _ __
#        |  \| |/ _ \ __| |_) / _ \ '__| |_| |\/| |/ _ \ __/ _ \ '__|
#        | |\  |  __/ |_|  __/  __/ |  |  _| |  | |  __/ ||  __/ |
#        |_| \_|\___|\__|_|   \___|_|  |_| |_|  |_|\___|\__\___|_|
#
#                  NetPerfMeter -- 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
# Homepage: https://www.nntb.no/~dreibh/netperfmeter/

# Bash options:
set -eu


TCP_FLOWS=5
TCP_PARAMS="const0:const0:const0:const1460:cmt=off"

UDP_FLOWS=5
UDP_PARAMS="const0:const0:const25:const2500:onoff=+pareto0.166667,1.5,+pareto0.166667,1.5,repeat"


# ====== Handle arguments ===================================================
if [ $# -lt 1 ] ; then
   echo >&2 "Usage: $0 server:port [runtime]"
   exit 1
fi
SERVER="$(echo "$1" | sed -s 's/:.*$//g')"
PORT="$(echo "$1" | sed -s 's/^.*://g')"
if [[ ! "${PORT}" =~ ^[0-9]+$ ]] ; then
   echo >&2 "ERROR: Invalid port ${PORT}!"
   exit 1
fi
RUNTIME=10
if [ $# -ge 2 ] ; then
   RUNTIME="$2"
fi
if [[ ! "${RUNTIME}" =~ ^[0-9]+$ ]] ; then
   echo >&2 "ERROR: Invalid runtime ${RUNTIME}!"
   exit 1
fi

make
rm -f vgcore.* core.* core


# ====== Run NetPerfMeter ===================================================
options=""
i=0 ; while [ $i -lt $TCP_FLOWS ] ; do
   options="${options} -tcp ${TCP_PARAMS}"
   i=$((i+1))
done
i=0 ; while [ $i -lt $UDP_FLOWS ] ; do
   options="${options} -udp ${UDP_PARAMS}"
   i=$((i+1))
done
echo "OPTIONS=${options}"

# shellcheck disable=SC2086
./netperfmeter "${SERVER}:${PORT}" \
   -config=many.config -vector=many.vec -scalar=many.sca \
   ${options} \
   -runtime="${RUNTIME}"
