# Prompt colors - see `prompt -h grml`
GRML_J_HOST_COLOR='%F{white}'
case $HOST in
  (hydra)   GRML_J_HOST_COLOR='%B%F{green}'   ;;
  (hera)    GRML_J_HOST_COLOR='%B%F{blue}'    ;;
  (zeus)    GRML_J_HOST_COLOR='%F{yellow}'    ;;
  (iris)    GRML_J_HOST_COLOR='%B%F{magenta}' ;;
  (hestia)  GRML_J_HOST_COLOR='%F{blue}'      ;;
  (phoenix) GRML_J_HOST_COLOR='%B%F{yellow}'  ;;
  (makris)  GRML_J_HOST_COLOR='%F{magenta}'   ;;
esac

zstyle ':prompt:grml:left:setup' items rc time change-root user at host path vcs percent 
if (( EUID == 0 )); then
  zstyle ':prompt:grml:*:items:user' pre   '%B%F{blue}[%F{red}'
else
  zstyle ':prompt:grml:*:items:user' pre   '%B%F{blue}['
fi
zstyle ':prompt:grml:*:items:host' pre   "${GRML_J_HOST_COLOR}"
zstyle ':prompt:grml:*:items:host' post  '%B%F{blue}]%f%b '
zstyle ':prompt:grml:*:items:host' token '%m'
zstyle ':prompt:grml:*:items:time' pre   "%B%F{blue}(%f%b${GRML_J_HOST_COLOR}"
zstyle ':prompt:grml:*:items:time' post  "%B%F{blue})%f%b"
zstyle ':prompt:grml:*:items:time' token '%D{%H:%M}'

# Some extra configuration
setopt numericglobsort     # sort filenames numerically when it makes sense

# History configurations
HISTSIZE=10000
SAVEHIST=20000
setopt hist_expire_dups_first # delete duplicates first when HISTFILE size exceeds HISTSIZE
setopt hist_ignore_all_dups   # ignore duplicated commands history list
setopt hist_ignore_space      # ignore commands that start with space
setopt hist_verify            # show command with history expansion to user before running it
setopt share_history          # share command history data
setopt inc_append_history     # Write to the history file immediately, not when the shell exits.
setopt extended_history       # Write the history file in the ":start:elapsed;command" format.
setopt hist_save_no_dups      # Don't write duplicate entries in the history file.
setopt hist_reduce_blanks     # Remove superfluous blanks before recording entry.

# force zsh to show the complete history
alias history="history 0"

# enable additional color support
if [ -x /usr/bin/dircolors ]; then
    alias fgrep='fgrep --color=auto'
    alias diff='diff --color=auto'
    alias ip='ip --color=auto'
fi

# enable auto-suggestions based on the history
if [ -f /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh ]; then
    . /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
    # change suggestion color
    ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=#999'
fi

# enable syntax highlighting 
if [ -f /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]; then
    . /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
fi

# enable command-not-found if installed
if [ -f /etc/zsh_command_not_found ]; then
    . /etc/zsh_command_not_found
fi


####### PERSONAL CHANGES #################

# Distro specific settings
[[ -a /etc/gentoo-release ]] && export DISTRO=gentoo
[[ -a /etc/arch-release ]]   && export DISTRO=arch
[[ -a /etc/debian_version ]] && export DISTRO=debian
case $DISTRO in
    (debian)
        sudo="sudo "
        alias agi="${sudo}apt install"
        alias pai="${sudo}apt install"
        alias acs="apt search"
        alias pas="apt search"
        alias acp="apt policy"
        alias acss="apt show"
        alias agu="${sudo}apt update"
        alias agdu="${sudo}apt dist-upgrade"
    ;;
    (gentoo)
        export PALUDIS_OPTIONS="--dl-reinstall-scm weekly"
        export PALUDIS_NO_WRITE_CACHE_CLEAN=yes
        alias emerge='echo "Use Paludis!"'
        alias pap='paludis -pi'
        alias pai='nice -n 20 paludis -i'
        alias pas='inquisitio -s'
        alias paq='paludis -q'
        alias qlop='pint.rb'
        alias pao='paludis -o'
        alias pal='paludis --contents'
    ;;
    (arch)
        pkgman="pacman"
        sudo="sudo "
        if [ -x /usr/bin/yay ]; then
            pkgman="yay"
            sudo=""
        fi
        if [ -x /usr/bin/paru ]; then
            pkgman="paru"
            sudo=""
            alias yay=paru
        fi
            alias pai="${sudo}${pkgman} -S ${aur}"
            alias paR="${sudo}${pkgman} -Rnsc ${aur}"
            alias pas="${pkgman} -Ss ${aur}"
            alias pao="${pkgman} -Qo"
            alias pal="${pkgman} -Ql"
            alias paq="yes n |${sudo}${pkgman} -Si ${aur}"
            alias padu="${sudo}${pkgman} -Syu"
            alias y="${sudo}${pkgman} ${aur}"
    ;;
esac

# Functions
hex() { echo ${$(echo "obase=16; $1" | bc):l} }
int() { echo $(echo "ibase=16; ${1:u}" | bc) }
# Bash Versions:
# hex() { I=$(echo "obase=16; $1" | bc); echo ${I,,}; }
# int() { H=${1^^}; echo $(echo "ibase=16; $H" | bc); }
clearcaches()
{
    echo "Before:"
    free -m
    echo -n 'syncing... '
    sync
    echo 'clearing caches...\n'
    echo -n '3' > /proc/sys/vm/drop_caches
    echo 'After:'
    free -m
}
saneperms()
{
    find . -type d -print0| xargs -0 chmod 755
    find . -type f -print0| xargs -0 chmod ${1:=644}
}
lowercase()
{
    print -n 'Really lowercase all files? (yn) '
    if read -q ; then
        for i in * ; do
            mv $i $i:l
        done
    fi
}
dirdiff()
{
    # Shell-escape each path:
    DIR1=$(printf '%q' "$1"); shift
    DIR2=$(printf '%q' "$1"); shift
    nvim $@ -c "DirDiff $DIR1 $DIR2"
}

# Global aliases -- These do not have to be at the beginning of    
# the command line.    
alias -g :g='| grep' # Easy grepping    
alias -g :p='| $PAGER' # Easy piping one    
alias -g :sp='| sort | $PAGER' # Easy piping two    
alias -g :n='&> /dev/null' # Easy piping to /dev/null    
alias -g :sn='1> /dev/null' # See above    
alias -g :en='2> /dev/null' # Ditto    
# Directories    
hash -d KSP='/mnt/games/Steam/steamapps/common/Kerbal Space Program'

# Aliases
alias quickweb='python -m http.server'
alias df='pydf'
alias screen='tmux'
alias pp='python -mjson.tool'
alias vim='nvim'
alias vimdiff='nvim -d'
alias kssh='kitten ssh'
