# dotfiles .bashrc

# if not running interactively, don't do anything
[ -z "$PS1" ] && return

# source global bashrc
[ -f /etc/bashrc ] && . /etc/bashrc

# set prompt
case "$TERM" in
xterm*|rxvt*|cygwin*|ansi*|vt10*)
    PS1='\033]0;\H:$PWD\007\033[7m\u@\H: $PWD\033[m $?\n\$ '
    ;;
*)
    PS1='\u@\H: $PWD $?\n\$ '
    ;;
esac

export EDITOR=vim
export VISUAL=vim
export PAGER="/bin/sh -c \"col -b -x | view -c 'set ft=man nomod nolist' -c 'map q :q<CR>' -c 'map <SPACE> <C-D>' -c 'map b <C-U>' -\""
export MANPAGER="/bin/sh -c \"col -b -x | view -c 'set ft=man nomod nolist' -c 'map q :q<CR>' -c 'map <SPACE> <C-D>' -c 'map b <C-U>' -\""
export BROWSER=firefox
export PYTHONSTARTUP=~/.pythonrc
export GZIP="-9"
export GREP_OPTIONS='--color=auto'
# colorize directory listings (works on both BSD and GNU ls)
if `ls --version 2>/dev/null | grep -iq "free software foundation"` ; then
    # GNU ls
    eval "`dircolors 2>/dev/null`"
    export LS_OPTIONS='--color=auto'
else
    # BSD ls 
    export CLICOLOR=''
fi

# Add user's private directories if they exist.
if [ -d ~/bin ] ; then
    PATH=~/bin:"${PATH}"
fi
if [ -d ~/man ] ; then
    PATH=~/man:"${PATH}"
fi
if [ -d ~/info ] ; then
    PATH=~/info:"${PATH}"
fi

# file permissions: rwxr-xr-x
umask 022

export HISTSIZE=10000
export HISTIGNORE="history:h:.dotfiles"
# ingore duplicate adjacent items
export HISTCONTROL=ignoreboth
# this allows editing of multi-line commands.
shopt -s cmdhist
# multiple shells append to history file rather than overwriting each other.
shopt -s histappend
# check the window size after each command and,
# if necessary, update the values of LINES and COLUMNS
shopt -s checkwinsize
# allow .dotfile to be returned in path-name expansion
shopt -s dotglob

# don't exit shell on CTRL-D
set -o ignoreeof

# Turn on extended globbing and programmable completion
shopt -s extglob progcomp
# I could put 'scp' in this list but 95% of the time I'm copying a
# local file to a remote host and I want completion on the local filename.
complete -A hostname ssh telnet ftp ping fping host dig nmap
complete -A helptopic help
complete -d mkdir rmdir cd du find pushd
complete -f -X '!*.?(t)bz?(2)' bunzip2 bzcat bzcmp bzdiff bzegrep bzfgrep bzgrep 
complete -f -X '!*.@(zip|ZIP|jar|JAR|exe|EXE|pk3|war|wsz|ear|zargo|xpi|sxw|ott)' unzip zipinfo
complete -f -X '*.Z' compress znew
complete -f -X '!*.@(Z|gz|tgz|Gz|dz)' gunzip zcmp zdiff zcat zegrep zfgrep zgrep zless zmore 
complete -f -X '!*.@(Z|gz|tgz|Gz|bz2|tar)' tar
complete -f -X '!*.Z' uncompress
complete -f -X '!*.@(gif|jp?(e)g|tif?(f)|png|p[bgp]m|bmp|x[bp]m|rle|rgb|pcx|fits|pm|GIF|JPG|JP?(E)G|TIF?(F)|PNG|P[BGP]M|BMP|X[BP]M|RLE|RGB|PCX|FITS|PM)' xv zgv xzgv
complete -f -X '!*.@(@(?(e)ps|?(E)PS|pdf|PDF)?(.gz|.GZ|.bz2|.BZ2|.Z))' gv ggv kghostview
complete -f -X '!*.@(dvi|DVI)?(.@(gz|Z|bz2))' xdvi
complete -f -X '!*.@(dvi|DVI)' dvips dviselect dvitype kdvi dvipdf advi
complete -f -X '!*.@(pdf|PDF)' acroread gpdf xpdf kpdf
complete -f -X '!*.@(?(e)ps|?(E)PS)' ps2pdf
complete -f -X '!*.texi*' makeinfo texi2html
complete -f -X '!*.@(?(la)tex|?(LA)TEX|texi|TEXI|dtx|DTX|ins|INS)' tex latex slitex jadetex pdfjadetex pdftex pdflatex texi2dvi
complete -f -X '!*.@(mp3|MP3)' mpg123 mpg321 madplay
complete -f -X '!*.@(mp?(e)g|MP?(E)G|wma|avi|AVI|asf|vob|VOB|bin|dat|vcd|ps|pes|fli|flv|viv|rm|ram|yuv|mov|MOV|qt|QT|wmv|mp3|MP3|ogg|OGG|ogm|OGM|mp4|MP4|wav|WAV|asx|ASX|mng|MNG)' xine kaffeine mplayer
complete -f -X '!*.@(mpg|mpeg|avi|mov|qt)' xanim
complete -f -X '!*.@(ogg|OGG|m3u|flac|spx)' ogg123
complete -f -X '!*.@(mp3|MP3|ogg|OGG|pls|m3u)' gqmpeg freeamp
complete -f -X '!*.fig' xfig
complete -f -X '!*.@(exe|EXE|com|COM|scr|SCR|exe.so)' wine
complete -f -X '!*.@(?([xX]|[sS])[hH][tT][mM]?([lL]))' netscape mozilla lynx opera galeon curl dillo elinks amaya moz
# user commands see only users
complete -u su usermod userdel passwd chage write chfn groups slay w
# group commands see only groups
[ -n "$bash205" ] && complete -g groupmod groupdel newgrp 2>/dev/null
# bg completes with stopped jobs
complete -A stopped -P '%' bg
# other job commands
complete -j -P '%' fg jobs disown
# readonly and unset complete with shell variables
complete -v readonly unset
# set completes with set options
complete -A setopt set
# shopt completes with shopt options
complete -A shopt shopt
# unalias completes with aliases
complete -a unalias
# type and which complete on commands
complete -c command type which
# builtin completes on builtins
complete -b builtin

# source aliases
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# source local settings
if [ -f ~/.bashrc_local ]; then 
    . ~/.bashrc_local
fi

#This hangs on some systems not running X.
# add user font path
#xset fp+ ~/.fonts > /dev/null 2>&1
## reload X11 .Xresources
#if [ -f ~/.Xresources ]; then
#    xrdb -merge ~/.Xresources > /dev/null 2>&1
#fi

# Put this in ~/.bashrc_local if necessary.
## LD_LIBRARY_PATH is a necessary evil.
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/lib/mysql

# Put this in ~/.bashrc_local if you want auto updating dotfiles.
# get the latest dotfiles if older than 7 days.
# TODO should also test age of tarball on server.
# oh man, this might be a bad idea...
#BASHRC_AGE_IN_SECS=$[`date +%s` - `stat -c %Y ~/.dotfiles_install`]
#if [ $BASHRC_AGE_IN_SECS -gt 604800 ]; then
#    echo "UPDATING DOTFILES"
#    (~/.dotfiles 2>&1 >/dev/null) &
#fi

