# If not running interactively, then exit
if [ -n "$PS1" ]; then

test -f ~/.bashrc_site_before && source ~/.bashrc_site_before
test -f /sw/bin/init.sh && . /sw/bin/init.sh
source ~/.aliases

# speeding up text selection: The colon is a no-op for the Bourne family
# of shells. So just select whole lines and paste; with conventional
# prompts you have to be careful not to select the prompt as well.

set_prompt () {
    export PS1=": \u@\h \W; "
    export PS2=":; "
}

# append commands from all terminals into the history file immediately
shopt -s histappend
PROMPT_COMMAND='history -a'

stty sane 2>/dev/null
stty erase ^?
umask 022

IGNORE_EOF=1

export LESSCHARSET=iso8859
export EDITOR=vim
export CVSEDITOR=vim
export CVS_RSH=ssh
export PAGER=less
export TEST_MODE=1
export LC_COLLATE=C
export DISPLAY=:0.0
export TEST_AUTHOR=1
export PROJROOT
export PTAGSFILE=~/.ptags

export CPPFLAGS
for d in /{sw,usr/local}/include
do
    test -d $d && CPPFLAGS="-I$d $CPPFLAGS"
done

export LDFLAGS
for d in /{sw,usr/local}/lib
do
    test -d $d && LDFLAGS="-L$d $LDFLAGS"
done

# remove domain name from hostname, if domain name
# is in hostname (otherwise, just hostname).
export HOSTNAME=`hostname | sed s/\\\..\*//g`

export PATH
export MANPATH
for d in /usr /{usr,opt}/{local,share,local/share} /usr/local/gwTeX ~{,/home,/perl}
do
    test -d $d/bin && PATH=$d/bin:$PATH
    test -d $d/man && MANPATH=$d/man:$MANPATH
done

LESSPIPE=`which lesspipe.sh`
[[ $LESSPIPE ]] && export LESSOPEN="|$LESSPIPE %s"

IGNOREEOF=1  # so first ^D doesn't logout, second ^D does

cd () {
    builtin cd "$*"
    ls --color=auto
}

(which ncftp >/dev/null) && alias ftp=ncftp


# vim: If args are given, open those files. If no arg is given, use
# viminfo to jump to last known cursor position

vi () { vim ${@:- -c "normal '0"}; }

# perl module development
cdpm () { cd $(dirname $(pmpath $1)); }

pv () {
    perl -MCPAN -e"print CPAN::Shell->format_result(q{Module}, q{$1})"
}

# so you can do
#   vit Foo::Bar::Baz
# to edit the class
#   cdt Foo::Bar::Baz
# to cd to the class's directory, we use vim tags.
# We also have custom completion rules for cdt and vit in
# .bash_completion.

cdt () { cd `cdt.pl $1`; }


targets () { perl -ne 'print "$1\n" if /^(\w+):/' Makefile; }

# Check for recent enough version of bash.
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}

# Check for interactive shell.
if [ -n "$PS1" ]; then
  if [ $bmajor -eq 2 -a $bminor '>' 04 ] || [ $bmajor -gt 2 ]; then
    if [ -r /etc/bash_completion ]; then
      # Source completion code.
    . /etc/bash_completion/bash_completion
    . /etc/bash_completion/contrib/svk
    fi
  fi
fi
unset bash bminor bmajor


# list all instances of one or more files found within the $PATH.

wh () {
    perl -MFile::Which -e'
        for $file (@ARGV) {
            print "$_\n" for grep { !$seen{$_}++ } which($file)
        }
    ' $*;
}

projptags () {
    rm -f $PTAGSFILE
    ptags --use --exclude ~/.ptags_exclude $(find $PROJROOT -mindepth 1 -maxdepth 1 -type d -not -name [._]\* | perl -pe'chomp; $_ = "--lib $_ "') >>$PTAGSFILE
    ptags --perllib --exclude ~/.ptags_exclude >>$PTAGSFILE

    if [ -f $PTAGS_DYNAMIC ]; then
        echo Generating dynamic ptags
        $PTAGS_DYNAMIC >>$PTAGSFILE
    fi

    sort -o $PTAGSFILE $PTAGSFILE
}


ORIGPATH=$PATH

set_project () {
    export PROJROOT=$1
    export PERL5OPT="-MDevel::SearchINC::FindLib=$PROJROOT"
    export CF_CONF=local

    PATH=$ORIGPATH
    for i in $(distfind)
    do
        if [ -d $i/bin ]; then
            PATH=$PATH:$i/bin
        fi
    done
}



set_prompt
test -f ~/.bashrc_site_after && . ~/.bashrc_site_after

fi
