# .bashrc

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

# User specific aliases and functions
if [ -f ~/.bash_aliases ]; then
  . ~/.bash_aliases
fi

# Git safety
export GIT_CEILING_DIRECTORIES="${HOME}"

# Prepend update path-like variables if our desired content is not already
# present.
prepend_if_missing() {
  local var_name="$1"
  local value="$2"
  local current="${!var_name}"
  if [[ ":$current:" != *":$value:"* ]]
  then
    if [[ -n "$current" ]]
    then
      printf -v "$var_name" '%s:%s' "$value" "$current"
    else
      printf -v "$var_name" '%s' "$value"
    fi
  fi
  export "$var_name"
}

# CUDA Toolkit paths
prepend_if_missing PATH /usr/local/cuda/bin
prepend_if_missing LD_LIBRARY_PATH /usr/local/cuda/lib64:/usr/local/cuda/lib
prepend_if_missing C_INCLUDE_PATH /usr/local/cuda/include
prepend_if_missing CPLUS_INCLUDE_PATH /usr/local/cuda/include

# Opt Install Root
prepend_if_missing PATH /opt/local/bin
prepend_if_missing LD_LIBRARY_PATH /opt/local/lib64:/opt/local/lib
prepend_if_missing MANPATH /opt/local/share/man
prepend_if_missing C_INCLUDE_PATH /opt/local/include
prepend_if_missing CPLUS_INCLUDE_PATH /opt/local/include

# Home install root
prepend_if_missing PATH ${HOME}/.local/bin
prepend_if_missing LD_LIBRARY_PATH ${HOME}/.local/lib64:${HOME}/.local/lib
prepend_if_missing MANPATH ${HOME}/.local/share/man
prepend_if_missing C_INCLUDE_PATH ${HOME}/.local/include
prepend_if_missing CPLUS_INCLUDE_PATH ${HOME}/.local/include

# Sonar Scanner
if [[ -d "${HOME}/sonar-scanner-4.6.2.2472-linux/bin" ]]
then
  prepend_if_missing PATH ${HOME}/sonar-scanner-4.6.2.2472-linux/bin
fi

# Android setup (if installed)
EXPECTED_ANDROID_HOME="${HOME}/Android/Sdk"
if [ -d "${EXPECTED_ANDROID_HOME}" ]
then
  export ANDROID_HOME=${EXPECTED_ANDROID_HOME}
  prepend_if_missing PATH ${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/platform-tools
fi

EXPECTED_ANDROID_NDK_HOME="${ANDROID_HOME}/ndk-bundle"
if [ -d "${EXPECTED_ANDROID_NDK_HOME}" ]
then
  export ANDROID_NDK_HOME=${EXPECTED_ANDROID_NDK_HOME}
  prepend_if_missing PATH ${ANDROID_NDK_HOME}
fi

#
# Other Constant Variables
#
export EDITOR=vim
export LESS="RM"

export VAGRANT_DEFAULT_PROVIDER=virtualbox

#export TERM=xterm-256color


# After this, we only setup things meant for interactive consoles
if [[ $- = *i* ]]; then
  #
  # Bash options and behavior
  #
  # -- History controls
  # Don't put duplicate lines or lines starting with a space in the history.
  HISTCONTROL=ignoreboth
  # Append to hist instead of overwriting.
  shopt -s histappend
  #
  # -- env variable expansion
  # We want env variables to be expanded on pressing tab instead of having the
  # `$` escaped.
  shopt -s direxpand
  #
  # Check the window size after each command and, if necessary,
  # update the values of LINES and COLUMNS.
  #
  shopt -s checkwinsize

  #
  # Activate bash completion script if present
  #
  if [ -f /etc/bash_completion ]
  then
    . /etc/bash_completion
  elif [ -f /usr/share/bash-completion/bash_completion ]
  then
    . /usr/share/bash-completion/bash_completion
  fi

  #
  # dircolors setup
  #
  if [ -x /usr/bin/dircolors ]
  then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
  fi

  #
  # make less more friendly for non-text input files, see lesspipe(1)
  #
  [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

  #
  # Setup git things
  #
  # Activate git prompting when requisite file available
  if [ -f /usr/share/git-core/contrib/completion/git-prompt.sh ]
  then
      . /usr/share/git-core/contrib/completion/git-prompt.sh
  elif [ -f /usr/lib/git-core/git-sh-prompt ]
  then
    . /usr/lib/git-core/git-sh-prompt
  fi
  GIT_PS1_SHOWDIRTYSTATE=1

  #
  # PS1 command line customization
  #
  # Color a little for highlighting where the promt is.
  #
  # Forcing color support if possible (borrowed from ubuntu bashrc).
  if [ -x /usr/bin/tput ] && tput setaf 1 &>/dev/null
  then
    color_prompt=1
  else
    unset color_prompt
    case "$TERM" in
      xterm-color|*-256color) color_prompt=1;;
      cygwin) color_prompt=1;;
    esac
  fi

  if [ "${color_prompt}" ]
  then
    PS1='$(date +"%Y-%m-%d %T") \[\033[01;32m\]\u@\h \[\033[01;34m\]\W\[\033[00m\]$(__git_ps1 " (%s)") \$ '
  else
    PS1='$(date +"%Y-%m-%d %T") \u@\h \W$(__git_ps1 " (%s)") \$ '
  fi
  unset color_prompt

  #
  # Add ssh agent env variables if not found
  #
  if [ -n "$(command -v keychain)" ] && [ "${SSH_AGENT_PID}" = "" ]
  then
    # Newer versions (2.9+) of keychain don't have the --agents option.
    if $(keychain --help 2<&1 | grep -E '^\s+--agents' >/dev/null)
    then
      # Older versions of keychain
      eval $(keychain --agents ssh --eval --quiet)
    else
      eval $(keychain --eval --quiet)
    fi
  fi

  #
  # Miniconda non-invasive activation
  #
  # Source script that adds a `conda` bash function but does not initially
  # activate any of its environments.
  #
  CONDA_INSTALL_HINTS=( "${HOME}/miniconda"
                        "${HOME}/miniconda2"
                        "${HOME}/miniconda3" )
  CONDA_SCRIPT="etc/profile.d/conda.sh"
  for INSTALL_HINT in "${CONDA_INSTALL_HINTS[@]}"
  do
    CONDA_SCRIPT_ACTUAL="${INSTALL_HINT}/${CONDA_SCRIPT}"
    if [ -f "$CONDA_SCRIPT_ACTUAL" ]
    then
      . "$CONDA_SCRIPT_ACTUAL"
      break
    fi
  done
  unset CONDA_SCRIPT CONDA_INSTALL_HINTS CONDA_SCRIPT_ACTUAL

  #
  # Activate virtualenvwrapper if present.
  #
  VEW_SCRIPT="${HOME}/.local/bin/virtualenvwrapper.sh"
  export WORKON_HOME="${HOME}/.python_virtualenvs"
  export VIRTUALENVWRAPPER_PYTHON="$(command -v python3)"
  if [[ -z "${VIRTUALENVWRAPPER_PYTHON}" ]]
  then
    export VIRTUALENVWRAPPER_PYTHON="$(command -v python2)"
    if [[ -z "${VIRTUALENVWRAPPER_PYTHON}" ]]
    then
      echo "WARNING: no python 2/3 available for virtualenvwrapper."
    fi
  fi
  if [[ -f "${VEW_SCRIPT}" ]] && [[ -n "${VIRTUALENVWRAPPER_PYTHON}" ]]
  then
    if [ ! -d "${WORKON_HOME}" ]
    then
      mkdir -p "${WORKON_HOME}"
    fi
    . "${VEW_SCRIPT}"
  fi
  unset VEW_SCRIPT

  # Mark this TTY as usable for GPG stuff
  export GPG_TTY=$(tty)

  #
  # Activate thefuck alias
  #
  if [ -n "$(which thefuck 2>/dev/null)" ]
  then
    eval $(thefuck --alias)
  fi

  #
  # PyENV
  #
  export PYENV_ROOT="${HOME}/.pyenv"
  if [[ -d "${PYENV_ROOT}/bin" ]]
  then
    export PATH="${PYENV_ROOT}/bin:${PATH}"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
  fi

  # Activate NVM
  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
  (command -v node) && export NODE_PATH="$NVM_DIR/versions/node/$(node -v)/lib/node_modules:${NODE_PATH}"

fi
