#!/bin/bash

# 获取脚本所在的绝对路径，确保后续引用都正确
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# 可执行文件和结果根目录都基于 SCRIPT_DIR
EXECUTABLE="${SCRIPT_DIR}/bin/speedtest"
RESULTS_ROOT="${SCRIPT_DIR}/results"

# 测试类型
names=('dc_cond')

# 固定参数
B_VALUES=(-1 0 32)
# 新的 E 参数列表
E_VALUES=(1 28 57 85 114 142 171 199)

# 固定 t
T_VALUE=1024

# 每个 t 启动的第一个 seed
BASE_SEED=137

NUM_SEEDS=5

# 确保结果根目录存在
mkdir -p "$RESULTS_ROOT"

for name in "${names[@]}"; do
  for e in "${E_VALUES[@]}"; do
    # 为当前 E 建一级目录
    outdir="${RESULTS_ROOT}/${name}/E${e}"
    mkdir -p "$outdir"

    echo "=== test=${name}, t=${T_VALUE}, E=${e}: will run seeds ${BASE_SEED}..$((BASE_SEED + NUM_SEEDS - 1)) ==="

    for b in "${B_VALUES[@]}"; do
      for ((i=0; i<NUM_SEEDS; i++)); do
        seed=$((BASE_SEED + i))
        TEST_NAME="${name}_b${b}_e${e}_t${T_VALUE}_s${seed}"
        LOG_FILE="${outdir}/${TEST_NAME}.out"
        MEM_FILE="${outdir}/${TEST_NAME}.mem"

        echo "  -> b=${b}, seed=${seed}"

        # 在子 shell 里切到 outdir，再执行命令
        (
          cd "$outdir"
          /usr/bin/time -v "$EXECUTABLE" "${name}_tbg" "$b" "$e" "$T_VALUE" "$seed"
        ) \
          1> "$LOG_FILE" \
          2> "$MEM_FILE"

      done
    done

    echo
  done
done

echo "All tests completed. Results in ${RESULTS_ROOT}/"
