#!/bin/bash
# Phase1 + cross eval for the 3 A+C combo seeds.
set -e
ROOT=/home/chy/mambafortrafficmodeling
MIXED_PHASE1=${ROOT}/Mixed_CFM/eval_phase1.py
MIXED_CROSS=${ROOT}/Mixed_CFM/eval_cross.py
CROSS_DIR=${ROOT}/artifacts/route_comparison/cross
mkdir -p ${CROSS_DIR}

# GPU 0: phase1 + cross→IDS2017 for all 3 seeds
{
for seed in 42 43 44; do
  md=${ROOT}/artifacts/route_comparison/route_ac_combo_ciciot2023_seed${seed}
  [ -f "${md}/model.pt" ] || { echo "[wait] seed${seed} model.pt not yet"; continue; }

  if [ ! -f "${md}/phase1_summary.json" ]; then
    echo "[gpu0 phase1] seed${seed}"
    cd ${ROOT}/Mixed_CFM
    CUDA_VISIBLE_DEVICES=0 stdbuf -oL uv run --no-sync python -u ${MIXED_PHASE1} \
      --model-dir ${md} --out-dir ${md} \
      --batch-size 256 --n-steps 16 \
      --n-val-cap 5000 --n-atk-cap 10000 \
      > ${md}/phase1.log 2>&1
  fi

  ids_out=${CROSS_DIR}/route_ac_combo_seed${seed}_to_cicids2017.json
  if [ ! -f "${ids_out}" ]; then
    echo "[gpu0 cross→ids2017] seed${seed}"
    cd ${ROOT}/Mixed_CFM
    CUDA_VISIBLE_DEVICES=0 stdbuf -oL uv run --no-sync python -u ${MIXED_CROSS} \
      --model-dir ${md} \
      --target-store ${ROOT}/datasets/cicids2017/processed/full_store \
      --target-flows ${ROOT}/datasets/cicids2017/processed/flows.parquet \
      --target-flow-features ${ROOT}/datasets/cicids2017/processed/flow_features.parquet \
      --out ${ids_out} \
      --n-benign 10000 --n-attack 10000 --seed 42 --T 64 --batch-size 256 --n-steps 16 \
      > ${CROSS_DIR}/route_ac_combo_seed${seed}_to_cicids2017.log 2>&1
  fi
done
echo "[gpu0 done]"
} > /tmp/ac_eval_gpu0.log 2>&1 &
GPU0=$!

# GPU 1: cross→DDoS19 for all 3 seeds
{
for seed in 42 43 44; do
  md=${ROOT}/artifacts/route_comparison/route_ac_combo_ciciot2023_seed${seed}
  [ -f "${md}/model.pt" ] || { echo "[wait] seed${seed} model.pt not yet"; continue; }

  ddos_out=${CROSS_DIR}/route_ac_combo_seed${seed}_to_cicddos2019.json
  if [ ! -f "${ddos_out}" ]; then
    echo "[gpu1 cross→ddos19] seed${seed}"
    cd ${ROOT}/Mixed_CFM
    CUDA_VISIBLE_DEVICES=1 stdbuf -oL uv run --no-sync python -u ${MIXED_CROSS} \
      --model-dir ${md} \
      --target-store ${ROOT}/datasets/cicddos2019/processed/full_store \
      --target-flows ${ROOT}/datasets/cicddos2019/processed/flows.parquet \
      --target-flow-features ${ROOT}/datasets/cicddos2019/processed/flow_features.parquet \
      --out ${ddos_out} \
      --n-benign 10000 --n-attack 10000 --seed 42 --T 64 --batch-size 256 --n-steps 16 \
      > ${CROSS_DIR}/route_ac_combo_seed${seed}_to_cicddos2019.log 2>&1
  fi
done
echo "[gpu1 done]"
} > /tmp/ac_eval_gpu1.log 2>&1 &
GPU1=$!

wait $GPU0
wait $GPU1
echo "[all ac combo evals done]"
