# Eryn Wells <eryn@erynwells.me>

# Don't read global startup. It messes things up...
unsetopt GLOBAL_RCS

export SYS=`uname -s | tr A-Z a-z`

function init-env-dotfiles-path
{
    local dotfiles_config="$HOME/.config/dotfiles-home"
    if [[ -f "$dotfiles_config" ]]; then
        export DOTFILES_HOME=$(cat "$dotfiles_config")
        return
    fi

    if [[ -f "$HOME/.dotfiles/setup.sh" ]]; then
        export DOTFILES_HOME="$HOME/.dotfiles"
        return
    fi

    echo "WARNING: Couldn't find path to dotfiles" 1>&2
}

function init-env-fpath
{
    local -r fpath_candidates=( \
        "$DOTFILES_HOME/zsh/${SYS}-functions" \
        "$DOTFILES_HOME/zsh/func" \
    )

    # Process the array in reverse order (`Oa` means "descending index order",
    # in other worse "reverse order"; see PARAMETER EXPANSION section of the man
    # pages) so the paths are prepended in the correct order.
    for candidate in ${(Oa)fpath_candidates}; do
        if [[ ! -d "$candidate" ]]; then
            continue
        fi
        fpath=($candidate $fpath)
    done
}

init-env-dotfiles-path
init-env-fpath

autoload -Uz do_init_functions

typeset -a zsh_init_env_functions=( \
    init-env \
    init-env-path \
    init-env-tilde-paths \
    init-env-python \
    init-env-playdate \
    init-env-vi \
    init-env-$SYS \
)

do_init_functions zsh_init_env_functions
