# Liho shell integration for zsh — .zshenv.
#
# zsh reads four files from ZDOTDIR, and the editor has pointed ZDOTDIR at a
# directory of its own to get .zshrc read at all. That would silently drop the
# other three, so each of them exists here for one purpose: to read the
# person's own file. A setup that puts its PATH in .zshenv must keep working.
#
# This is also where the real ZDOTDIR is remembered. The editor exports it, and
# the fallback covers a shell started some other way.
if [[ -n "${LIHO_CONTROL_POSIX-}" && -r "$LIHO_CONTROL_POSIX" ]]; then
  source "$LIHO_CONTROL_POSIX"
  __liho_control_bootstrap zsh
else
  unset LIHO_CONTROL_ENDPOINT LIHO_CONTROL_CAPABILITY LIHO_CONTROL_GENERATION
  unset LIHO_CONTROL_CLIENT LIHO_CONTROL_NODE LIHO_CONTROL_POSIX
fi

: "${LIHO_USER_ZDOTDIR:=${ZDOTDIR:-$HOME}}"
export LIHO_USER_ZDOTDIR

__liho_integration_zdotdir="$ZDOTDIR"
ZDOTDIR="$LIHO_USER_ZDOTDIR"

if [[ -f "$LIHO_USER_ZDOTDIR/.zshenv" ]]; then
  source "$LIHO_USER_ZDOTDIR/.zshenv"
fi

# A common .zshenv moves ZDOTDIR to ~/.config/zsh. Keep that as the person's
# directory, but point startup back to our directory until .zshrc installs the
# hooks; zsh resolves every subsequent startup file from the current ZDOTDIR.
if [[ "${ZDOTDIR-}" != "$__liho_integration_zdotdir" ]]; then
  LIHO_USER_ZDOTDIR="${ZDOTDIR:-$HOME}"
  export LIHO_USER_ZDOTDIR
fi
ZDOTDIR="$__liho_integration_zdotdir"
unset __liho_integration_zdotdir
