# Single source of truth for OMARCHY_PATH + PATH adjustments. Sourced by:
#   /etc/profile.d/omarchy.sh                 (system login shells)
#   /etc/skel/.bashrc                         (interactive shells)
#   /usr/share/uwsm/env.d/10-omarchy          (Hyprland session via uwsm)
#   /usr/share/omarchy/default/bash/envs      (bash rc chain; SSH/non-login)

# /etc/omarchy.conf is written by omarchy-dev-link and reset by
# omarchy-dev-unlink. When absent, force the packaged default instead of
# preserving a stale inherited value.
if [ -f /etc/omarchy.conf ]; then
  . /etc/omarchy.conf
  : "${OMARCHY_PATH:=/usr/share/omarchy}"
else
  OMARCHY_PATH=/usr/share/omarchy
fi
export OMARCHY_PATH

# Only prepend in dev-link mode. On a production install, the binaries are
# already on PATH as /usr/bin/omarchy-* via the omarchy package — prepending
# /usr/share/omarchy/bin would just be noise.
if [ "$OMARCHY_PATH" != /usr/share/omarchy ]; then
  case ":$PATH:" in
    *":${OMARCHY_PATH%/}/bin:"*) ;;
    *) PATH="${OMARCHY_PATH%/}/bin${PATH:+:$PATH}" ;;
  esac
fi

# The subscription account dispatchers for claude, codex and grok must precede
# real tool directories inherited from a parent shell, so scripts follow account
# switches without Bash functions. mise's command wrappers did this once; that
# directory is dropped wherever an older session left it. mise's shims go just
# ahead of the system directories, as in the uwsm session, so a version pinned
# with mise wins in login shells too while anything a parent put first stays first.
omarchy_wrapper_dir="$HOME/.local/state/omarchy/agent-accounts/bin"
omarchy_old_wrapper_dir="${MISE_DATA_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mise}/command-wrappers/bin"
omarchy_shims_dir="$HOME/.local/share/mise/shims"
omarchy_wrapper_path=$omarchy_wrapper_dir
omarchy_shims_placed=0
if [ -n "${PATH:-}" ]; then
  omarchy_remaining_path=$PATH
  while :; do
    omarchy_path_entry=${omarchy_remaining_path%%:*}
    case "$omarchy_path_entry" in
      "$omarchy_wrapper_dir" | "$omarchy_old_wrapper_dir" | "$omarchy_shims_dir") ;;
      /usr/local/sbin | /usr/local/bin | /usr/sbin | /usr/bin | /sbin | /bin)
        if [ "$omarchy_shims_placed" = 0 ]; then
          omarchy_wrapper_path="$omarchy_wrapper_path:$omarchy_shims_dir"
          omarchy_shims_placed=1
        fi
        omarchy_wrapper_path="$omarchy_wrapper_path:$omarchy_path_entry"
        ;;
      *) omarchy_wrapper_path="$omarchy_wrapper_path:$omarchy_path_entry" ;;
    esac
    case "$omarchy_remaining_path" in
      *:*) omarchy_remaining_path=${omarchy_remaining_path#*:} ;;
      *) break ;;
    esac
  done
fi
if [ "$omarchy_shims_placed" = 0 ]; then
  omarchy_wrapper_path="$omarchy_wrapper_path:$omarchy_shims_dir"
fi
PATH=$omarchy_wrapper_path
unset omarchy_wrapper_dir omarchy_old_wrapper_dir omarchy_shims_dir omarchy_wrapper_path omarchy_shims_placed omarchy_remaining_path omarchy_path_entry

# Arch's ROCm packages keep their commands in /opt/rocm/bin, which rocm-core's
# /etc/profile.d/rocm.sh adds only for login shells.
if [ -d /opt/rocm/bin ]; then
  export ROCM_PATH="${ROCM_PATH:-/opt/rocm}"
  case ":$PATH:" in
    *":/opt/rocm/bin:"*) ;;
    *) PATH="${PATH:+$PATH:}/opt/rocm/bin" ;;
  esac
fi

# ~/.local/bin is appended, so system binaries keep precedence over it. Keep
# this order in sync with the PAM PATH line from install/config/ssh-command-path.sh,
# which covers SSH commands that run no shell setup at all.
case ":$PATH:" in
  *":$HOME/.local/bin:"*) ;;
  *) PATH="${PATH:+$PATH:}$HOME/.local/bin" ;;
esac
export PATH
_OMARCHY_ENV_BOOTSTRAPPED=1
