#!/bin/sh
#
# Network Performance Meter
# Copyright (C) 2009-2022 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: thomas.dreibholz@gmail.com
#

if [ $# -lt 1 ] ; then
   echo >&2 "Usage: $0 [Config File] {-ownfile} {-colour|-color|-greyscale|-grayscale|-blackandwhite}"
   exit 1
fi

# ====== Read configuration from config file ================================
CONFIG_FILE="$1"
NUM_FLOWS=0
VECTOR_ACTIVE_NODE=""
VECTOR_PASSIVE_NODE=""
if [ ! -e "$CONFIG_FILE" ] ; then
   echo >&2 "ERROR: Config file $CONFIG_FILE not found!"
   exit 1
fi
. $CONFIG_FILE
shift

if [ "$VECTOR_PASSIVE_NODE" = "" -o "$VECTOR_ACTIVE_NODE" = "" -o $NUM_FLOWS -lt 1 ] ; then
   echo >&2 "ERROR: Bad vector file configuration!"
   exit 1
fi
if [ ! -e "$VECTOR_ACTIVE_NODE" ] ; then
   echo >&2 "ERROR: Vector file $VECTOR_ACTIVE_NODE of active node not found!"
   exit 1
fi
if [ ! -e "$VECTOR_PASSIVE_NODE" ] ; then
   echo >&2 "ERROR: Vector file $VECTOR_ACTIVE_NODE of passive node not found!"
   exit 1
fi

OUTPUT_PREFIX="`echo "$CONFIG_FILE" | sed -e "s/.config$//g"`"

echo "Configuration:"
echo "   - Input of Active Node ($NAME_ACTIVE_NODE) -> $VECTOR_ACTIVE_NODE"
echo "   - Input of Passive Node ($NAME_PASSIVE_NODE) -> $VECTOR_PASSIVE_NODE"
echo "   - Output prefix -> $OUTPUT_PREFIX"


# ====== Get options ========================================================
PLOT_COLOR_MODE="cmColor"
PLOT_OWN_FILE="FALSE"
while [ $# -gt 0 ] ; do
   if [ "$1" = "-ownfile" ] ; then
      PLOT_OWN_FILE="TRUE"
   elif [ "$1" = "-colour" -o "$1" = "-color" ] ; then
      PLOT_COLOR_MODE="cmColor"
   elif [ "$1" = "-greyscale" -o "$1" = "-grayscale" ] ; then
      PLOT_COLOR_MODE="cmGrayScale"
   elif [ "$1" = "-blackandwhite" ] ; then
      PLOT_COLOR_MODE="cmBlackAndWhite"
   else
      echo >&2 "ERROR: Invalid option $1!"
      exit 1
   fi
   shift
done


# ====== Create combined data files =========================================
SEARCH_PATHS=". /usr/local/bin /usr/bin"
COMBINESUMMARIES="combinesummaries"
for searchPath in $SEARCH_PATHS ; do
   if [ -e "$searchPath/$COMBINESUMMARIES" ] ; then
      COMBINESUMMARIES="$searchPath/combinesummaries"
      break
   fi
done

SUMMARY_NAME="$OUTPUT_PREFIX-summary.data.bz2"
(
   echo "--values=\"\"$NAME_ACTIVE_NODE\" 1\""
   echo "--input=$VECTOR_ACTIVE_NODE"
   echo "--values=\"\"$NAME_PASSIVE_NODE\" 0\""
   echo "--input=$VECTOR_PASSIVE_NODE"
) | $COMBINESUMMARIES $SUMMARY_NAME "NodeName IsActive" -quiet

FLOW_SUMMARY_NAME="$OUTPUT_PREFIX-flows.data.bz2"
(
   i=0
   while [ $i -lt $NUM_FLOWS ] ; do
      flowDescriptionVar="\$FLOW$i""_DESCRIPTION"
      activeVectorVar="\$FLOW$i""_VECTOR_ACTIVE_NODE"
      passiveVectorVar="\$FLOW$i""_VECTOR_PASSIVE_NODE"
      eval flowDescription=$flowDescriptionVar
      eval activeVector=$activeVectorVar
      eval passiveVector=$passiveVectorVar

      echo "--values=\"\"$flowDescription\" $i \"$NAME_ACTIVE_NODE\" 1\""
      echo "--input=$activeVector"
      echo "--values=\"\"$flowDescription\" $i \"$NAME_PASSIVE_NODE\" 0\""
      echo "--input=$passiveVector"
      i=$(($i+1))
   done
) | $COMBINESUMMARIES $FLOW_SUMMARY_NAME "FlowName FlowID NodeName IsActive" -quiet


# ====== Locate plotting scripts ============================================
SEARCH_PATHS=". /usr/share/netperfmeter /usr/local/share/netperfmeter"
PLOT_PROGRAM_DIR=""
PLOT_PROGRAM_SCRIPT="plot-netperfmeter-results.R"
for searchPath in $SEARCH_PATHS ; do
   if [ -e "$searchPath/$PLOT_PROGRAM_SCRIPT" -a -e "$searchPath/plotter.R" ] ; then
      PLOT_PROGRAM_DIR=$searchPath
      break
   fi
done
if [ "x$PLOT_PROGRAM_DIR" = "x" ] ; then
   echo >&2 "ERROR: Cannot find $PLOT_PROGRAM_SCRIPT and plotter.R!"
   exit 1
elif [ "$PLOT_PROGRAM_DIR" = "." ] ; then
   echo "   - NOTE: Using $PLOT_PROGRAM_SCRIPT and plotter.R from $searchPath!"
fi


# ====== Call the R script for plotting =====================================
R CMD BATCH --slave --vanilla \
   "--args
      programDirectory=\"$PLOT_PROGRAM_DIR\"
      configFile=\"$CONFIG_FILE\"
      summaryFile=\"$SUMMARY_NAME\"
      flowSummaryFile=\"$FLOW_SUMMARY_NAME\"
      pdfFilePrefix=\"$OUTPUT_PREFIX\"
      plotColorMode=$PLOT_COLOR_MODE
      plotOwnFile=$PLOT_OWN_FILE" \
   $PLOT_PROGRAM_DIR/$PLOT_PROGRAM_SCRIPT /dev/stdout


# ====== Remove temporary files =============================================
echo "Cleaning up"
# rm -f $SUMMARY_NAME $FLOW_SUMMARY_NAME
