#!/bin/bash

# omarchy:summary=Set and launch the default coding agent
# omarchy:args=[pi|omp|opencode|ori|claude|codex|grok|openclaw|agy|hermes|copilot|crush|cursor-agent|muse]
# omarchy:examples=omarchy default agent | omarchy default agent codex | omarchy default agent claude

installing=false
if [[ ${1:-} == "--install" ]]; then
  installing=true
  shift
fi

agent_file="$HOME/.config/omarchy/defaults/agent"

if (($# == 0)); then
  if [[ -f $agent_file ]]; then
    read -r agent <"$agent_file"
  fi

  # Silent when unset rather than defaulting: Omarchy picks no agent for you, so
  # the menu leaves every entry unchecked until one is chosen.
  [[ -n ${agent:-} ]] && echo "$agent"
  exit 0
fi

case "$1" in
pi) agent="pi"; name="Pi" ;;
omp | oh-my-pi) agent="omp"; name="Oh My Pi"; agent_package="github:can1357/oh-my-pi" ;;
opencode | open-code) agent="opencode"; name="OpenCode" ;;
ori | openrouter) agent="ori"; name="Ori"; agent_package="github:OpenRouterLabs/ori-releases" ;;
claude | claude-code) agent="claude"; name="Claude Code" ;;
codex) agent="codex"; name="Codex" ;;
crush) agent="crush"; name="Crush" ;;
grok) agent="grok"; name="Grok"; agent_package="npm:@xai-official/grok" ;;
openclaw) agent="openclaw"; name="OpenClaw"; agent_installer="omarchy-install-openclaw-cli" ;;
agy | antigravity | antigravity-cli | gemini | gemini-cli) agent="agy"; name="Antigravity"; agent_package="antigravity-cli" ;;
hermes) agent="hermes"; name="Hermes"; agent_installer="omarchy-install-hermes-cli" ;;
copilot | github-copilot) agent="copilot"; name="GitHub Copilot" ;;
muse | muse-code | musecode)
  agent="muse"; name="Muse Code"
  # Meta's launcher verifies and updates the native binary for this platform.
  agent_package="http:muse[url=https://api.meta.ai/muse-launcher.sh,bin=muse,version_list_url=https://api.meta.ai/muse-code/channels/muse-stable,version_json_path=.version]"
  ;;
cursor | cursor-agent) agent="cursor-agent"; name="Cursor CLI" ;;
*)
  echo "Usage: omarchy-default-agent <pi|omp|opencode|ori|claude|codex|grok|openclaw|agy|hermes|copilot|crush|cursor-agent|muse>"
  exit 1
  ;;
esac

agent_package=${agent_package:-$agent}

# Hermes and OpenClaw are not mise tools: Hermes is the desktop app's
# self-updating runtime and OpenClaw comes from its pacman package. Each has
# its own installer with the same --check/--now contract mise-backed agents
# get from mise; see omarchy-install-hermes-cli and omarchy-install-openclaw-cli.
if [[ -n ${agent_installer:-} ]]; then
  # Not omarchy-cmd-present: a command on PATH says nothing about whether the
  # install behind it finished or runs the sessions omarchy-agent starts.
  # Treating it as installed would skip the floating terminal and run a
  # minutes-long install inside the menu action instead.
  agent_present() { "$agent_installer" --check; }
  agent_install() { "$agent_installer" --now; }
else
  # Anything at the wrapper's path other than the wrapper is the user's own
  # install, such as the symlink Cursor's installer leaves. A mise copy would
  # only shadow it, since the mise shims precede ~/.local/bin on PATH.
  user_install() {
    [[ -x $HOME/.local/bin/$agent ]] &&
      { [[ -L $HOME/.local/bin/$agent ]] || ! grep -q '^mise use -g' "$HOME/.local/bin/$agent"; }
  }
  agent_present() { user_install || mise where "$agent_package" &>/dev/null; }
  agent_install() { user_install || mise use -g "$agent_package"; }
fi

if [[ $installing == "false" ]] && ! agent_present; then
  exec omarchy-launch-floating-terminal-with-presentation omarchy-default-agent --install "$agent"
fi

if ! agent_install; then
  if [[ $installing == "true" && -n ${agent_installer:-} ]]; then
    echo "Could not install $name" >&2
  elif [[ $installing == "true" ]]; then
    echo "Could not install $name with mise" >&2
  else
    echo "Could not set $name as the default coding agent" >&2
  fi
  exit 1
fi

if [[ $agent == "claude" ]]; then
  omarchy-install-chromium-claude 2>/dev/null || true
fi

mkdir -p "$(dirname "$agent_file")"
printf '%s\n' "$agent" >"$agent_file"

if [[ $installing == "true" ]]; then
  printf '\033[2J\033[3J\033[H'
  exec omarchy-agent --inline
else
  exec omarchy-agent
fi
