#PATHs
if [ -d "$HOME/.local/bin" ]; then
    export PATH="$HOME/.local/bin:$PATH"
fi

if [ -f "$HOME/.bash_functions" ]; then
    source "$HOME/.bash_functions"
fi

# User profiles
source $HOME/.profile
# Define $POWERLINE_ROOT
if [ -f $HOME/.profile.local ]; then
    set -a
    source $HOME/.profile.local
    set +a
fi

if [ -n "$DOTFILES_DIR" ] && [ -d "$DOTFILES_DIR" ]; then
    # Gruvbox colors
    [[ -f "$DOTFILES_DIR/dependencies/gruvbox/gruvbox_256palette.sh" ]] && source "$DOTFILES_DIR/dependencies/gruvbox/gruvbox_256palette.sh"
fi

# Italic support
if [ -f $HOME/.xterm-256color-italic.terminfo ]; then
    tic $HOME/.xterm-256color-italic.terminfo
fi
if [ -f $HOME/.screen-256color-italic.terminfo ]; then
    tic $HOME/.screen-256color-italic.terminfo
fi

# Use neovim where available
if program_installed neovim; then
    alias vim='nvim'
fi

# fzf fuzzy finder
if program_installed fzf; then
    # TODO: This is broken when fzf is old, should include a local copy
    eval "$(fzf --bash)"
fi

# thefuck command correction
if program_installed fuck; then
    eval "$(thefuck --alias)"
fi

# aactivator
if program_installed aactivator; then
    eval "$(aactivator init)"
fi

# Preferred editor for local and remote sessions
export EDITOR='vim'

# Powerline
if program_installed powerline; then
    if [ -n "$POWERLINE_ROOT" ] && \
        [ -f "$POWERLINE_ROOT" ] && \
        [ -f "$POWERLINE_ROOT/bindings/bash/powerline.sh" ]; then
        # Skip lookup if location is known
        source "$POWERLINE_ROOT/bindings/bash/powerline.sh"
    else
        POWERLINE_BASH_SOURCES=( $(powerline_bash_sources) )
        found=0
        for file in "${POWERLINE_BASH_SOURCES[@]}"; do
            if [[ -a $file ]]; then
                # TODO: Store the POWERLINE_ROOT in .profile.local if found
                source $file
                found=1
                break
            fi
        done
        if [ $found -eq 0 ]; then
            echo "Powerline binding not found."
            echo "Please set \$POWERLINE_ROOT in .profile.local"
        fi
    fi
fi

# Local overrides
if [ -f $HOME/.bashrc_local ]; then
    source $HOME/.bashrc_local
fi
