#!/bin/sh -eu
# ==========================================================================
#         _   _      _   ____            __ __  __      _
#        | \ | | ___| |_|  _ \ ___ _ __ / _|  \/  | ___| |_ ___ _ __
#        |  \| |/ _ \ __| |_) / _ \ '__| |_| |\/| |/ _ \ __/ _ \ '__|
#        | |\  |  __/ |_|  __/  __/ |  |  _| |  | |  __/ ||  __/ |
#        |_| \_|\___|\__|_|   \___|_|  |_| |_|  |_|\___|\__\___|_|
#
#                  NetPerfMeter -- Network Performance Meter
#                 Copyright (C) 2009-2025 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=3
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=Destination Protocol 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"

               # ------ Prepare name/patterns for results files ---------------------------
               scalarPattern="$directory/run.sca.bz2"
               vectorPattern="$directory/run.vec.bz2"
               configName="$directory/run.config"

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

               # ------ Run NetPerfMeter --------------------------------------------------
               # NOTE: Add -vector="$vectorPattern", if needed
               netperfmeter "[$destination]:9000" \
                  -scalar="$scalarPattern" \
                  -config="$configName" \
                  -runtime=$RUNTIME \
                  "-$protocol" const0:const1024:const0:const1024

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

            done
         done
      done
   done
done

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