#!/bin/bash

# omarchy:summary=Write the account dispatchers for the installed claude, codex and grok
# omarchy:args=[claude|codex|grok...]
# omarchy:hidden=true

# A dispatcher runs its CLI as the selected subscription account
# (omarchy-agent-account-exec), and leads PATH so scripts and editors follow
# account switches too. One exists only while its CLI is installed: there's no
# claude command before Claude Code is. Installing or removing an agent through
# Omarchy runs this; with no agents named, every one is brought in line.

dir="$HOME/.local/state/omarchy/agent-accounts/bin"

agents=("$@")
(( ${#agents[@]} )) || agents=(claude codex grok)

for agent in "${agents[@]}"; do
  case $agent in
  claude | codex | grok) ;;
  *) continue ;;
  esac

  # The real CLI is whatever PATH finds past the dispatchers, this home's or not.
  if type -aP "$agent" | grep -qvF /omarchy/agent-accounts/bin/; then
    # Renamed into place, so a command starting while this runs never finds
    # the dispatcher half written.
    mkdir -p "$dir"
    printf '#!/bin/bash\nexec omarchy-agent-account-exec %s "$@"\n' "$agent" >"$dir/.$agent.new"
    chmod +x "$dir/.$agent.new"
    mv -f "$dir/.$agent.new" "$dir/$agent"
  else
    rm -f "$dir/$agent"
  fi
done
