# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# Must come before shfuncs.sh, because we have a better quote()
if [[ $PS1 && -f /etc/bash_completion && -z "$BASH_COMPLETION" ]]; then
    . /etc/bash_completion
fi

. ~/bin/shfuncs.sh

if (( BASH_VERSINFO[0] >= 4 )); then
	shopt -s globstar
fi


if type lesspipe >/dev/null 2>&1; then
    eval `lesspipe`
elif type lessfile >/dev/null 2>&1; then
    eval `lessfile`
fi

# My .inputrc binds C-w to kill-region (as in emacs), but that doesn't work
# if it is already set as the werase character.
tty -s && stty werase ^- 2>/dev/null

show_git_branch() {
    if [[ -d .git || (-f HEAD && -d objects) ]]; then
        { git branch | sed -ne 's/$/]/ ; s/^* / [/p'; } 2>/dev/null
    fi
}

# If running interactively, then:
if [ "$PS1" ]; then
    if pid_is_terminal "$PPID"; then
        SHLVL=1
    fi

    # Use 256 colors in gnome-terminal (for xterm we use .Xresources).
    if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
        TERM=gnome-256color
    fi

    # If our terminal type is unknown on this machine, use xterm.
    if ! infocmp >/dev/null 2>&1; then
        TERM=xterm
    fi

    # If xterm is monochrome, use xterm-color if that exists.
    if [[ $TERM = xterm && $(tput colors) -lt 8 ]] && infocmp xterm-color >/dev/null 2>&1; then
        TERM=xterm-color
    fi

    eval "$(dircolors -b)"

    # Avoid classification characters in pipes, and avoid an
    # OpenSolaris b118 bug whereby /usr/bin/ls --color=auto
    # reports an error whenever it would not activate color.
    ls() {
        local carg="";
        [ -t 1 ] && carg="--color -F"
        command ls $carg "$@"
    }

    # If we have colors, set a fancy prompt.
    if [[ $TERM && $(tput colors) -ge 8 ]]; then
        dscolor=3
        [[ $(hostname) = dirac* ]] && dscolor=3
        [[ $(hostname) = church* ]] && dscolor=1
        [[ $(hostname) = trillian* ]] && dscolor=4
        PS1="$(printf "%$((SHLVL - 1))s" )"
        PS1+='\[$(dir_access_color)\]\w'
        PS1+='\['"$(tput sgr0)"'\]'
        PS1+='$(show_git_branch)'
        PS1+='\['"$(tput setaf $dscolor)$(tput bold)"'\]\$'
        PS1+='\['"$(tput sgr0)"'\] '
        # unexport PS1 so that non-bash shells don't get confused
        export -n PS1

        # make less colorful
        export LESS_TERMCAP_mb="$(tput bold; tput setaf 6)"
        export LESS_TERMCAP_md="$(tput bold; tput setaf 1)"
        export LESS_TERMCAP_me="$(tput sgr0)"
        export LESS_TERMCAP_se="$(tput sgr0)"
        export LESS_TERMCAP_so="$(tput bold; tput setaf 3; tput setab 4)"
        export LESS_TERMCAP_ue="$(tput sgr0)"
        export LESS_TERMCAP_us="$(tput bold; tput setaf 2)"
    fi


    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|gnome*)
        PROMPT_COMMAND='nfm_xterm_title'
        ;;
    *)
        ;;
    esac


    shopt -s histappend
    unset HISTFILESIZE
    # export in case we need to do bash --norc
    export HISTSIZE=500000
    export HISTTIMEFORMAT="[%F %T] "

    shopt -s histreedit
    export TIMEFORMAT=$'%3U s.user + %3S s.sys  %3R s.elapsed  %P%%\n'
    export MAILPATH='/var/mail/neil?You have mail.:/home/neil/mail/debian?You have debian mail.'
    export MAILCHECK=10
fi # interactive only


export JAVA_OPTS=-Djava.net.preferIPv4Stack=true
export VISUAL=vim
export EDITOR=vim
export PAGER=less
export IRCNAME='Neil Moore <neil@s-z.org>'
export IRCNICK='\amethyst'
export IRCSERVER=irc.freenode.net # irc.vrfx.com
export SSFE='-beep'
export MAILREADER=/home/neil/bin/vmmail
export WWW_HOME='http://www.google.com/'
export PS_FORMAT='user,pid,stat,tname,bsdtime,pcpu,rss,sz,pmem,command'
export BC_ENV_ARGS="-l ${HOME}/.bcrc"