#!/usr/bin/env bash
# SuperLocalMemory V3 CLI
# Part of Qualixar | https://superlocalmemory.com

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PKG_ROOT="$(dirname "$SCRIPT_DIR")"
SRC_DIR="$PKG_ROOT/src"

# Handle --version / -v directly (fast path)
for arg in "$@"; do
    if [ "$arg" = "--version" ] || [ "$arg" = "-v" ]; then
        VER=$(grep '"version"' "$PKG_ROOT/package.json" 2>/dev/null | head -1 | sed 's/.*"\([0-9][0-9.]*\)".*/\1/')
        echo "superlocalmemory ${VER:-unknown}"
        exit 0
    fi
done

# LLD-06 §6.3 — prefer the PyInstaller-built binary on the hot hook path.
# If the onedir binary exists and isn't disabled, run it directly; the
# binary is stdlib-only and ~30ms cold on Windows. Otherwise fall back
# to the Python CLI below.
SLM_HOOK_BIN="${SLM_HOOK_BINARY:-$HOME/.superlocalmemory/bin/slm-hook/slm-hook}"
if [ "${1:-}" = "hook" ] && [ "${2:-}" = "user_prompt_submit" ] \
   && [ -x "$SLM_HOOK_BIN" ] \
   && [ "${SLM_HOOK_BINARY_DISABLED:-0}" != "1" ]; then
    exec "$SLM_HOOK_BIN"
fi

# Find Python
PYTHON=""
for cmd in python3 python; do
    if command -v "$cmd" &>/dev/null; then
        PYTHON="$cmd"
        break
    fi
done

if [ -z "$PYTHON" ]; then
    echo "Error: Python 3 not found. Install Python 3.11+ from python.org"
    exit 1
fi

# Set PYTHONPATH so Python finds the npm package's src/ directory
if [ -n "$PYTHONPATH" ]; then
    export PYTHONPATH="$SRC_DIR:$PYTHONPATH"
else
    export PYTHONPATH="$SRC_DIR"
fi

# Prevent PyTorch Metal/MPS GPU memory reservation
export PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0
export PYTORCH_MPS_MEM_LIMIT=0
export PYTORCH_ENABLE_MPS_FALLBACK=1
export TOKENIZERS_PARALLELISM=false
export TORCH_DEVICE=cpu
export CUDA_VISIBLE_DEVICES=""

# Run V3 CLI
exec "$PYTHON" -m superlocalmemory.cli.main "$@"
