#!/usr/bin/env bash
# Source-able environment shim for the two EvoMO Python environments.
#
#   source scripts/env.sh            # env A: evox 2.0 / evoxc / iree / XLA on ROCm
#   source scripts/env.sh --legacy   # env B: torch 2.9.1+rocm6.4 / evox 1.4.1
#
# Run it from the repository root: PYTHONPATH is built from $PWD, so sourcing it
# from another directory points PYTHONPATH at the wrong tree.
# The venv roots can be overridden with EVOMO_ENV_A / EVOMO_ENV_B (the same
# variables scripts/setup_env.sh honours).
#
# This file only exports variables (plus a missing-venv warning): everything
# else -- creating the venvs, the pins, the failure modes -- lives in
# scripts/setup_env.sh and scripts/README.md.

EVOMO_ENV_A="${EVOMO_ENV_A:-/tmp/evomo_env}"
EVOMO_ENV_B="${EVOMO_ENV_B:-/tmp/evomo_env_legacy}"

# Both interpreters, available in either mode (smoke scripts take a python path).
export PYTHON_A="$EVOMO_ENV_A/bin/python"
export PYTHON_LEGACY="$EVOMO_ENV_B/bin/python"

if [ "${1:-}" = "--legacy" ]; then
    EVOMO_ENV_MODE=legacy
    EVOMO_PY="$PYTHON_LEGACY"
    EVOMO_BIN="$EVOMO_ENV_B/bin"

    # Env B is the torch-only baseline: its `evomo` is the FROZEN PyTorch
    # snapshot under benchmarks/legacy/src (a namespace package, no
    # __init__.py), NOT the 1.0 library in ./src -- and the 2.0 stack (local
    # evox/evoxc heads) must NOT leak in, it would import the wrong evox.
    export PYTHONPATH="$PWD/benchmarks/legacy/src"

    # XLA/PJRT is meaningless for torch and an inherited value would make a
    # later evox 2.0 process load a plugin its venv does not have.
    unset EVOXC_PJRT_PLUGIN
    unset XLA_FLAGS
else
    EVOMO_ENV_MODE=new
    EVOMO_PY="$PYTHON_A"
    EVOMO_BIN="$EVOMO_ENV_A/bin"

    # ./src (this checkout) + the local evox 2.0 / evoxc heads.  The two local
    # heads SHADOW the PyPI installs in env A on purpose: they are newer than
    # the published evox 2.0.0a1 / evoxc 0.1.0 wheels.
    export PYTHONPATH="$PWD/src:/root/evox/src:/root/evoxc"

    # PJRT plugin for the xla backend. It ships in the jax-rocm7-pjrt wheel
    # (pinned to 0.10.2: 0.11/0.12 are built against the ROCm pip "sysdeps"
    # libraries, which this host does not have -- see scripts/README.md).
    export EVOXC_PJRT_PLUGIN="$EVOMO_ENV_A/lib/python3.12/site-packages/jax_plugins/xla_rocm7/xla_rocm_plugin.so"

    # HIP graph command buffers SIGSEGV inside PJRT_Executable_Execute with the
    # xla_rocm7 plugin, so they must stay off for every xla run.
    export XLA_FLAGS="--xla_gpu_enable_command_buffer="
fi

# Pin the single MI300X to in-process device id 0: evoxc addresses it as
# "rocm:0" and torch as "cuda:0" under the same remapping.
export HIP_VISIBLE_DEVICES=0
export ROCR_VISIBLE_DEVICES=0

# Put the selected venv first on PATH (uv is not on PATH on this host; it lives
# at /root/.local/bin/uv, see scripts/README.md).
case ":$PATH:" in
    *":$EVOMO_BIN:"*) ;;
    *) export PATH="$EVOMO_BIN:$PATH" ;;
esac

export EVOMO_ENV_MODE

if [ ! -x "$EVOMO_PY" ]; then
    echo "evomo: $EVOMO_ENV_MODE venv is missing ($EVOMO_PY) -- run scripts/setup_env.sh" >&2
fi