#!/bin/sh -eu
# ==========================================================================
#         _   _      _   ____            __ __  __      _
#        | \ | | ___| |_|  _ \ ___ _ __ / _|  \/  | ___| |_ ___ _ __
#        |  \| |/ _ \ __| |_) / _ \ '__| |_| |\/| |/ _ \ __/ _ \ '__|
#        | |\  |  __/ |_|  __/  __/ |  |  _| |  | |  __/ ||  __/ |
#        |_| \_|\___|\__|_|   \___|_|  |_| |_|  |_|\___|\__\___|_|
#
#                  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/

NAME="experiment1"
RUNTIME=30
CONTROL="--control-over-tcp"   # E.g.: --control-over-tcp --local=... --controllocal=...
DESTINATIONS="127.0.0.1"
FLOWS="const0:const1024:const0:const1024"
PROTOCOLS="tcp sctp"
OPTION1="value1"
OPTION2="test1"

# ------ Prepare results directory --------------------------------------------------------
mkdir -p "$NAME"
cd "$NAME"
if [ ! -e results.summary ] ; then
   echo "--varnames=Timestamp Destination Protocol Flow Option1 Option2 Directory" >results.summary
fi

for destination in $DESTINATIONS ; do
   for flow in $FLOWS ; do
      for protocol in $PROTOCOLS ; do
         for option1 in $OPTION1 ; do
            for option2 in $OPTION2 ; do

               # ------ Prepare a unique directory for the results ------------------------
               now="$(date -u -Iseconds)"   # <- Unique name by using date
               run="$now:$destination-$flow-$protocol-$option1-$option2"
               directory="run-$(echo "$run" | sha1sum | cut -d' ' -f1)"
               mkdir -p "$directory"
               timestamp="$(getabstime)"   # Current time as Unix timestamp in microseconds

               # Do something, to configure non-NetPerfMeter options
               # something-to-configure-option1 "$option1"
               # something-to-configure-option2 "$option2"
               # ...

               # ------ Run NetPerfMeter --------------------------------------------------
               (
                  cd "$directory"
                  # *******************************************************************
                  # NOTE: Add option `--vector "run.vec.bz2"` only when necessary!
                  # Vectors may use a lot of storage capacity for high-bandwidth flows!
                  # *******************************************************************
                  netperfmeter "[$destination]:9000" \
                     --config "run.config"  \
                     --scalar "run.sca.bz2" \
                     --vector "run.vec.bz2" \
                     --runtime "$RUNTIME"   \
                     ${CONTROL}             \
                     "--$protocol" const0:const1024:const0:const1024
               )

               # ------ Append run to results.summary -------------------------------------
               (
                  echo "--values=$timestamp $destination $protocol $flow $option1 $option2 $directory"
                  echo "--input=$directory/run-active.sca.bz2"
                  echo "--values=$timestamp $destination $protocol $flow $option1 $option2 $directory"
                  echo "--input=$directory/run-passive.sca.bz2"
               ) >>results.summary

            done
         done
      done
   done
done

# ------ Run CreateSummary ----------------------------------------------------------------
createsummary --batch <results.summary
