#!/usr/bin/env bash

EXECUTOR_NUMBER="$1"
RUN_NAME="$2"

BENCHMARK_BIN_DIR="$HOME/colcon_ws/install/irobot_benchmark/lib/irobot_benchmark"
BENCHMARK_RESULTS_DIR="$HOME/ros_benchmarks/$RUN_NAME"

source /opt/ros/rolling/local_setup.bash
source $HOME/colcon_ws/install/setup.bash

cd "$BENCHMARK_BIN_DIR"
./irobot_benchmark \
   topology/bitbots.json \
   --time 120 \
   --executor $EXECUTOR_NUMBER \
   --ipc on \
   --sampling 500 \
   --late-percentage 25 \
   --late-absolute 5000 \
   --too-late-percentage 100 \
   --too-late-absolute 50000

mkdir -p "$BENCHMARK_RESULTS_DIR/$RUN_NAME"

RUN_RESULTS_DIRNAME="$(basename `ls -d $BENCHMARK_BIN_DIR/*_log`)" 
cp -r "$BENCHMARK_BIN_DIR/$RUN_RESULTS_DIRNAME" "$BENCHMARK_RESULTS_DIR"
rm -r $BENCHMARK_BIN_DIR/*_log

# add linux kernel cmdline textfile to track test run
cat /proc/cmdline > "$BENCHMARK_RESULTS_DIR/cmdline.txt"

# rename to snake_case and move to $RUN_NAME folder
cd "$BENCHMARK_RESULTS_DIR" 
for folder in $(ls -d *_log | grep 'Executor'); do
    mv "$folder" "$(echo "$folder" | sed -r 's/([A-Z])/_\L\1/g' | sed 's/__/_/')"
done
