##  .bashrc for euske
##

# invoke minimum settings
. $HOME/rc/bashrc_minimum

if [[ "$PS1" ]]; then
  # Interactive mode

  # PATH
  PATH=~/bin:~/bin/python:$PATH
  PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin

  # Shell options (override)
  HISTSIZE=100000
  HISTFILESIZE=100000

  # Env vars
  if [ -x /usr/bin/vim ]; then
      export EDITOR=vim
  fi
  if [ -x /usr/bin/jless ]; then
      export PAGER=jless
  fi
  if [ -x $HOME/bin/lesspipe.sh ]; then
      export LESSOPEN="| $HOME/bin/lesspipe.sh %s"
  fi
  export LC_CTYPE=ja_JP.EUC-JP
  export JLESSCHARSET=japanese-euc
  export PYTHONPATH=.
  export PYTHONSTARTUP=$HOME/rc/pythonrc.py
  export LYNX_CFG=$HOME/rc/lynx.cfg
  export LYNX_LSS=$HOME/rc/lynx.lss
  export MYHISTFILE=$HOME/.bash_myhistory
  export RSYNC_BACKUP_BASE='.old'
  export RSYNC_PATTERN
  RSYNC_PATTERN='--exclude NOBACKUP/ --exclude INACTIVE/ --exclude LOCAL/ --exclude local/'
  RSYNC_PATTERN="$RSYNC_PATTERN --exclude tmp/ --exclude .\*"
  RSYNC_PATTERN="$RSYNC_PATTERN --exclude \*.o --include \*.obj' --include \*.Z"

  # Terminal setting
  if [ "linux" = "$TERM" ]; then
      echo -en '\e]P0000000' # black
      echo -en '\e]P1ff4500' # orangered
      echo -en '\e]P200ff00' # green
      echo -en '\e]P3ffd700' # gold
      echo -en '\e]P40000ff' # blue
      echo -en '\e]P5ffbbff' # plum1
      echo -en '\e]P687ceeb' # skyblue
      echo -en '\e]P7ffffff' # white
      echo -en '\e]P8222222' # darkgrey
      echo -en '\e]P9982b2b' # red
      echo -en '\e]PA89b83f' # green
      echo -en '\e]PBefef60' # yellow
      echo -en '\e]PC2b4f98' # blue
      echo -en '\e]PD826ab1' # magenta
      echo -en '\e]PEa1cdcd' # cyan
      echo -en '\e]PFdedede' # lightgray
  fi

  # Prompt setting
  function screen_status {
    if [[ "screen" = "$TERM" ]]; then echo -en "\033k$HOSTNAME:$1\033\0134"; fi
  }
  function show_exit {
    if [[ $1 -eq 0 ]]; then return; fi
    echo -e "\007exit $1"
  }
  function log_history {
    echo "`date '+%Y-%m-%d %H:%M:%S'` $HOSTNAME:$$ $PWD ($1) `history 1`" >> $MYHISTFILE
  }
  function prompt_cmd {
    local s=$?
    show_exit $s; 
    log_history $s;
    screen_status "${PWD/$HOME/~}"
  }
  function end_history {
	log_history $?;
	echo "`date '+%Y-%m-%d %H:%M:%S'` $HOSTNAME:$$ $PWD (end)" >> $MYHISTFILE
  }
  echo "`date '+%Y-%m-%d %H:%M:%S'` $HOSTNAME:$$ $PWD (start)" >> $MYHISTFILE
  trap end_history EXIT
  PROMPT_COMMAND=prompt_cmd

  case "$PROMPT_COLOR" in
	red*) col=31;;
	green*) col=32;;
	yellow*) col=33;;
	blue*) col=34;;
	pink*) col=35;;
	cyan*) col=36;;
	*) col=1;;
  esac

  PS0="\t $HOSTNAME:\[\e[${col}m\]\W[\!]\$\[\e[m\] "
  PS1="\t $HOSTNAME\[\e[${col}m\]\w[\!]\$\[\e[m\] "
  
  # Funcitons
  # cd
  . ~/rc/cdhist.sh

  # grep
  function GG { command tf py sh c cc C cpp h H hpp | LC_CTYPE=C xargs grep "$@"; }
  function gg { command tf py sh c cc C cpp h H hpp | LC_CTYPE=C xargs grep -i "$@"; }
  function gt { command tf txt tex texi html htm xml | LC_CTYPE=C xargs grep -i "$@"; }

  # ssh & rsync
  function scp2 { sshon && command scp "$@"; }
  function ssh2 { sshon && command ssh "$@"; }
  function sftp2 { sshon && command sftp "$@"; }
  function rget { sshon && command homesync.py get "$@"; }
  function rput { sshon && command homesync.py put "$@"; }
  function rs { if [ $# -eq 0 ]; then echo 'usage: rs [host:]src1 [host:]src2 ... [host:]dest';
    else sshon && rsync -auvbz --delete $RSYNC_PATTERN "$@"; fi }

  # misc.
  function px { local tmp=$PS1; PS1=$PS0; PS0=$tmp; }
  function e { command emacs -nw "$@"; }
  function i { if [ "$1" ]; then grep "$1" $MYHISTFILE; else tail -100 $MYHISTFILE; fi }
  function formv {
    if [ $# -lt 2 ]; then echo 'usage: formv s/pat1/pat2/ file ..'; 
    else s=$1; shift; for i in "$@"; do j=`echo "$i" | sed "$s"`; echo "$i -> $j"; mv -i "$i" "$j"; done;
    fi
  }
  function wi {
    case `type -t "$1"` in
      alias|function) type "$1";;
      file) lt `command -v "$1"`;;
      function) type "$1";;
    esac
  }
  complete -c wi
  
fi
