#!/usr/bin/env bash

##############################
# $USER specified file for configuring BASH.
# Author: Chris Jones
# Contact: twitter > @truckmonth
# Contact: email > chris.r.jones.1983@gmail.com <- email
# Contact: github > @ipatch
##

# shellcheck source=/dev/null

##############################
# 1) /etc/profile
# 2) /etc/bash.bashrc
# 3) ~/.bashrc | NOTE non-login interactive mode 
# 4) ~/.bash_profile | NOTE used for interactive login shell, typically used for console logins, ie. ctrl+alt+f1
# NOTE: for a deeper understanding of `interactive/login` see "INVOCATION" in the BASH man page.
# 5) ~/.bash_login
# 6) ~/.profile | NOTE used for graphical sessions, ie. KDE, | NOTE `~/.bash_profile` is not sourced
# 7) ~/.bash_logout
##

## Extremely helpful for debugging BASH scripts
#set -x
#trap read debug

# Env Vars

# User details | IRC
export USERNAME="capin"
export NICKNAME="capin"

export XDG_CONFIG_HOME="$HOME/.config"
export code="/opt/code"
export dots="/opt/code/dotfiles"
export github="/opt/code/github"
export public="/opt/code/public"
export private="/opt/code/github/private"

# Env Vars for git
export GIT_RC="$XDG_CONFIG_HOME/git"

##############################
# Aliases for interactive shell sessions
##
alias ..='cd ..'
alias cd..='cd ..'
# alias l='ls -lahF --color=auto'
alias l='/bin/ls -lahF'
alias ls='ls --color=auto'
alias lr='ls -lrth'

##
alias pp='echo $PATH'
alias br='exec bash'



alias cp='cp -iv'
alias rm='rm -iv'
alias mv='mv -iv'
# move quickly to directories
alias dots='cd $dots'
alias public='cd $public'
alias private='cd $private'
alias dotfiles='cd /opt/code/dotfiles'

# `git` aliases for git projects
alias gs='git status'
alias gd='git diff --stat'
alias ga='git add --all'
alias gp='git push'

# "storage" aliases
alias df='df -h'
alias du='du -sh'

# NOTE: functions in BASH are all stored in memory, ie. RAM and not on disk
# test to see if a functions dir exists with `$XDG_CONFG_HOME`
if [ -d ~/.config/bash/functions ]; then
	export funcs_dir="$HOME/.config/bash/functions"
	for file in "$funcs_dir"/*.bash; 
	do 
		source "$file";
	done
fi


# BASHRC FUNCTIONS

# read man specific entries for shell builtins
bashman () {
  man bash | man bash | less -p "^       $1 "
}

brew() {
  if [[ $@ == "cask outdated --greddy" ]]; then
    command brew cask outdated --greedy
  else
    command brew "$@"
  fi
}

# BASHRC FUNCTIONS end

# BASH history
## no dupes
HISTCONTROL=ignoredups:erasedups
# append on exit
shopt -s histappend
# append, reread histfile after each cmd
## TODO: below `PROMPT_COMMAND` === not hot dog
# PROMPT_COMMAND="{PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

# WRONG!!!
# check to see if $HOME/.bash_profile exists then load in current shell session.
# if [ -f ~/.bash_profile ]; then
#   source ~/.bash_profile
# fi

# nuke $PATH because I manually set it up below
# NOTE: since $PATH is being nuked for each interactive bash session there presently isn't a need to check for duplicate path entries, "BONUS".
PATH=""

# TODO: create a `paths` func to load paths into `PATH`

# setup an $OS env var for distinguishing between different Operating Systems
# NOTE: Darwin = macOS, Linux = Debian; and other assorted Linux distros
if [ -x /usr/bin/uname ]; then
	os=$(/usr/bin/uname)
	export os
elif [ -x /bin/uname ]; then
	os=$(/bin/uname)
	export os
fi

# os=$(/usr/bin/uname)
# export os

# some homebrew magic
if [ "$os" = Darwin ] && [ -e /usr/local/bin/brew ]; then
  brew_prefix="/usr/local"
  export brew_prefix
elif [ "$os" = Linux ] && [ -e /home/linuxbrew/.linuxbrew/bin/brew ]; then
  brew_prefix="/home/linuxbrew/.linuxbrew"
  export brew_prefix
fi

# TODO: conditionally only add paths for different operating systems
# NOTE: since $PATH has been nuked, these paths are added from top to bottom, ie. .../sbin will be the first entry in $PATH unless other bash files hijack the $PATH; ...i'm looking at you asdf
PATH=/Users/capin/.asdf/installs/elixir/1.7.4/bin
# below entries will be appened to $PATH
PATH=$PATH:$brew_prefix/sbin
PATH=$PATH:$brew_prefix/bin
PATH=$PATH:$brew_prefix/opt/coreutils/libexec/gnubin
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
PATH=$PATH:/usr/sbin
PATH=$PATH:/usr/bin
PATH=$PATH:/sbin
PATH=$PATH:/bin

# source helpful files for working with the `asdf` version manager
# if [ -f ~/.asdf/asdf.sh ]; then
# 	source ~/.asdf/asdf.sh
# fi

# if [ -f ~/.asdf/completions/asdf.bash ]; then 
# 	source ~/.asdf/completions/asdf.bash
# fi

# source helpful file for working with `fzf` CLI fuzzy finder
# NOTE: the below statement tests to see the `.fzf.bash` file exists in ~ and then loads into the interactive BASH session.
if [ -f ~/.fzf.bash ]; then
	source ~/.fzf.bash
fi

# TODO: setup bash to use node.js provided by nvm and not homebrew
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
alias e='echo'
