# Console 99 starts zsh with ZDOTDIR pointing here, so zsh reads each of its startup files from here. Each one
# sources the owner's own file with their ZDOTDIR in place, then points ZDOTDIR here again for the next;
# .zlogin, the last, hands it back for good. The owner's files run at the top level, not in a function,
# so their typeset and local behave as without Console 99.

typeset -g _p133_directory=$ZDOTDIR

# What the app set from env and the tab's profile, noted before the owner's files run, as .zlogin sets it again
# after them, so it wins. ZDOTDIR isn't the app's to set.
typeset -gA _p133_app_variables
for _p133_name in ${=CONSOLE99_VARIABLE_NAMES}; do
  [[ $_p133_name == ZDOTDIR ]] && continue
  _p133_app_variables[$_p133_name]=${(P)_p133_name}
done
unset _p133_name CONSOLE99_VARIABLE_NAMES

# Points ZDOTDIR at the owner's directory, as their own startup files expect.
_p133_owner_zdotdir() {
  if [[ -n $_p133_owner_set ]]; then
    export ZDOTDIR=$_p133_owner
  else
    unset ZDOTDIR
  fi
}

# Notes the owner's ZDOTDIR, which their file may have changed, and points ZDOTDIR here again.
_p133_our_zdotdir() {
  typeset -g _p133_owner_set=${ZDOTDIR+set} _p133_owner=$ZDOTDIR
  export ZDOTDIR=$_p133_directory
}

typeset -g _p133_owner_set=${CONSOLE99_ZDOTDIR+set} _p133_owner=$CONSOLE99_ZDOTDIR
unset CONSOLE99_ZDOTDIR
_p133_owner_zdotdir
{
  [[ -r ${ZDOTDIR:-$HOME}/.zshenv ]] && source ${ZDOTDIR:-$HOME}/.zshenv
} always {
  _p133_our_zdotdir
  [[ -o interactive ]] && source $_p133_directory/p133-integration.zsh
}
