# Stub only. Everything of substance lives in ~/.config/zsh/rc.d/.
#
# Load order, and why it is this one:
#
#   1. p10k instant prompt   must be the first thing that runs
#   2. role                  needed before plugins, see below
#   3. plugins               must be set before oh-my-zsh reads them
#   4. oh-my-zsh
#   5. rc.d/[1-4]*.zsh       shared by every machine
#   6. rc.d/50-<role>.zsh    exactly one of desktop/server/mobile
#   7. rc.d/90-local.zsh     machine-local, never in the repository
#
# Step 3 is the reason the role is resolved so early: oh-my-zsh consumes the
# plugins array while sourcing, so nothing loaded afterwards can influence it.

# Must stay at the top: this block may print, and anything above it that also
# prints breaks the instant prompt. Absent on machines without powerlevel10k,
# where the test simply fails and nothing happens.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

export ZSH="$HOME/.oh-my-zsh"

# Defaults to desktop so a machine without the file still gets a working shell.
# Servers must have it in place before the first login, or shron comes up on
# the desktop profile - blue prompt on a production mail server.
ROLE=$(cat "$HOME/.config/dotconfs/role" 2>/dev/null || echo desktop)
export ROLE

# One list for every machine, filtered by what is actually installed. A plugin
# named but not present makes oh-my-zsh complain on every single shell start,
# and the alternative - a separate list per role - means editing three files to
# add one plugin.
_want_plugins=(git archlinux colored-man-pages colorize zsh-interactive-cd fzf)
plugins=()
for _p in $_want_plugins; do
  if [[ -d "$ZSH/plugins/$_p" || -d "${ZSH_CUSTOM:-$ZSH/custom}/plugins/$_p" ]]; then
    plugins+=("$_p")
  fi
done
unset _p _want_plugins

# Theme and oh-my-zsh's own settings, which it reads while being sourced.
# Anything of that kind placed in a role file - which comes afterwards - is
# silently ignored, so it lives here instead.
[[ -r "$HOME/.config/zsh/rc.d/05-pre-omz.zsh" ]] && source "$HOME/.config/zsh/rc.d/05-pre-omz.zsh"

source "$ZSH/oh-my-zsh.sh"

# (N) makes an empty directory a no-op instead of an error.
for _f in "$HOME"/.config/zsh/rc.d/[1-4]*.zsh(N); do
  source "$_f"
done
unset _f

[[ -r "$HOME/.config/zsh/rc.d/50-$ROLE.zsh" ]] && source "$HOME/.config/zsh/rc.d/50-$ROLE.zsh"

# Last on purpose: this one can override anything above without the repository
# needing to know about it.
[[ -r "$HOME/.config/zsh/rc.d/90-local.zsh" ]] && source "$HOME/.config/zsh/rc.d/90-local.zsh"
