# /etc/zsh/zshrc or ~/.zshrc

# Path to local zsh configuration
LOCAL_ZSHRC=~/.zshrc.local

## Environment
if [ -e /etc/environment ]
then
  source /etc/environment
fi

## Aliases
alias ls='ls --color=auto '
alias ll='ls -l'
alias la='ls -a'
alias lL='ls -la'
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
alias less='less --quiet'
alias myip4="curl -4 https://chroot-me.in/ip/ 2> /dev/null || w3m -4 -dump https://chroot-me.in/ip"
alias myip6="curl -6 https://chroot-me.in/ip/ 2> /dev/null || w3m -6 -dump https://chroot-me.in/ip"
alias aaa="apt update && apt full-upgrade && apt clean"
alias www-share='python3 -m http.server'
alias lscp='scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
alias lssh='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
alias lrsync='rsync -e "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"'
alias s='ssh'

## Set Terminal Title with current hostname
if [ "$TERM" != "linux" ]; then precmd () {print -Pn "\e]0;%n@%m: %~\a"}; fi

## Keybindings and Prompt
# Lookup in /etc/termcap or /etc/terminfo else, you can get the right keycode
# by typing ^v and then type the key or key combination you want to use.
# "man zshzle" for the list of available actions
bindkey '^A'      beginning-of-line       # Home
bindkey '^E'      end-of-line             # End
bindkey '^D'      delete-char             # Del
bindkey '\e[3~'   delete-char             # Del
bindkey '\e[2~'   overwrite-mode          # Insert
bindkey '\e[5~'   history-search-backward # PgUp
bindkey '\e[6~'   history-search-forward  # PgDn
bindkey '\e[1;5C' forward-word            # C-Right
bindkey '\e[1;5D' backward-word           # C-Left
bindkey '\e.'     insert-last-word        # Esc+. works now in screen/tmux

# Linux console, screen or rxvt.
if [ "$TERM" = "linux" -o "$TERM" = "screen" -o "$TERM" = "rxvt" -o "$TERM" = "screen-256color-bce" ]
then
  bindkey '\e[1~' beginning-of-line       # Home
  bindkey '\e[4~' end-of-line             # End
fi

# xterm
if [ "$TERM" = "xterm" ]
then
  bindkey '\e[H'  beginning-of-line       # Home
  bindkey '\e[F'  end-of-line             # End
  bindkey '\eOH'  beginning-of-line       # Home
  bindkey '\eOF'  end-of-line             # End
fi

# Colorized prompt. Root and normal users have different colors.
if [ "`id -u`" -eq 0 ]; then
  export PS1="%{[33;36;1m%}%T%{[0m%} %{[33;34;1m%}%n%{[0m[33;33;1m%}@%{[33;37;1m%}%m %{[33;32;1m%}%~%{[0m[33;33;1m%}%#%{[0m%} "
  export HISTFILE=/root/.history
else
  export PS1="%{[33;36;1m%}%T%{[0m%} %{[33;31;1m%}%n%{[0m[33;33;1m%}@%{[33;37;1m%}%m %{[33;32;1m%}%~%{[0m[33;33;1m%}%#%{[0m%} "
  export HISTFILE=$HOME/.history
fi

# Color Management for the `ls` command
if [ -x /usr/bin/dircolors ]
then
  if [ -r ~/.dir_colors ]
  then
    eval "`dircolors ~/.dir_colors`"
  elif [ -r /etc/dir_colors ]
  then
    eval "`dircolors /etc/dir_colors`"
  else
    eval "`dircolors`"
  fi
fi

## ZSH specific settings
# man zshoptions
setopt correct          # try to correct spelling of commands
setopt print_exit_value # print return value if non-zero
unsetopt beep           # no bell on error
unsetopt clobber        # must use >| to truncate existing files
unsetopt hist_beep      # no bell on error in history
unsetopt ignore_eof     # do not exist on end-of-file
unsetopt list_beep      # no bell on ambiguous completion
unsetopt rm_star_silent # ask for confirmation for `rm *' or `rm path/*'

# Completion style
unsetopt list_ambiguous # ??

# Completion options
# man zshoptions
setopt auto_cd            # if command is a path, cd into it
setopt auto_pushd         # make cd push old dir in dir stack
setopt auto_remove_slash  # self explicit
setopt chase_links        # resolve symlinks
setopt extended_glob      # activate complex pattern globbing
setopt glob_dots          # include dotfiles in globbing
setopt pushd_ignore_dups  # no duplicates in dir stack
setopt pushd_silent       # no dir stack after pushd or popd
setopt pushd_to_home      # `pushd` = `pushd $HOME`
unsetopt bg_nice          # no lower prio for background jobs
unsetopt hup              # no hup signal at shell exit

zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}'
zstyle ':completion:*' max-errors 3 numeric
zstyle ':completion:*:functions' ignored-patterns '_*'
zstyle ':completion:*' use-compctl false

# On slow systems, checking the cached .zcompdump file to see if it must be
# regenerated adds a noticable delay to zsh startup.  This little hack restricts
# it to once a day.  It should be pasted into your own completion file.
#
# The globbing is a little complicated here:
# - '#q' is an explicit glob qualifier that makes globbing work within zsh's [[ ]] construct.
# - 'N' makes the glob pattern evaluate to nothing when it doesn't match (rather than throw a globbing error)
# - '.' matches "regular files"
# - 'mh+24' matches files (or directories or whatever) that are older than 24 hours.
autoload -Uz compinit
if [[ -n ${ZDOTDIR}/.zcompdump(#qN.mh+24) ]]; then
        compinit;
else
        compinit -C;
fi;

## History settings
export HISTORY=10000
export HISTSIZE=10000
export SAVEHIST=8000
setopt extended_history     # save timestamp and duration
setopt hist_ignore_all_dups # no duplicate
setopt hist_ignore_space    # ignore space prefixed commands
setopt hist_verify          # verify hist expansion
setopt inc_append_history   # append instead of replace
setopt share_history        # share hist between sessions

## Exports
export LANG="en_US.UTF-8"
export LANGUAGE="en"
export LC_CTYPE="en_US.UTF-8"
export LC_NUMERIC="en_US.UTF-8"
export LC_TIME="en_US.UTF-8"
export LC_COLLATE="en_US.UTF-8"
export LC_MONETARY="en_US.UTF-8"
export LC_MESSAGES="en_US.UTF-8"
export LC_PAPER="en_US.UTF-8"
export LC_NAME="en_US.UTF-8"
export LC_ADDRESS="en_US.UTF-8"
export LC_TELEPHONE="en_US.UTF-8"
export LC_MEASUREMENT="en_US.UTF-8"
export LC_IDENTIFICATION="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export EDITOR="/usr/bin/vim"
# FIXME - mtr AS lookup is currently broken...
#export MTR_OPTIONS="-z -o NLSD BAW  X"
export MTR_OPTIONS="-o NLSD BAW  X"
export QUOTING_STYLE=literal # No single quotes around filenames in `ls`

## Loading local configuration
if [ -e $LOCAL_ZSHRC ]
then
  source $LOCAL_ZSHRC
fi
