# Source .bashrc for non-interactive Bash shells
if [[ $- != *i* ]] ; then
  # Shell is non-interactive.  Be done now!
  return
fi

export BASH_ENV=~/.bashrc
echo "shared "

THISOS=$(uname | awk '{print tolower($1)}')
THISHOST=$(uname -n)

_setaliases() {
  case "$THISOS" in
    darwin)
        # Use MacVim's terminal vim for awesomeness support
        hash rvim 2>/dev/null  && alias vim=rvim
        local FIND_EGREP="-E .";
        local LS_COLORFLAG='-G'
        ;;
    linux)
        alias ls='ls -G'
        local FIND_EGREP=". -regextype posix-egrep";
        local LS_COLORFLAG='--color=auto'
        ;;
  esac

  alias ls="ls $LS_COLORFLAG"
  alias ll='ls -hl'
  alias l.='ls -d .* --color=auto'
  alias tree='tree -C'

  alias cruft="find $FIND_EGREP -regex '.*swo|.*swp|.*pyc|.*pyo|.*~' -exec rm {} \;"

  alias p="ps aux |grep "
  alias grep="grep --color=auto"

  alias facts="echo -ne '\033[36m'; curl -s randomfunfacts.com | grep '<i>' | sed 's/.*<i>\(.*\)<\/i>.*/\1/'; echo -ne '\033[0m'; tput sgr0"

  # show numeric permissions
  local FORMATFLAG="-c"
  if ( uname -a | grep Darwin >/dev/null); then
    FORMATFLAG="-f"
  fi
  alias perms="stat $FORMATFLAG '%A %a %n' *"

  alias .bashrc='source ~/.bashrc'

  alias sha256='shasum -a 256'


  alias path='echo -e ${PATH//:/\\n}'
  alias GitPull='/zz/zShared/sbin/GitPull'


  alias zansible-playbook='docker run -it --rm -v $(pwd):/ansible ${ZANSIBLE}:latest ansible-playbook'
  alias zansible-lint='docker run -it --rm -v $(pwd):/ansible ${ZANSIBLE}:latest ansible-lint'
}


_setpath() {
  local paths=(
    /usr/local/bin
    /usr/local/sbin
    /usr/local/sqlite/bin
    /zz/zShare/bin
  )

  local i
  for i in ${paths[@]}; do
    # Move these paths to the front
    PATH=$(echo $PATH | sed -e "s#$i##g")
    if [ -d $i ]; then
      PATH=$i:$PATH
    fi
  done

  PATH=`echo $PATH | sed -e 's/^\://' -e 's/\:\:/:/g'`

  export PATH
}

_setldpath() {
  local paths=(
    /usr/local/sqlite/lib
  )

  local i
  for i in ${paths[@]}; do
    # Move these paths to the front
    LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | sed -e "s#$i##g")
    if [ -d $i ]; then
      LD_LIBRARY_PATH=$i:$LD_LIBRARY_PATH
    fi
  done

  LD_LIBRARY_PATH=`echo $LD_LIBRARY_PATH | sed -e 's/^\://' -e 's/\:\:/:/g'`

  export LD_LIBRARY_PATH
}

_setldrunpath() {
  local paths=(
    /usr/local/sqlite/lib
  )

  local i
  for i in ${paths[@]}; do
    # Move these paths to the front
    LD_RUN_PATH=$(echo $LD_RUN_PATH | sed -e "s#$i##g")
    if [ -d $i ]; then
      LD_RUN_PATH=$i:$LD_RUN_PATH
    fi
  done

  LD_RUN_PATH=`echo $LD_RUN_PATH | sed -e 's/^\://' -e 's/\:\:/:/g'`

  export LD_RUN_PATH
}

_setprompt() {
  local SAVEHISTORY="history -a"
  local SETWINDOWTITLE='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'

  local TMUXCMD=''
  if [ -n $TMUX ]; then
    local TMUXENV='tmux set-environment -g CWD "$PWD"'
    local TMUXPATH='tmux set-option default-path $PWD'
    local TMUXCMD="($TMUXENV 2>/dev/null && $TMUXPATH 2>/dev/null >&2)"
  fi

  export PROMPT_COMMAND="$SETWINDOWTITLE;$SAVEHISTORY;$TMUXCMD"

  # Reset
  local Color_Off='\[\e[0m\]'       # Text Reset

  # Regular Colors
  local Black='\[\e[0;30m\]'        # Black
  local Red='\[\e[0;31m\]'          # Red
  local Green='\[\e[0;32m\]'        # Green
  local Yellow='\[\e[0;33m\]'       # Yellow
  local Blue='\[\e[0;34m\]'         # Blue
  local Purple='\[\e[0;35m\]'       # Purple
  local Cyan='\[\e[0;36m\]'         # Cyan
  local White='\[\e[0;37m\]'        # White
  local NEWLINE="\n"

  # Use for chroots, venvs, or other info that should be stuck in the prompt
  EXTRAPROMPT=$2

  case "$1" in
    simple)
      export PS1="\[\[\e[32;1m\]\h$EXTRAPROMPT \W> \[\e[0m\]"
      ;;
    2line)
      # Default PROMPT_COLOR values
      : ${PROMPT_COLOR:=Yellow}
      : ${PROMPT_COLOR2:=Blue}
      local C1=${!PROMPT_COLOR}
      local C2=${!PROMPT_COLOR2}

      # ┌(jer@myhost)─(✗)─(10:18 PM Sun Apr 14)
      # └─(~/dev/git/myproject)─>
      local DASH="\342\224\200"
      local X="\342\234\227"
      local ERRCODE="\$([[ \$? != 0 ]] && echo \"${DASH}(${Red}${X}${White})\")${DASH}"

      LINE1="${White}\342\224\214(${C1}\u@\h$EXTRAPROMPT${White})${ERRCODE}(${C1}\@ \d${White})"
      # local LINE2="\342\224\224\342\224\200(${C2}\w${White})-> "
      local LINE2="\342\224\224\342\224\200(${C1}\w${White})-> "
      export PS1="${NEWLINE}${LINE1}${NEWLINE}${LINE2}${Color_Off}"
      ;;
    3line)
      _setprompt 2line
      export PS1="$PS1${NEWLINE}${White}\342\224\224\342\224\200>${Color_Off} "
      ;;
  esac
}


_sethistory() {
  export HISTFILE=~/.bash_history
  export HISTSIZE=10000
  export HISTFILESIZE=${HISTSIZE}
  export HISTCONTROL=ignoredups:ignorespace
  shopt -s histappend

  # Do *not* append the following to our history:
  HISTIGNORE='\&:fg:bg:ls:pwd:cd ..:cd ~-:cd -:cd:jobs:set -x:ls -l:ls -l'
  HISTIGNORE=${HISTIGNORE}':%1:%2:popd:top:shutdown*'
  export HISTIGNORE

  # Save multi-line commands in history as single line
  shopt -s cmdhist
}

_manpagecolor() {
  export LESS_TERMCAP_mb=$'\E[01;31m'
  export LESS_TERMCAP_md=$'\E[01;31m'
  export LESS_TERMCAP_me=$'\E[0m'
  export LESS_TERMCAP_se=$'\E[0m'
  export LESS_TERMCAP_so=$'\E[01;44;33m'
  export LESS_TERMCAP_ue=$'\E[0m'
  export LESS_TERMCAP_us=$'\E[01;32m'
}

_setpath
_setldpath
_setldrunpath
_setaliases
_setlocalaliases
_setprompt 2line
_sethistory
_manpagecolor

export EDITOR=vi

# Correct minor spelling errors in cd commands
shopt -s cdspell
# Enable egrep-style pattern matching
shopt -s extglob
shopt -s checkwinsize

export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
export TERM=xterm-256color
export GOPATH=${HOME}/dev/go

export ANSIBLE_STDOUT_CALLBACK=yaml
export GPODDER_HOME=
export GPODDER_DOWNLOAD_DIR=

PATH=${PATH}:.local/bin ; export PATH
export SIGNAL_PHONE_NUMBER=

