#!/usr/bin/env bash

set -Eeuo pipefail

shopt -s nullglob

pushd "$(dirname "$0")" >/dev/null

for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
  echo performance | sudo dd status=none of="$i"
done

results_dir="$PWD/results"
input_dir="$PWD/inputs"
iterations=${MHB_ITERATIONS:-25}

mkdir -p "$results_dir"

pushd runners >/dev/null
for r in *; do
  if [[ ! -d "$r" ]] || [[ "$r" == "node_modules" ]]; then
    continue
  fi
  echo "Running $r..."
  pushd "$r" >/dev/null
  out="$results_dir/$r.json"
  if [[ "$(uname -s)" = "Darwin" ]]; then
    MHB_ITERATIONS="$iterations" MHB_INPUT_DIR="$input_dir" RUST_BACKTRACE=1 ./run >"$out"
  else
    sudo --preserve-env=MHB_HTML_ONLY,PATH MHB_ITERATIONS="$iterations" MHB_INPUT_DIR="$input_dir" RUST_BACKTRACE=1 nice -n -20 taskset -c 1 ./run >"$out"
  fi
  popd >/dev/null
done
popd >/dev/null

echo "All done!"

popd >/dev/null
