#!/usr/bin/env bash
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUT="$(mktemp)"

TARGET="${ATLAS_OPTIMIZE_TARGET:-$1}"
export ATLAS_EVAL_SEED="${ATLAS_EVAL_SEED:-0}"

START=$(date +%s.%N)
( python3 "$TARGET" ) >"$OUT" 2>&1
RC=$?
END=$(date +%s.%N)

if [ "$RC" -ne 0 ]; then
  echo "[benchmark] run command exited $RC — candidate failed" >&2
  tail -n 40 "$OUT" >&2
  exit "$RC"
fi

python3 "$HERE/score.py" --stdout "$OUT" --elapsed "$(awk "BEGIN { print $END - $START }")"
