KBWDEBUG=${KBWDEBUG:-no}

function dprint {
if [[ $KBWDEBUG == yes && $- == *i* ]]; then
    #date "+%H:%M:%S $*"
    echo $SECONDS $*
fi
}
dprint alive
if [ -r "${HOME}/.bashrc.local.preload" ]; then
    dprint "Loading bashrc preload"
    source "${HOME}/.bashrc.local.preload"
fi

#########################
# Some useful functions #
#########################

function have { type "$1" &>/dev/null ; }

# This is a readlink wrapper, just in case readlink doesn't exist.
function readlink_func \
{
    if have readlink ; then
        readlink "$1"
    #elif have perl ; then # seems slower than alternative
    #   perl -e 'print readlink("'"$1"'") . "\n"'
    else
        \ls -l "$1" | sed 's/[^>]*-> //'
    fi
}

# This is a `realpath` replacement, since realpath is a rare utility.
# Essentially, you feed it a path, and it spits out the same path with all of
# the symbolic links resolved.
# This is a function, rather than broken out into a script, because its used
# by add_to_path_file(), and if it was a script, using it would cause an
# infinite loop.
function realpath_func \
{
    local input="${1}"
    local output="/"
    if [ -d "$input" -a -x "$input" ] ; then
        # All too easy...
        output=`( cd "$input"; \pwd -P )`
    else
        # sane-itize the input to the containing folder
        input="${input%%/}"
        local fname="${input##*/}"
        input="${input%/*}"
        if [ ! -d "$input" -o ! -x "$input" ] ; then
            echo "$input is not an accessible directory" >&2
            return
        fi
        output="`( cd "$input" ; \pwd -P )`/"
        input="$fname"
        # output is now the realpath of the containing folder
        # so all we have to do is handle the fname (aka "input)
        if [ ! -L "$output$input" ] ; then
            output="$output$input"
        else
            input="`readlink_func "$output$input"`"
            while [ "$input" ] ; do
                if [[ $input == /* ]] ; then
                    output="$input"
                    input=""
                elif [[ $input == ../* ]] ; then
                    output="${output%/*/}/"
                    input="${input#../}"
                elif [[ $input == ./* ]] ; then
                    input="${input#./}"
                elif [[ $input == */* ]] ; then
                    output="$output${input%${input#*/}}"
                    input="${input#*/}"
                else
                    output="$output$input"
                    input=""
                fi
                if [ -L "${output%%/}" ] ; then
                    if [ "$input" ] ; then
                        input="`readlink_func "${output%%/}"`/$input"
                    else
                        input="`readlink_func "${output%%/}"`"
                    fi
                    output="${output%%/}"
                    output="${output%/*}/"
                fi
            done
        fi
    fi
    echo "${output%%/}"
}

# Usage: add_to_path_force PATH /some/directory
# This adds /some/directory to the end of PATH, but if /some/directory is
# already in PATH, it does nothing
function add_to_path_force \
{
    if eval '[[' -z "\$$1" ']]'; then
        eval "export $1='$2'"
    elif ! eval '[[' \
        -z "\"\${$1##*:\$2:*}\"" '||' \
        -z "\"\${$1%%*:\$2}\"" '||' \
        -z "\"\${$1##\$2:*}\"" '||' \
        "\"\${$1}\"" '==' "\"$2\"" ']]'; then
        eval "export $1=\"\$$1:$2\""
    fi
}

# Usage: add_to_path PATH /some/directory
# This adds /some/directory to the end of PATH, but if /some/directory is
# already in PATH, it does nothing
function add_to_path \
{
    local folder="${2%%/}"
    [ -d "$folder" -a -x "$folder" ] || return
    folder=`( cd "$folder" ; \pwd -P )`
    add_to_path_force "$1" "$folder"
}


# Usage: add_to_path_file PATH /some/file
# This adds /some/file to the end of PATH, but if /some/file is already
# in PATH, it does nothing.
function add_to_path_file \
{
    local file="${2}"
    [ -f "$file" -a -r "$file" ] || return
    # realpath alias may not be set up yet
    file=`realpath_func "$file"`
    add_to_path_force "$1" "$file"
}

# Usage: add_to_path_first PATH /some/directory
# This adds /some/directory to the *BEGINNING* of PATH, but prevents duplicate
# entries (i.e. duplicates are removed before /some/directory is added to the
# beginning of PATH)
function add_to_path_first \
{
    local folder="${2%%/}"
    [ -d "$folder" -a -x "$folder" ] || return
    folder=`( cd "$folder" ; \pwd -P )`
    # in the middle, move to front
    if eval '[[' -z "\${$1##*:$folder:*}" ']]'; then
        eval "$1=\"$folder:\${$1//:\$folder:/:}\""
        # at the end
    elif eval '[[' -z "\${$1%%*:\$folder}" ']]'; then
        eval "$1=\"$folder:\${$1%%:\$folder}\""
        # no path
    elif eval '[[' -z "\$$1" ']]'; then
        eval "$1=\"$folder\""
        # not in the path
    elif ! eval '[[' -z "\${$1##\$folder:*}" '||' "\$$1" '==' "\"$folder\"" ']]'; then
        eval "export $1=\"$folder:\$$1\""
    fi
}

# This function takes the path named (e.g. MANPATH) and makes sure
# that each item in the path exists, and resolves all symlinks. It also
# eliminates duplicates (with the use of add_to_path).
function verify_path \
{
    # separating cmd out is stupid, but is compatible
    # with older, buggy, bash versions (2.05b.0(1)-release)
    local cmd="echo \$$1"
    local arg="`eval $cmd`"
    eval "$1=\"\""
    while [[ $arg == *:* ]] ; do
        dir="${arg%:${arg#*:}}"
        arg="${arg#*:}"
        if [ "$dir" != "." -a -d "$dir" -a -x "$dir" -a -r "$dir" ] ; then
            dir=`( \cd "$dir" ; \pwd -P )`
            add_to_path "$1" "$dir"
        fi
    done
    if [ "$arg" != "." -a -d "$arg" -a -x "$arg" -a -r "$arg" ] ; then
        arg=`( cd "$arg" ; \pwd -P )`
        add_to_path "$1" "$arg"
    fi
}

dprint Setting up conf funcs ...

function updatesetup {
(
if [ "$HOSTNAME" == "atakapa.local" ] ; then
    if ssh-add -l | grep -q -v '75:50:80:4a:5d:dc:34:e1:fb:61:aa:91:9e:75:d9:30' ; then
        echo Add key to keychain...
        ssh-add
    fi
fi
if [ -d ~/.conf_secure/ ] ; then
    cd ~/.conf_secure
    [ "$1" ] && echo .conf_secure...
    cvs -q update
fi
if [ -d ~/.conf/ ] ; then
    cd ~/.conf/
    [ "$1" ] && echo .conf...
    cvs -q update
    bash ./setup.sh
else
    echo CONFIG IS NOT BEING MANAGED!
fi
)
}

function sendsetup {
(
if [ -d ~/.conf_secure/ ] ; then
    cd ~/.conf_secure
    cvs -q commit
fi
if [ -d ~/.conf/ ] ; then
    cd ~/.conf/
    cvs -q commit
else
    echo CONFIG IS NOT BEING MANAGED!
fi
)
}

########################################
# SYSTEM SETTINGS
########################################
dprint Loading system settings ...

# The KBW_SYSTEM_BASHRC is to make sure that the /etc/bashrc doesn't
# reload my ~/.bashrc and create a loop (I found a machine where it did)
if [[ -r /etc/bashrc && -z $KBW_SYSTEM_BASHRC ]]; then
    dprint " - loading /etc/bashrc"
    export KBW_SYSTEM_BASHRC=1
    . /etc/bashrc
fi

#########################################
# BEHAVIORAL SETTINGS
#########################################

shopt -s extglob # Fancy patterns, e.g. +()
# only interactive
if [[ $- == *i* ]]; then
    dprint setting the really spiffy stuff
    shopt -s checkwinsize # don't get confused by resized terminals
    shopt -s checkhash # if hash is broken, doublecheck it
    shopt -s cdspell # be tolerant of cd spelling mistakes
fi

##########################################
# ENVIRONMENT VARIABLES
##########################################

dprint PATH = $PATH

if [[ $TERM == *:* && ( $SSH_CLIENT || $SSH_TTY || $SSH_CLIENT2 ) ]] ; then
    dprint "Smuggled information through the TERM variable!"
    term_smuggling=( ${TERM//:/ } )
    export SSH_LANG=${term_smuggling[1]}
    TERM=${term_smuggling[0]}
    unset term_smuggling
fi

if [[ -z $USER || -z $GROUPNAME ]] ; then
    idout=(`id`)
    if [[ -z $USER ]] ; then
        USER="${idout[0]%%\)*}"
        USER="${USER##*\(}"
        dprint corrected missing USER to be $USER
    fi
    if [[ -z $GROUPNAME ]] ; then
        if [[ ${idout[i]%%\(*} != $GROUPNAME ]] ; then
            GROUPNAME="${idout[1]%%\)*}"
            GROUPNAME="${GROUPNAME##*\(}"
        else
            GROUPNAME="UnknownGroup"
        fi
        dprint corrected missing GROUPNAME to be $GROUPNAME
    fi
    unset idout
fi

# set up the standard variables
dprint term == $TERM

# I tote my own terminfo files around with me
[ -d ~/.terminfo ] && export TERMINFO=~/.terminfo/
[ "$TERM_PROGRAM" == "Apple_Terminal" ] && export TERM=nsterm-16color

MAILPATH=""
MAILCHECK=30
add_to_path_file MAILPATH /var/spool/mail/$USER
add_to_path MAILPATH $HOME/Maildir/
[[ -z $MAILPATH ]] && unset MAILCHECK
[[ -z $HOSTNAME ]] && export HOSTNAME=`/bin/hostname` && echo 'Fake Bash!'
HISTSIZE=10000
HOST=${OSTYPE%%[[:digit:]]*}
OS_VER=${OSTYPE#$HOST}
[ -z "$OS_VER" ] && OS_VER=$( uname -r )
OS_VER=(${OS_VER//./ })
TTY=`tty`
PARINIT="rTbgq B=.,?_A_a P=_s Q=>|}+"
GDBHISTFILE="~/.gdb_history"

export USER GROUPNAME MAILPATH HISTSIZE OS_VER HOST TTY PARINIT GDBHISTFILE

dprint ' - umask (started `umask`)'
if [[ $UID -gt 99 ]]; then
    umask 007
else
    umask 077
fi

if [[ $USER == root ]] ; then
    [[ $SSH_CLIENT || $SSH_TTY || $SSH_CLIENT2  ]] && export TMOUT=600 || export TMOUT=3600
fi

if [[ -z $INPUTRC && ! -r $HOME/.inputrc && -r /etc/inputrc ]]; then
    export INPUTRC=/etc/inputrc
fi

export BASHRCREAD=1

# Source global definitions
if [[ -r /etc/profile && -z $KBW_SYSTEM_PROFILE ]]; then
    dprint "- loading /etc/profile ... "
    export KBW_SYSTEM_PROFILE=1
    . /etc/profile
fi

if [[ $- == *i* ]]; then
    if [[ $TERM == xterm* || $OSTYPE == darwin* ]]; then
        # This puts the term information into the title
        PSterminfo='\[\e]2;\u@\h: \w\a\]'
    fi
    PSparts[3]='(\d \T)\n'
    PSparts[2]='[\u@\h \W]'
    PSparts[1]='\$ '
    PScolors[2]='\[\e[34m\]' # Blue
    PScolors[3]='\[\e[35m\]' # Purple
    PScolors[4]='\[\e[36m\]' # Cyan
    PScolors[5]='\[\e[32m\]' # Green
    PScolors[6]='\[\e[33m\]' # Yellow
    PScolors[100]='\[\e[31m\]' # Badc
    PScolors[0]='\[\e[0m\]' # Reset
    if [[ $USER == root ]] ; then
        PScolors[1]='\[\e[31m\]' # Red
    elif [[ $SSH_CLIENT || $SSH_TTY || $SSH_CLIENT2 ]] ; then
        PScolors[1]="${PScolors[6]}" # yellow
        if [[ $HOSTNAME == marvin ]] ; then
            PScolors[1]="${PScolors[5]}" # green
        fi
    else
        unset PSparts[3]
        PScolors[1]=""
    fi
    function bashrc_genps {
        if [ "$1" -a "${PScolors[$1]}" ] ; then
            PSgood="$PSterminfo${PSparts[3]}${PScolors[$1]}${PSparts[2]}${PSparts[1]}${PScolors[0]}"
        else
            PSgood="$PSterminfo${PSparts[3]}${PSparts[2]}${PSparts[1]}"
        fi
        PSbad="$PSterminfo${PSparts[3]}${PScolors[$1]}${PSparts[2]}${PScolors[100]}${PSparts[1]}${PScolors[0]}"
    }
    bashrc_genps 1
    function safeprompt {
        export PS1='{\u@\h \W}\$ '
        unset PROMPT_COMMAND
    }
    alias stdprompt='bashrc_genps 1'
    alias blueprompt='bashrc_genps 2'
    alias purpleprompt='bashrc_genps 3'
    alias cyanprompt='bashrc_genps 4'
    alias greenprompt='bashrc_genps 5'
    alias whiteprompt='bashrc_genps'
    # this is executed before every prompt is displayed
    # it changes the prompt based on the preceeding command
    export PROMPT_COMMAND='[ $? = 0 ] && PS1=$PSgood || PS1=$PSbad'
fi

dprint Manipulating your path ...

verify_path PATH
add_to_path PATH "/usr/local/sbin"
add_to_path PATH "/usr/local/teTeX/bin"
add_to_path PATH "/usr/X11R6/bin"
add_to_path PATH "$HOME/bin"
add_to_path PATH "$HOME/Public/bin"
add_to_path PATH /System/Library/Frameworks/Python.framework/Versions/2.3/bin
add_to_path PATH /Developer/usr/bin/
add_to_path_first PATH "/sbin"
add_to_path_first PATH "/bin"
add_to_path_first PATH "/usr/sbin"
add_to_path_first PATH "/opt/local/bin"
add_to_path_first PATH "/opt/local/sbin"
add_to_path_first PATH "/usr/local/bin"
add_to_path_first PATH "/usr/local/texlive/2008/bin/universal-darwin/"

if [[ $OSTYPE == darwin* ]] ; then
    add_to_path PATH "$HOME/.conf/darwincmds"
    add_to_path PATH "/usr/local/cuda/bin"
    add_to_path DYLD_LIBRARY_PATH "$HOME/lib"
    add_to_path DYLD_LIBRARY_PATH "/usr/local/cuda/lib"

    # The XFILESEARCHPATH (for app-defaults and such) is a wonky kind of path
    [ -d /opt/local/lib/X11/app-defaults/ ] && \
        add_to_path_force XFILESEARCHPATH /opt/local/lib/X11/%T/%N
    [ -d /sw/etc/app-defaults/ ] && \
        add_to_path_force XFILESEARCHPATH /sw/etc/%T/%N
    add_to_path_force XFILESEARCHPATH /private/etc/X11/%T/%N
fi

verify_path MANPATH
add_to_path MANPATH "/usr/man"
add_to_path MANPATH "/usr/share/man"
add_to_path MANPATH "/usr/X11R6/man"
add_to_path_first MANPATH "/opt/local/share/man"
add_to_path_first MANPATH "/opt/local/man"
add_to_path_first MANPATH "/usr/local/man"
add_to_path_first MANPATH "/usr/local/share/man"

verify_path INFOPATH
add_to_path INFOPATH "/usr/share/info"
add_to_path INFOPATH "/opt/local/share/info"

export PATH MANPATH INFOPATH

dprint Setting up standard envariables ...

export PAGER='less'
have vim && export EDITOR='vim' || export EDITOR='vi'
if [[ -z $DISPLAY && $OSTYPE == darwin* ]]; then
    processes=`ps ax`
    if [[ ${processes} == *xinit* || ${processes} == *quartz-wm* ]]; then
        export DISPLAY=:0
    else
        unset DISPLAY
    fi
fi
if [[ $HOSTNAME == wizard ]] ; then
    dprint Wizards X forwarding is broken
    unset DISPLAY
fi
export TZ="US/Central"
if [ "${BASH_VERSINFO[0]}" -le 2 ]; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL="ignorespace:erasedups"
fi
export HISTIGNORE="&:ls:[bf]g:exit"
export GLOBIGNORE=".:.."
export CVSROOT=kyle@cvs.memoryhole.net:/home/kyle/cvsroot
export CVS_RSH=ssh
export BASH_ENV=$HOME/.bashrc
if [[ $- == *i* ]]; then
    [ -r "$HISTFILE" -a -w "$HISTFILE" ] || echo "HISTFILE $HISTFILE has wrong permissions!"
fi
add_to_path_file MAILCAPS $HOME/.mailcap
add_to_path_file MAILCAPS /etc/mailcap
add_to_path_file MAILCAPS /usr/etc/mailcap
add_to_path_file MAILCAPS /usr/local/etc/mailcap
export EMAIL='kyle-envariable@memoryhole.net'
export GPG_TTY=$TTY
export RSYNC_RSH="ssh -2 -c arcfour -o Compression=no -x"
if [ -d /opt/local/include -a -d /opt/local/lib ] ; then
    export CPPFLAGS="-I/opt/local/include $CPPFLAGS"
    export LDFLAGS="-L/opt/local/lib $LDFLAGS"
fi
if have glibtoolize ; then
    have libtoolize || export LIBTOOLIZE=glibtoolize
fi
add_to_path LD_LIBRARY_PATH $HOME/lib
add_to_path LD_LIBRARY_PATH $HOME/Public/lib

# I prefer using terminals capable of displaying UTF-8 characters
# e.g. uxterm or urxvt or whatever
# This should make sure that LC_CTYPE is set correctly
if [[ -z $LANG ]] ; then
    dprint no LANG set
    if [[ $WINDOWID ]] && have xprop ; then
        dprint querying xprop
        __bashrc__wmlocal=(`xprop -id $WINDOWID -f WM_LOCALE_NAME 8s ' $0' -notype WM_LOCALE_NAME`)
        export LANG=`eval echo ${__bashrc__wmlocal[1]}`
        unset __bashrc__wmlocal
    elif [[ $OSTYPE == darwin* ]] ; then
        dprint "I'm on Darwin"
        if [[ ( $SSH_LANG && \
            ( $SSH_LANG == *.UTF* || $SSH_LANG == *.utf* ) || \
            $TERM_PROGRAM == Apple_Terminal ) && \
            -d "/usr/share/locale/en_US.UTF-8" ]] ; then
            export LANG='en_US.UTF-8'
        elif [ -d "/usr/share/locale/en_US" ] ; then
            export LANG='en_US'
        else
            export LANG=C
        fi
    elif [[ $TERM == linux || $TERM_PROGRAM == GLterm ]] ; then
        if [ -d "/usr/share/locale/en_US" ] ; then
            export LANG='en_US'
        else
            export LANG=C # last resort
        fi
    else
        if [[ $SSH_LANG == C ]] ; then
            export LANG=C
        elif have locale ; then
            dprint "checking locale from big list (A)"
            locales=`locale -a`
            locales="${locales//[[:space:]]/|}" # not +() because that's slow
            if [[ en_US.utf8 == @($locales) ]] ; then
                export LANG='en_US.utf8'
            elif [[ en_US.utf-8 == @($locales) ]] ; then
                export LANG='en_US.utf-8'
            elif [[ en_US.UTF8 == @($locales) ]] ; then
                export LANG='en_US.UTF8'
            elif [[ en_US.UTF-8 == @($locales) ]] ; then
                export LANG='en_US.UTF-8'
            elif [[ en_US == @($locales) ]] ; then
                export LANG='en_US'
            else
                export LANG=C
            fi
            unset locales
        fi
    fi
else
    dprint "- LANG IS ALREADY SET! ($LANG)"
    if [[ $SSH_LANG && $SSH_LANG != $LANG ]]; then
        if [[ $SSH_LANG == C ]] ; then
            export LANG=C
        else
            dprint "checking locale from big list (B)"
            locales=`locale -a`
            locales="${locales//[[:space:]]/|}" # not +() because that's slow
            if [[ $SSH_LANG == @(${locales}) ]] ; then
                dprint "- SSH_LANG is a valid locale, resetting LANG"
                LANG=$SSH_LANG
            else
                dprint "- SSH_LANG is NOT a valid locale"
                wantutf8=no
                if [[ $SSH_LANG == *.@(u|U)@(t|T)@(f|F)?(-)8 ]] ; then
                    wantutf8=yes
                    if [[ ! $LANG == *.@(u|U)@(t|T)@(f|F)?(-)8 ]] ; then
                        dprint "- want utf-8, but LANG is not utf8, unsetting"
                        unset LANG
                    fi
                else
                    dprint "- don't want utf-8"
                fi
                if [[ ! $LANG || ! $LANG == @($locales) ]] ; then
                    if [ "$wantutf8" = yes ] ; then
                        dprint "- finding a utf8 LANG"
                        if [[ en_US.utf8 == @($locales) ]] ; then
                            export LANG='en_US.utf8'
                        elif [[ en_US.utf-8 == @($locales) ]] ; then
                            export LANG='en_US.utf-8'
                        elif [[ en_US.UTF8 == @($locales) ]] ; then
                            export LANG='en_US.UTF8'
                        elif [[ en_US.UTF-8 == @($locales) ]] ; then
                            export LANG='en_US.UTF-8'
                        elif [[ en_US == @($locales) ]] ; then
                            export LANG='en_US'
                        else
                            export LANG=C
                        fi
                    else
                        dprint "- finding a basic LANG"
                        if [[ en_US == @($locales) ]] ; then
                            export LANG='en_US'
                        else
                            export LANG=C
                        fi
                    fi
                fi
                unset wantutf8
            fi
            unset locales
        fi
    else
        dprint "- ... without SSH_LANG, why mess with it?"
    fi
fi
dprint - LANG is $LANG
unset LESSCHARSET
if [[ $LANG == *.@(u|U)@(t|T)@(f|F)?(-)8 ]] ; then
    export LESSCHARSET=utf-8
fi

########
# Fink #
########

if [[ -r /sw/bin/init.sh ]] ; then
    dprint Loading fink stuff ...
    source /sw/bin/init.sh
fi

#########################
# ssh-agent environment #
#########################
if have ssh-agent ; then
    function get_ssh_info \
    {
        dprint - getting ssh-agent info
        { read SSH_AUTH_SOCK; read SSH_AGENT_PID; } < ~/.ssh_agent_socketinfo
        SSH_AUTH_SOCK="${SSH_AUTH_SOCK%; export *}"
        SSH_AUTH_SOCK="${SSH_AUTH_SOCK#SSH_AUTH_SOCK=}"
        SSH_AGENT_PID="${SSH_AGENT_PID%; export *}"
        SSH_AGENT_PID="${SSH_AGENT_PID#SSH_AGENT_PID=}"
        export SSH_AUTH_SOCK SSH_AGENT_PID
    }
    dprint Setting up the ssh-agent environment
    if [[ -z $SSH_AUTH_SOCK ]] ; then
        if [ -f ~/.ssh_agent_socketinfo ] ; then
            get_ssh_info
        fi
        if [[ $USER != root && ( -z $SSH_AUTH_SOCK || ! -S $SSH_AUTH_SOCK ) ]] ; then
            dprint - launching ssh-agent
            ssh-agent -s > ~/.ssh_agent_socketinfo
            get_ssh_info
        fi
    fi
    dprint "- SSH_AUTH_SOCK => $SSH_AUTH_SOCK"
    dprint "- SSH_AGENT_PID => $SSH_AGENT_PID"
    unset -f get_ssh_info
fi

#########################
# gpg-agent environment #
#########################
function get_gpg_info \
{
    dprint - getting gpg-agent info
    read GPG_AGENT_INFO < ~/.gpg_agent_socketinfo
    export GPG_AGENT_INFO="${GPG_AGENT_INFO#GPG_AGENT_INFO=}"
}
function setup_gpg_agent \
{
    dprint - launching gpg-agent
    gpg-agent --daemon --quiet \
        --write-env-file ~/.gpg_agent_socketinfo >/dev/null
    get_gpg_info
    export GPG_TTY=`tty`
}
function fix_gpg_agent \
{
    dprint Setting up the gpg-agent environment
    if [ -f ~/.gpg_agent_socketinfo ] ; then
        get_gpg_info
    fi
    if [ "$GPG_AGENT_INFO" -a -r "${GPG_AGENT_INFO%%:*}" ] ; then
        gpg_agent_pid=${GPG_AGENT_INFO%:*}
        gpg_agent_pid=${gpg_agent_pid#*:}
        # this tests to see if the given pid is running
        if kill -0 $gpg_agent_pid 2>/dev/null ; then
            dprint - agent already running
            export GPG_AGENT_INFO
            export GPG_TTY=`tty`
        else
            setup_gpg_agent
        fi
        unset gpg_agent_pid
    else
        setup_gpg_agent
    fi
    dprint "- GPG_AGENT_INFO => $GPG_AGENT_INFO"
    dprint "- GPG_TTY => $GPG_TTY"
}
if have gpg-agent ; then
    fix_gpg_agent
else
    unset -f setup_gpg_agent fix_gpg_agent get_gpg_info
fi

#######################################
# User specific aliases and functions #
#######################################
dprint Setting up aliases ...

function exec_cvim {
for V in /Applications/Vim-cocoa.app/Contents/MacOS/Vim \
    /Applications/Vim.app/Contents/MacOS/Vim ; do
    if [ -x "$V" ] ; then
        "$V" -g "$@" 2>/dev/null &
        break;
    fi
done
}

# use the new md* commands to use the spotlight database :D
# It's done as a function so I can use command-line arguments
# and single and double quotes.
function darwin_locate { mdfind "kMDItemDisplayName == '$@'wc"; }
if [[ $- == *i* && $OSTYPE == darwin* && ${OS_VER[0]} -ge 8 ]] ; then
alias locate=darwin_locate
fi

# this function is just darn useful sometimes
function printargs { for F in "$@" ; do printf '%s\n' "$F" ; done ; }
function printarray {
for ((i=0;$i<`eval 'echo ${#'$1'[*]}'`;i++)) ; do
    echo $1"[$i]" = `eval 'echo ${'$1'['$i']}'`
done
}
function foreach { prog="$1"; shift; for F in "$@" ; do "$prog" "$F" & done }

if [[ $- == *i* ]]; then
    alias back='cd "$OLDPWD"'
    # GNU ls check
    if [[ $OSTYPE == darwin* ]]; then
        dprint "- DARWIN ls"
        alias ls='/bin/ls -FG'
        alias ll='/bin/ls -lhFG'
    elif have colorls ; then
        dprint "- BSD colorls"
        alias ls='colorls -FG'
        alias ll='colorls -lhFG'
    else
        __kbwbashrc__lsarray=(`\type -ap ls`)
        __kbwbashrc__lsfound=no
        for ((i=0;$i<${#__kbwbashrc__lsarray[*]};i=$i+1)) ; do
            if ${__kbwbashrc__lsarray[$i]} --version 2>/dev/null | grep 'GNU coreutils' &>/dev/null ; then
                dprint "- found GNU ls: ${__kbwbashrc__lsarray[$i]}"
                alias ls="${__kbwbashrc__lsarray[$i]} --color -F"
                alias ll="${__kbwbashrc__lsarray[$i]} --color -F -lh"
                __kbwbashrc__lsfound=yes
                break
            fi
        done
        if [ "$__kbwbashrc__lsfound" == no ] ; then
            if ls -F &>/dev/null ; then
                dprint "- POSIX ls"
                alias ls='ls -F'
                alias ll='ls -lhF'
            else
                alias ll='ls -lh'
            fi
        fi
        unset __kbwbashrc__lsarray __kbwbashrc__lsfound
    fi
    if [[ $OSTYPE == linux* ]] ; then
        # actually, just Debian, but this works for now
        alias gv="gv --watch --antialias"
    else
        alias gv="gv -watch -antialias"
    fi
    if have gsed ; then
        alias sed=gsed
    elif have gnused ; then
        alias sed=gnused
    fi
    if have gmake ; then
        alias make=gmake
    elif have gnumake ; then
        alias make=gnumake
    fi
    # Detect the kind of grep
    have ggrep && alias grep=ggrep
    # Detect the kind of ps
    if [[ $OSTYPE == darwin* ]] ; then
        _PSARGS="-a -w -w -x"
    elif [[ $OSTYPE == linux* ]] ; then
        _PSARGS="awx"
    elif ps awx &>/dev/null ; then
        _PSARGS="awx"
    elif ps -A -f &>/dev/null ; then
        # note not $OSTYPE == solaris* because it might be GNU ps
        _PSARGS="-A -f"
    else
        _PSARGS="ax"
    fi
    eval "function psq { ps ${_PSARGS} | grep -i \"\$@\" | grep -v grep ; }"
    unset _PSARGS
    alias macfile="perl -e 'tr/\x0d/\x0a/'"
    have tidy && alias tidy='tidy -m -c -i'
    have vim && alias vi='vim'
    [ -x /Applications/Vim.app/Contents/MacOS/Vim ] && alias vim=/Applications/Vim.app/Contents/MacOS/Vim
    alias vlock='vlock -a'
    alias fastscp='scp -c arcfour -o Compression=no' # yay speed!
    alias startx='nohup ssh-agent startx & exit'
    alias whatlocale='printenv | grep ^LC_'
    alias fixx='xauth generate $DISPLAY'
    alias whatuses='fuser -v -n tcp'
    alias which=type
    have watch || source ~/.conf/watch.sh
    alias ssh='env TERM="$TERM:$LANG" ssh'
    have realpath || alias realpath=realpath_func
    if have readlink ; then
        unset -f readlink_func
    else
        alias readlink=readlink_func
    fi
    if [[ $OSTYPE == darwin* ]]; then
        alias top='top -R -F -ocpu -Otime'
        if have mvim ; then
            alias cvim=mvim
            alias gvim=mvim
        else
            alias cvim=exec_cvim
            alias gvim=exec_cvim
        fi
    fi
    if ! have uxterm ; then
        have xterm && alias uxterm="~/.conf/uxterm"
    fi
fi

# useful definitions
completion_options=(
/etc/bash_completion
/opt/local/etc/bash_completion
~/.conf/bash_completion
)
if [[ $BASH_VERSION && -z $BASH_COMPLETION && $- == *i* ]] ; then
    bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
    if [ $bmajor -eq 2 -a $bminor '>' 04 ] || [ $bmajor -gt 2 ] ; then
        for bc in "${completion_options[@]}" ; do
            if [[ -r $bc ]] ; then
                dprint Loading the bash_completion file $bc
                [[ ${BASH_COMPLETION} ]] || BASH_COMPLETION="$bc"
                #COMP_CVS_REMOTE=yes
                export COMP_CVS_ENTRIES=yes
                source "$bc"
                break
            fi
        done
    fi
    unset bash bminor bmajor
fi
unset completion_options
# I want to keep this function available, but it gets undefined
# by the standard bash_completion files.
function have { type "$1" &>/dev/null ; }

dprint Checking for bashrc.local in $HOME
if [[ -r ${HOME}/.bashrc.local ]]; then
    dprint - loading local bashrc
    source "${HOME}/.bashrc.local"
fi

if [[ $TMOUT && $- == *i* ]]; then
    echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    echo You will be autologged out after:
    echo -e -n '\t'
    seconds=$TMOUT
    days=$((seconds/60/60/24))
    seconds=$((seconds-days*24*60*60))
    hours=$((seconds/60/60))
    seconds=$((seconds-hours*60*60))
    minutes=$((seconds/60))
    seconds=$((seconds-minutes*60))
    [[ $days != 0 ]] && echo -n "$days days "
    [[ $hours != 0 ]] && echo -n "$hours hours "
    [[ $minutes != 0 ]] && echo -n "$minutes minutes "
    [[ $seconds != 0 ]] && echo -n "$seconds seconds "
    echo
    echo ... of being idle.
    unset days hours minutes seconds
fi
unset -f verify_path
[[ $OSTYPE == solaris* ]] || unset KBW_SYSTEM_BASHRC KBW_SYSTEM_PROFILE

dprint BASHRC_DONE
# vim:set ft=sh