#####
# Someguy123's Customized .zshrc
#
# From github.com/someguy123/someguy-scripts
# License: GNU AGPLv3
#
# If this file is located in /etc/zsh - do not modify it! Instead, make your user customizations in ~/.zshrc
#
# If someguy-scripts didn't install a local .zshrc, you may need to copy it from /etc/skel
#
#    cp /etc/skel/.zshrc ~/.zshrc
#
#####

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
if [[ -d "$HOME/.oh-my-zsh" ]]; then
  export ZSH="$HOME/.oh-my-zsh"
elif [[ -d "/etc/oh-my-zsh" ]]; then
  export ZSH="/etc/oh-my-zsh"
else
  echo "CRITICAL ERROR: could not find oh-my-zsh in $HOME/.oh-my-zsh or /etc/oh-my-zsh"
fi

# If sourcing this file and you want to override any oh-my-zsh settings such as ZSH_THEME
# then set this to 'y' in your script before sourcing this file
: ${SG_OVERRIDE='n'}

# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
(( ! ${+ZSH_THEME} )) || [[ "$SG_OVERRIDE" == 'n' ]] && ZSH_THEME='fino'
# Note: THEMES ARE CasE SenSItIvE
# best user themes: Soliah fino bira dstufft candy obraun jreese
# best root themes: flazz afowler dstufft

# CASE_SENSITIVE="true"        # Uncomment the following line to use case-sensitive completion.
# DISABLE_AUTO_UPDATE="true"   # Uncomment the following line to disable bi-weekly auto-update checks.
# export UPDATE_ZSH_DAYS=13    # Uncomment the following line to change how often to auto-update (in days).
# DISABLE_LS_COLORS="true"     # Uncomment the following line to disable colors in ls.
# DISABLE_AUTO_TITLE="true"    # Uncomment the following line to disable auto-setting terminal title.
# ENABLE_CORRECTION="true"     # Uncomment the following line to enable command auto-correction.
# Display red dots whilst waiting for completion.
(( ! ${+COMPLETION_WAITING_DOTS} )) || [[ "$SG_OVERRIDE" == 'n' ]] && COMPLETION_WAITING_DOTS="true"

# Uncomment the following line to use hyphen-insensitive completion. 
# Case sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line if you want to disable marking untracked files under VCS as dirty. This makes repository 
# status check for large repositories much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time stamp shown in the history command output. 
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.

(( ! ${+plugins} )) || [[ "$SG_OVERRIDE" == 'n' ]] && plugins=(git command-not-found docker)

function reload_omz() { source "$ZSH/oh-my-zsh.sh"; }
reload_omz

# Global simple line separator used by the various helpers
LINE="============================================"

# if ZSHFILES_DIR isn't set by a sourcing script then load zsh_files from homedir. if that's not found, try etc
[ -z ${ZSHFILES_DIR+x} ] && {
  if [[ -d "$HOME/.zsh_files" ]]; then
    ZSHFILES_DIR="$HOME/.zsh_files"
  elif [[ -d "/etc/zsh_files" ]]; then
    ZSHFILES_DIR="/etc/zsh_files"
  fi
}

if ! [ -z ${ZSHFILES_DIR+x} ]; then 
  # load script helpers
  source "$ZSHFILES_DIR/common.zsh"      # Various random helper functions
  source "$ZSHFILES_DIR/colors.zsh"      # Terminal colours such as $RED, $YELLOW etc.
  source "$ZSHFILES_DIR/gnusafe.zsh"     # GNU Utility sanity checker
  source "$ZSHFILES_DIR/ssh.zsh"         # Helpers such as await-ssh, ssh-reset
  source "$ZSHFILES_DIR/rsync.zsh"       # brsync - better rsync
  source "$ZSHFILES_DIR/time.zsh"        # Date/time helper functions
  source "$ZSHFILES_DIR/steem.zsh"       # Steem RPC related e.g. rpc_rq, rpc_get_time
  if [[ "$(uname -s)" == "Darwin" ]]; then
      source "$ZSHFILES_DIR/osx.zsh"     # OSX specific functions
  fi
  # Linux specific functions
  if [[ "$(uname -s)" == "Linux" ]]; then
      source "$ZSHFILES_DIR/lvsnapshot.zsh"     
  fi
  source "$ZSHFILES_DIR/zshreload.zsh"   # ZSH Hook to recommend reloading after editing zshrc
else
  echo "WARNING: failed to find ~/.zsh_files and /etc/zsh_files - some things may break..."
fi

# Easily change between OMZ themes with a single command
# Usage: try-theme [theme name]  ##  Example: try-theme obraun
function try-theme() {
  [[ "$#" -lt 1 ]] && { msg red "No theme specified.\nUsage: try-theme [theme name]"; return; }
  msg green "Changing from theme '$ZSH_THEME' to '$1'"
  export ZSH_THEME="$1"
  reload_omz
}

# enable "help" for zsh built-ins
# e.g. $ help read
unalias run-help 2>/dev/null
autoload run-help
# if help isn't working properly, try setting HELPDIR
# to the correct location of the help files
# HELPDIR="/usr/local/share/zsh/help"
alias help=run-help

# small helpers to get external IPv4 and IPv6
function myip () {
  echo "${RED}$LINE"
  echo
  echo "${BLUE}IPv4:${RESET}"
  echo "$(myip4)"
  echo 
  echo "${GREEN}IPv6:${RESET}"
  echo "$(myip6)"
  echo
  echo "${RED}$LINE${RESET}"
}
function myip4 () {
  curl -4 -fsSL http://icanhazip.com
}
function myip6 () {
  curl -6 -fsSL http://icanhazip.com
}

# quick edit access to zshrc and vimrc
alias zshrc="vim ~/.zshrc"
alias vimrc="vim ~/.vimrc"

###
# Python Virtualenv shortcuts
###

function activate() {
    local envdir="./venv"
    if [[ "$#" -gt 0 ]]; then envdir="$1"; fi
    source "${envdir}/bin/activate"
    msg bold green "Activated virtualenv in $envdir"
}

# Usage:  mkvenv [python_exe] [env_folder]
# mkvenv                  # no args = use system python3 and make in ./venv
# mkvenv python3.7        # use system python3.7 and make in ./venv
# mkvenv python3.6 ./env  # use system python3.6 and make in ./env
function mkvenv() {
    local pyexe="python3"
    local envdir="./venv"
    if [[ "$#" -gt 0 ]]; then pyexe="$1"; fi
    if [[ "$#" -gt 1 ]]; then envdir="$2"; fi
    local pyver=$(/usr/bin/env "$pyexe" -V)
    /usr/bin/env "$pyexe" -m venv "$envdir"
    msg bold green "Made virtual env using $pyver @ $envdir"
}

###
# Other small aliases and helpers
###

alias l='ls -lah'
export LS_OPTIONS='--color=auto'
if [ -x "$(command -v dircolors)" ]; then
  eval "`dircolors`"
  alias ls='ls $LS_OPTIONS'
  alias ll='ls $LS_OPTIONS -l'
  alias l='ls $LS_OPTIONS -lah'
  alias la='ls $LS_OPTIONS -la'
fi

# Node Version Manager
[ -d "$HOME/.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
}

export EDITOR='vim'

# For convenience, when non-privileged users run 'su' or 'su -', convert it
# into 'sudo su -'. If the user is root, always run su normally.
su() {
    # For root, always run su normally
    if (( EUID == 0 )); then
        env su "$@"; return $?
    fi
    # If user runs 'su' or 'su -' convert it into 'sudo su -'
    if (( $# == 0 )); then 
        sudo su -; return $?
    elif (( $# == 1 )) && [[ "$1" == "-" ]]; then
        sudo su -; return $?
    else # If the command isn't 'su' or 'su -', then run su normally
        env su "$@"; return $?
    fi
}

# for Linux servers, if running as a normal user, alias important commands to use sudo
if [[ "$(uname -s)" == "Linux" && $UID != 0 && $EUID != 0 ]]; then
  # Sudo aliases for apt package manager
  alias apt='sudo apt'
  alias dpkg='sudo dpkg'
  alias apt-get='sudo apt-get'
  alias apt-add-repository='sudo apt-add-repository'
  alias apt-cache='sudo apt-cache'

  # Sudo aliases for services / systemd
  alias systemctl='sudo systemctl'
  alias sysctl='sudo sysctl'
  alias service='sudo service'
  alias journalctl='sudo journalctl'

  # Sudo aliases for LVM
  alias lvs='sudo lvs'
  alias lvdisplay='sudo lvdisplay'
  alias vgs='sudo vgs'
  alias pvs='sudo pvs'

  alias lvchange='sudo lvchange'
  alias lvconvert='sudo lvconvert'
  alias lvcreate='sudo lvcreate'
  alias lvextend='sudo lvextend'
  alias lvresize='sudo lvresize'
  alias lvreduce='sudo lvreduce'
  alias lvrename='sudo lvrename'
  alias lvscan='sudo lvscan'
  alias vgscan='sudo vgscan'
  alias vgcreate='sudo vgcreate'
  alias pvscan='sudo pvscan'

  # Sudo aliases for disk management
  alias fdisk='sudo fdisk'
  alias parted='sudo parted'

  alias iptables='sudo iptables'
  alias iptables-save='sudo iptables-save'
  
  # Sudo aliases for networking
  alias ip6tables='sudo ip6tables'
  alias ip6tables-save='sudo ip6tables-save'
  
  # Basic commands such as `ip addr` and `ip route` shouldn't require sudo
  # Don't use `sudo ip` if there's 0 or only 1 arg, allowing ip to work on accounts with no sudo privilege
  function ip() {
    [[ "$#" -gt 1 ]] && sudo /usr/bin/env ip "$@" || /usr/bin/env ip "$@"
  }
  alias ifconfig='sudo ifconfig'
  alias route='sudo route'
fi

