# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
  . /etc/bashrc
fi

# 新しく作られたファイルのパーミッションがつねに 644 になるようにする
umask 022
# core ファイルを作らせないようにする
ulimit -c 0

export GTK_IM_MODULE=ibus
export XMODIFIERS=@im=ibus
export QT_IM_MODULE=ibus

# 非対話モードの場合は終了
if [[ ! "${PS1}" ]] ; then
  return
fi

########################
# 環境ごとの設定
########################
if [[ "${MSYSTEM}" ]] ; then
  export COPY_CLIP='eval nkf -s | clip'
  alias peco='percol'
  GIT_PROMPT=/usr/share/git/completion/git-prompt.sh
  export DISPLAY=localhost:0.0
elif [[ "$(uname)" == 'Darwin' ]]; then
  export COPY_CLIP='pbcopy'
  GIT_PROMPT=/usr/local/etc/bash_completion.d/git-prompt.sh
  if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
  fi
else
# elif [[ "$(expr substr $(uname -s) 1 5)" == 'Linux' ]]; then
  export COPY_CLIP='xclip -i -sel clip'
  GIT_PROMPT=/usr/share/git-core/contrib/completion/git-prompt.sh
fi

########################
# 基本的な設定
########################
export PAGER='less -m -i'

# EOF (Ctrl-D) 入力は 10回まで許可。
IGNOREEOF=10
# 履歴のサイズ
HISTSIZE=1000
# 履歴ファイルを上書きではなく追加する。
shopt -s histappend
# 実行するまえに必ず展開結果を確認できるようにする。
shopt -s histverify 
# 履歴の置換に失敗したときやり直せるようにする。
shopt -s histreedit
#履歴の重複を削除
export HISTCONTROL=erasedups		#ignoredups,ignorespace,erasedups

bind '"\C-n": history-search-forward'
bind '"\C-p": history-search-backward'

########################
# aliasの設定
########################
alias less='less -i'
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# alias for misc
alias grep='grep --color'
# alias for some shortcuts for different directory listings
if [[ "$(uname)" == 'Darwin' ]]; then
  export LSCOLORS=gxfxcxdxbxegedabagacad
  alias ls='ls -G'
else
  export LS_COLORS='di=01;36'
  alias ls='ls -hF --color=auto --show-control-chars'
fi
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'
# alias cdd='cd $(find . -maxdepth 1 -type d | peco)'
alias cdd='cd'
_cdd() {
  _cd
  COMPREPLY=($((IFS=$'\n'; echo "${COMPREPLY[*]}") | peco))
}
complete -F _cdd cdd


########################
# pecoで履歴をインクリメントサーチ
########################
peco-select-history() {
  declare l=$(HISTTIMEFORMAT= history | sort -k1,1nr | perl -ne 'BEGIN { my @lines = (); } s/^\s*\d+\s*//; $in=$_; if (!(grep {$in eq $_} @lines)) { push(@lines, $in); print $in; }' | peco --query "$READLINE_LINE")
  READLINE_LINE="$l"
  READLINE_POINT=${#l}
}
bind -x '"\C-r": peco-select-history'

########################
# git
########################
gitps1=true
if [[ -r ${GIT_PROMPT} ]]; then
  . ${GIT_PROMPT}
  gitps1="__git_ps1"
fi 
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUPSTREAM=auto

if [[ "${MSYSTEM}" ]] ; then
  export PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`${gitps1}`\[\033[0m\]\n$ '
else
  export PS1='[\u@\h \[\033[33m\]\W\[\033[36m\]$(${gitps1})\[\033[0m\]]\$ '
fi
# デフォルト
# '[\u@\h \W]\$ '

########################
# ssh agent
########################
agent="$HOME/.ssh/agent"
if [ -S "$SSH_AUTH_SOCK" ]; then
    case $SSH_AUTH_SOCK in
    /tmp/*/agent.[0-9]*)
        ln -snf "$SSH_AUTH_SOCK" $agent && export SSH_AUTH_SOCK=$agent
    esac
elif [ -S $agent ]; then
    export SSH_AUTH_SOCK=$agent
fi

if [[ ! "${TMUX}" ]] && type -a tmux > /dev/null 2>&1; then
  tmux new-session 
fi
