#!/usr/bin/env bash
# Agent-facing wrapper for the pinned rtplan-matrad env + matRad checkout.
# Usage:
#   software/run_matrad.sh                 # interactive octave shell with matRad on path
#   software/run_matrad.sh --eval 'stmt'   # run Octave statement non-interactively
#   software/run_matrad.sh path/to/script.m
#   software/run_matrad.sh python ...      # arbitrary command inside the pinned env
set -euo pipefail

ENV_NAME="rtplan-matrad"
MAMBA_ROOT_PREFIX="${HOME}/.local/share/micromamba"
MATRAD_PREFIX="/opt/matrad-c014dc82"
MAMBA_BIN="${HOME}/.local/bin/micromamba"

export MAMBA_ROOT_PREFIX

if [ "${1:-}" = "python" ] || [ "${1:-}" = "--python" ]; then
  shift
  exec "${MAMBA_BIN}" run -n "${ENV_NAME}" python "$@"
fi

OCTAVE_ARGS=("--no-gui" "--path" "${MATRAD_PREFIX}")
if [ "${1:-}" = "--eval" ]; then
  OCTAVE_ARGS+=("--eval" "${2:?usage: run_matrad.sh --eval 'statement'}")
  shift 2 || true
elif [ "${1:-}" = "--script" ] || [[ "${1:-}" == *.m ]]; then
  if [ "${1:-}" = "--script" ]; then shift; fi
  OCTAVE_ARGS+=("$1")
  shift || true
fi

exec "${MAMBA_BIN}" run -n "${ENV_NAME}" octave "${OCTAVE_ARGS[@]}" "$@"
