# Set file as having been loaded to avoid looping
IS_BASH_PROFILE_LOADED=true
# If bashrc hasn't been loaded, load it
if [ -z "$IS_BASHRC_LOADED" ] ; then
    # shellcheck source=bashrc
    source "$HOME/.bashrc"
fi

# Some stuff is OS Dependent
DET_OS="unknown"
UNAME_STR=$(uname)

if [[ "$UNAME_STR" == "Darwin" ]]; then
    DET_OS="mac"
elif [[ "$UNAME_STR" == "Linux" ]]; then
    DET_OS="linux"
fi

# Since this is a mac, source the bashrc
if [[ "$DET_OS" == "mac" ]]; then
    # Bash Completion
    if [ -f /opt/local/etc/profile.d/bash_completion.sh ]; then
        source /opt/local/etc/profile.d/bash_completion.sh
    fi
    if [ -f /opt/local/share/bash-completion/completions/git-flow ]; then
        source /opt/local/share/bash-completion/completions/git-flow
    fi
fi

# Set prompt to include some useful information
function ps_git_branch {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return
    echo " (${ref#refs/heads/})"
}

function ps_atf_target {
    org=$(atf-target 2> /dev/null) || return
    echo " [$org]"
}

function ps_force_target {
    test "true" = "$( git config force.use 2> /dev/null )" || return
    org=$(force-target 2> /dev/null) || return
    echo " [$org]"
}

function ps_host_name {
    # Returns hostname if on remote host and not using tmux
    # Check if we're on a non-local host via ssh
    if [ -n "$SSH_CLIENT" ] && [ -n "$SSH_TTY" ]; then
        # Check if we're using tmux, since tmux status line displays the host
        if [ -z "$TMUX" ]; then
            echo '\h '
        fi
    fi
}

function ps_virtual_env {
    [ -z "$VIRTUAL_ENV" ] && return
    venv=$(basename "$VIRTUAL_ENV") || return
    echo " [$venv]"
}

# Disable standard virtualenv prompt
export VIRTUAL_ENV_DISABLE_PROMPT=1

# Alias for colors
# RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
ENDCOLOR="\[\e[0m\]"

# New prompt format
PS1="$(ps_host_name)\W$YELLOW\$(ps_git_branch)$ENDCOLOR$GREEN\$(ps_atf_target)\$(ps_force_target)\$(ps_virtual_env)$ENDCOLOR\$ "

# Prompt Title
export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007"'

# Set cursor colors
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad

# Export colors
eval $($HOME/bin/derive_colors.py --export)

# Check for nerd font
if fc-list -q 'Symbols Nerd Font'; then
    # Doesn't guarantee the font is in use, but it's a good guess
    export TERM_NERD_FONT=1
fi

# FZF
if [ -f ~/.fzf.bash ]; then
    source ~/.fzf.bash
elif fzf --bash &> /dev/null; then
    source <(fzf --bash)
else
    if [ -d /opt/local/share/fzf/shell ]; then
        fzf_shell_path=/opt/local/share/fzf/shell
    elif [ -d /usr/share/doc/fzf/examples/ ]; then
        fzf_shell_path=/usr/share/doc/fzf/examples
    fi

    if [ -n "$fzf_shell_path" ]; then
        # Auto-completion
        [[ $- == *i* ]] && source $fzf_shell_path/completion.bash 2> /dev/null

        # Key bindings
        source $fzf_shell_path/key-bindings.bash
    fi
fi
if type rg &> /dev/null; then
    export FZF_DEFAULT_COMMAND='rg --files'
    # export FZF_DEFAULT_COMMAND='rg --files --no-ignore-vcs --hidden'
elif type ag &> /dev/null; then
    export FZF_DEFAULT_COMMAND='ag -g ""'
fi
if [ -n "$FZF_DEFAULT_COMMAND" ]; then
    export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND \$dir"
fi

# vim: set filetype=sh: