#!/usr/bin/env zsh
# Initialize the ZSH Standard Library

# look for the library root
if [[ -z $ZSL_LIB_ROOT ]]; then
    # loaded with bare name?
    _zsl_binpath="$0"
    if [[ $_zsl_binpath != */* ]]; then
        _zsl_binpath=$(which "$0")
        if (($?)); then
            echo "cannot locate $0" >&2
            exit 1
        fi
    fi
    _zsl_binpath="$(realpath "$_zsl_binpath")"
    _zsl_bindir="$(dirname "$_zsl_binpath")"
    _zsl_maybe_root="$(dirname "$_zsl_bindir")"
    if [[ -f "$_zsl_maybe_root/modules/zslinit.zsh" ]]; then
        export ZSL_ROOT="$_zsl_maybe_root"
    else
        echo "cannot find stdlib at $ZSL_ROOT" >&2
        exit 1
    fi
fi

if ! . "$ZSL_ROOT/modules/zslinit.zsh"; then
    echo "error sourcing stdlib" >&2
    exit 2
fi
require logging

# check if we are run directly
if [[ "$ZSH_SCRIPT" = "$0" ]]; then
    cat <<EOH
This is Michael's zsh standard library entry point, not mean to run directly.
Load into a script with:
    . stdlib.zsh
EOH
    print -P "%BAvailable modules:%b"
    zsl_list_modules
    exit 1
fi
