# .zshrc

# Ensure current directory is readable
[[ -r "$PWD" ]] || cd "$HOME"

#export PROMPT="%n@%m %~ %# "
export PROMPT="%F{magenta}%n %F{blue}%~%f %# "

autoload -Uz +X compinit && compinit

# Case insensitive tab completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'

# auto-fill the first viable candidate for tab completion
setopt menucomplete

# vi-editing on command line and for files
bindkey -v
export EDITOR=vi

# Fix zsh bug where tab completion hangs on git commands
# https://superuser.com/a/459057
__git_files () {
    _wanted files expl 'local files' _files
}

# Only allow unique entries in path
typeset -U path

# utilities
command -v bat &>/dev/null && alias cat='bat --paging=never'
command -v direnv &>/dev/null && eval "$(direnv hook zsh)"

# ls
if command -v eza &>/dev/null ; then
    alias ls=eza
    alias l='ls -l --git'
    alias li='ls -l --git --git-ignore'
    alias ll='ls -al --git'
    alias lli='ls -al --git --git-ignore'
    alias tree='ls -lT --git'
else
    alias l='ls -l'
    alias ll='ls -al'
fi

# Perform sandvault setup
"$HOME/guest-setup"

# Use GNU binaries over outdated OSX CLI binaries
if command -v brew &>/dev/null ; then
    BREW_PREFIX="$(brew --prefix)"
    if [[ -d "$BREW_PREFIX/opt/coreutils/libexec/gnubin" ]]; then
        path=("$BREW_PREFIX/opt/coreutils/libexec/gnubin" $path)
    fi
    if [[ -d "$BREW_PREFIX/opt/findutils/libexec/gnubin" ]]; then
        path=("$BREW_PREFIX/opt/findutils/libexec/gnubin" $path)
    fi
    if [[ -d "$BREW_PREFIX/opt/gnu-getopt/bin" ]]; then
        path=("$BREW_PREFIX/opt/gnu-getopt/bin" $path)
    fi
    if [[ -d "$BREW_PREFIX/opt/python/libexec/bin" ]]; then
        path=("$BREW_PREFIX/opt/python/libexec/bin" $path)
    fi
fi

# Add bin directories
path=("$HOME/.local/bin" $path)
path=("$HOME/bin" $path)
export PATH


###############################################################################
# Create symbolic links for all projects
###############################################################################
# Wait until install.sh has installed coreutils with homebrew
# so we can use the simpler/easier/better homebrew ln tool
LN="$(brew --prefix)/opt/coreutils/libexec/gnubin/ln"
# Guard the shared-folder mount: when an instance is started with no --dir
# args (or before tart has surfaced the mount), the path is absent — `fd`
# would error. -r on xargs handles the symmetric case where the mount is
# present but empty, avoiding `ln: missing file operand`.
if [[ -x "$LN" ]] && [[ -d "/Volumes/My Shared Files" ]]; then
    mkdir -p "$HOME/projects"
    fd -t d --max-depth 1 . "/Volumes/My Shared Files" -0 | \
        xargs -0 -r "$LN" -sf --target "$HOME/projects"
fi


###############################################################################
# Load user configuration
###############################################################################
if [[ -f "$HOME/user/.zshrc" ]]; then
    source "$HOME/user/.zshrc"
fi


###############################################################################
# Set active project
###############################################################################
PROJECT="${PROJECT:-project}"
PROJECT_DIR="$HOME/projects/$PROJECT"
if [[ -d "$PROJECT_DIR" ]]; then
    cd "$PROJECT_DIR"
    # Reconcile this project's Brewfile (cheap when satisfied; self-heals
    # when the user adds the project via 'clod set --dir' or edits Brewfile).
    if [[ -f "$HOME/lib/brewfile.sh" ]]; then
        source "$HOME/lib/brewfile.sh"
        apply_brewfile_if_present "$PROJECT_DIR"
    fi
    # If INITIAL_DIR is set, navigate to the subdirectory within the project
    if [[ -n "${INITIAL_DIR:-}" ]] && [[ -d "$PROJECT_DIR/$INITIAL_DIR" ]]; then
        cd "$PROJECT_DIR/$INITIAL_DIR"
    fi
fi

# Run specified application
command_args=()
if [[ -n "${COMMAND_ARGS_B64:-}" ]]; then
    while IFS= read -r -d '' arg; do
        command_args+=("$arg")
    done < <(printf '%s' "$COMMAND_ARGS_B64" | base64 --decode)
fi

if [[ "${COMMAND:-}" != "" ]]; then
    exec "$COMMAND" "${command_args[@]}"
elif [[ ${#command_args[@]} -gt 0 ]]; then
    exec "${command_args[@]}"
fi

