#! /usr/bin/env bash

# Use the iterm2 material theme in the root directory of the essentials repository. For linux,
# you will need to source a material theme color scheme for your terminal (for XFCE use nord in the xfce
# themes folder).

# This monstrosity returns the directory of the .bashrc from it's symlinked source so we can
# call script files correctly.

if [[ "$OSTYPE" == "linux-gnu" ]]; then
  symlink_dir=$(cd "$( dirname "`readlink -f ${BASH_SOURCE[0]}`" )" && pwd)
else
  # OS X is not a fan of `readlink -f`
  symlink_dir=$(cd "$( dirname "`readlink ${BASH_SOURCE[0]}`" )" && pwd)
fi


# -------------------------------- SETUP -------------------------------- #

# Store private information (exports, keys, etc) in .bash_private.
if [[ -f $HOME/.bash_private ]]; then
  source $HOME/.bash_private
  echo ".bash_private loaded"
fi

if [ -f $HOME/.inputrc ]; then
        source $HOME/.inputrc
else
        echo "No .inputrc detected"
fi

if [[ "$OSTYPE" == "darwin"* ]]; then
  if [ -f $HOME/.macrc ]; then
          source $HOME/.macrc
  fi
fi


# Set up projects and gopath folder properly for the export in the export section.

if [[ ! -d $HOME/projects ]]; then
  echo "Making $HOME/projects"
  mkdir -p $HOME/projects
fi

if [[ -x go ]]; then
  echo "Making $HOME/projects/go"
  mkdir -p $HOME/projects/go
fi


# Enable git completion
test -f $symlink_dir/scripts/git-completion.bash && . $_


# ------------------------------ END SETUP ------------------------------ #



# -------------------- FUNCTIONS -------------------- #

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
}

# If you ever needed weather...
weather() {
    curl wttr.in
}

# ------------------ END FUNCTIONS ------------------ #

# ---------------- ALIASES ----------------#

# Useful global functions
alias cgen=$symlink_dir/scripts/cgen.sh

if [[ "$OS_TYPE" != "gnu-linux" ]]; then
  alias python='python3'
fi

# rlwrap provides readline wrapping for programs.
# This alias makes the SBCL REPL usable.
alias sbcl='rlwrap sbcl'

# Useful git aliases
alias gita='git add'
alias gitap='git add -p'
alias gitc='git commit'
alias gitp='git push'
alias gits='git status'
alias gitd='git diff'

alias k8='kubectl'

# Good overrides

# Don't nuke the computer
alias rm='rm -i'

# Better ls - lists more information for each file
alias ll='ls -lh'

# Human readable ls for the current directory
if [[ "$OS_TYPE" != "gnu-linux" ]]; then
    # OS X lt
    alias lt='du -sh * | sort -h'
else
    alias lt='ls --human-readable --size -1 -S --classify'
fi


# Count files in the current directory
alias count='find . -type f | wc -l'

# Generate sha1 hashes on the fly
alias sha1='openssl sha1'

# -------------- END ALIASES --------------#


# -------------- EXPORTS --------------#

export EDITOR=vim
export CC=$(which clang)
export CXX=$(which clang++)
export OPENSSL_ROOT_DIR=$(which openssl)
export PATH="/usr/local/opt/openjdk/bin:$PATH"

# Ignore duplicates in history to make history less of a hassle to use.
export HISTCONTROL=ignoredups


# If go is installed, set the $GOPATH to the projects directory created above.
if which go > /dev/null; then export GOPATH=$HOME/projects/go; fi


# ------------ END EXPORTS ------------#


# ----------------------------------------- INITIALIZATIONS -------------------------------------- #

if [[ "$OSTYPE" == "linux-gnu" ]]; then
  export PATH="$HOME/.pyenv/bin:$PATH"

	# For whatever reason the linux version of pyenv doesnt install `pyenv-virtualenv-init`.
	eval "$(pyenv init -)"
	eval "$(pyenv virtualenv-init -)"
fi

if which pyenv-virtualenv-init > /dev/null; then
	eval "$(pyenv init -)"
	eval "$(pyenv virtualenv-init -)"
fi

# RVM configuration
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

# Test for the `bash-completion` package. Installing this adds better tab completion to bash.
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

# --------------------------------------- END INITIALIZATIONS ------------------------------------ #


# ------------------------------------------ FORMATTING ------------------------------------------ #

if [[ $- == *i* ]]
then
	# Better color handling so scrolling doesn't get broken.
	# Note for Cygwin you need ncurses for this to work.
	reset=$(tput sgr0)
	green=$(tput setaf 2)
	blue=$(tput setaf 4)
	white=$(tput setaf 7)

	bold=$(tput bold || tput md)

	export PROMPT_DIRTRIM=2

	# Important note for PS1 modification - everything non-printable must be escaped with `\[` and `\]`. Otherwise readline
	# fails to track the prompt right and things get messed up when you input long strings.

	if [[ "$OSTYPE" == "linux-gnu" ]]; then
	  # I had some rendering issues running in XFCE unless I added an extra space at the end.
	  export PS1='\[$bold\]\[$white\]\u@\h\[$reset\]: \[$bold\]\[$blue\]\w\[$reset\] \[$green\]$(parse_git_branch)\[$reset\]\[$bold\]\[$white\]⇨\[$reset\] '
	else
	  export PS1='\[$bold\]\[$white\]\u@\h\[$reset\]: \[$bold\]\[$blue\]\w\[$reset\] \[$green\]$(parse_git_branch)\[$reset\]\[$bold\]\[$white\]⇨\[$reset\] '
	fi
fi


# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.
export PATH="$PATH:$HOME/.rvm/bin"