#!/bin/bash

# omarchy:summary=Run open models on this machine for your agents
# omarchy:args=<use|list|status|start|stop|remove> [args]
# omarchy:examples=omarchy agent local use best | omarchy agent local use qwen3.8-27b-gufo | omarchy agent local stop | omarchy-agent-local use --help

# The engine and its recipes come in omarchy-local-ai, on every machine so the
# agents panel knows which models fit. Podman, which runs them, only comes when
# a model is first set up, from the terminal that setup runs in. An NVIDIA GPU
# reaches the containers through the CDI spec nvidia-container-toolkit writes
# on install and again at every boot.
if omarchy-cmd-missing omarchy-local-ai; then
  if [[ ${1:-} == "refresh" && ${2:-} == "--quiet" ]]; then
    exit 0
  else
    echo "Local models require omarchy-local-ai, which has no Fedora package yet." >&2
    exit 1
  fi
fi

if [[ ${1:-} == "use" ]]; then
  packages=(podman crun)
  if omarchy-hw-nvidia; then
    packages+=(nvidia-container-toolkit)
  fi
  omarchy-pkg-add "${packages[@]}" || exit 1
fi

exec omarchy-local-ai "$@"
