#!/usr/bin/env bash

set -euo pipefail

IMAGE="mamba-ar-docker"
RUN_ID="${RUN_ID:-$(date +%Y%m%d-%H%M%S)}"   # override if you want
NAME="mamba-ar-run-$RUN_ID"
RUN_DIR="$HOME/projects/mamba-ar/runs/$RUN_ID"

# Editable CPU usage limit (85% of total cores by default)
CPU_PERCENT=0.85
TOTAL_CPUS=$(nproc)
CPU_LIMIT=$(awk -v c="$TOTAL_CPUS" -v p="$CPU_PERCENT" 'BEGIN {print c * p}')

mkdir -p "$RUN_DIR"

docker run -d --rm \
  --gpus "${GPU:-all}" \
  --cpus "$CPU_LIMIT" \
  --name "$NAME" \
  -v "$HOME/projects/mamba-ar:/workspace" \
  --workdir /workspace \
  --entrypoint bash \
  "$IMAGE" -lc 'cd scripts && python section_5__scaling_laws__axes_runs.py 2>&1'

echo
echo "Commands shortcuts:"
echo "docker logs -f $NAME  # view logs"
echo "docker kill $NAME  # stop job"

echo
echo "viewing logs; press ctrl+C anytime to exit (does not interrupt run)"

# view logs
docker logs -f $NAME