#!/bin/bash		### Honu's .bashrc

PRINTER=lp
VISUAL="emacs -nw"
EDITOR="emacs -nw"
BROWSER=firefox:iceweasel:chromium-browser:google-chrome
GEM_HOME=$HOME/.gems

export VISUAL EDITOR BROWSER

appendToPath () {
    if [ -d $1 ]; then
	[[ :$PATH == *:$1:* ]] || PATH+=:$1
    fi
}
prependToPath () {
    if [ -d $1 ]; then
	[[ :$PATH == *:$1:* ]] || PATH=$1:$PATH
    fi
}

appendToPath /opt/bin
prependToPath $HOME/bin
prependToPath $HOME/Honu/bin
for d in `ls -d $HOME/Honu/local/{bin,[a-z]*_bin} 2>/dev/null`; do prependToPath $d; done
for d in /opt/java/bin /usr/local/java/bin; do prependToPath $d; done

export PATH

if [ "$PS1" ]; then
  # We're in an interactive shell.  Start by changing the prompt:

  PS1='(\h $USER\W \!) '

  [[ -z $WS ]] || ws="WS=$WS "
  case "$TERM" in
      xterm*)
	  PS1='\[\033]1;\h:\u\]\[\033]2;$ws\w $USER@\h\](\h \W \!) '
	  # the following shows the currently-running command in the xterm title bar:
	  PS0='\033]2;$ws\w $(history 1 | sed "s/^[ ]*[0-9]*[ ]*//g")\007'
	  ;;
  esac
  
  export ignoreeof=0

  if [[ $WS ]]; then
      # If we know what workspace this terminal is on, give it its own history file
      HISTFILE=$HOME/.bash_history_$WS
      echo WS=$WS
  fi

  # At some point I will move the aliases and functions to .aliases,
  # which would let them be used by zsh

  getmail () {
      fetchmail | grep -v skipping | grep -v 'No mail' \
	  >> /home/steve/logs/fetchmail.log
  }
  
  # , f -> cd. ../$1   Note that ".." is now just a cd (because of shopt -s autocd)
  ,() {
      cd. ../$1; dirs; ls -aCF --color=auto
  }

  # Bash functions with conditionals confuse bash completion, so unset them.
  #  Otherwise filename completion will cause errors.  They look like:
  #  .: syntax error: operand expected (error token is ".")
  unset -f .
  unset -f cd.
  source /etc/bash_completion

  # cd. cd's to the *real* current directory to make .. work properly
  cd.() {
      if [[ -z "$1" ]]; then cd -P .; else cd -P "$1"; fi
  }

  # . f - functions as source (the usual definition) if f is an ordinary file,
  #       if f is a directory, cd -P to it and print useful info.
  .() {
     if [[ -z "$1" ]]; then 
	cd -P .; dirs; ls -aCF --color=auto
     elif [[ -d "$1" ]]; then
	cd -P "$1"; dirs; ls -aCF --color=auto
     else
	source $1
     fi
  }

  # rather than redefining .. we simply set autocd, which makes every directory name
  # a cd command.
  shopt -s autocd

  alias a="audacity --sync"
  alias h=history
  alias j=jobs
  alias p=pushd
  alias P=popd
  alias gf=grep-find
  alias gfs='grep-find -s'
  alias gg='git grep -n'
  alias ll='ls -alF --color=auto'
  alias lr='ls -aRF --color=auto'
  alias l='ls -aCF --color=auto'
  alias lp2s='lp -o sides=two-sided-long-edge'
  alias chrome="google-chrome >& /dev/null"
  alias gnus="emacs -f gnus -name gnus"
  alias pidgin="pidgin -f"
  
  # Chain in local/[a-z]*_aliases, which might have been put there by Myrtle et. al.
  for f in `ls $HOME/Honu/local/[a-z]*_aliases 2>/dev/null`; do source $f; done
  
  # make an alias for frm in case it's missing.  No use in scripts, of course.
  [ -x /usr/bin/frm ] || [ -x /usr/local/bin/frm ] \
      || alias frm='echo x | mail| tail +2'
fi

# Chain in local/[a-z]*_bashrc, which might have been put there by Myrtle et. al.
for f in `ls $HOME/Honu/local/[a-z]*_bashrc 2>/dev/null`; do source $f; done

### Here we have the local package repos for an assortment of scripting languages.
#
#   Currently we have nvm, golang, and ruby gems

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# if we have go installed in /usr/local or our home directory instead of in /usr/bin,
# set GOHOME and prepend its bin directory to PATH (prepend because we may want to
# override the installed version)
if [ -d $HOME/golang/go ]; then
    export GOHOME=$HOME/golang/go
    prependToPath $GOHOME/bin
elif [ -d /usr/local/go ]; then
    prependToPath /usr/local/go/bin
fi

# we don't have to specify GOPATH, because ~/go is the default, but we
# do have to add ~/go/bin to PATH in order to find the programs we
# build there.  We don't have to test for bin's existence here because
# prependToPath does that.
prependToPath $HOME/go/bin

# Install Ruby Gems to ~/gems (not clear this actualy works)
export GEM_HOME=$HOME/gems
prependToPath $GEM_HOME/bin

# probably need to use ruby/*/bin in the general case
prependToPath /home/steve/.gem/ruby/2.5.0/bin

# Console xterm on my ctwm dashboard.
[ x"$CONSOLE" != x ] && { unset CONSOLE ; $HOME/bin/idle& }

