# ~/.bashrc
# byMasterKTO

# NOTE if .bashrc is not sourced at login, add `source ~/.bashrc` to ~/.profile

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

########################################################################################################################
# set custom prompt
# for upgrade, we should look at https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Terminfo_escape_sequences

system=$(uname)

# colors and custom
C_BOLD="\[$(tput bold)\]"
C_RESET="\[$(tput sgr0)\]"
if [[ $system == "Linux" ]]; then
  C_USER="\[$(tput setaf 19)\]"
  C_ROOT="\[$(tput setab 1)\]"
  C_ARCH="\[$(tput setaf 32)\]"
  C_DEB="\[$(tput setaf 166)\]"
  C_RH="\[$(tput setaf 21)\]"
  C_SUSE="\[$(tput setaf 40)\]"
elif [[ $system == "OpenBSD" ]]; then
  C_USER="\[$(tput setaf 19 19 19)\]"
  C_ROOT="\[$(tput setab 1 1 1)\]"
  C_OBSD="\[$(tput setaf 11 11 11)\]"
fi

# user
if [[ $(whoami) == 'root' ]]; then
  user="$C_BOLD$C_ROOT\u$C_RESET"
else
  user="$C_BOLD$C_USER\u$C_RESET"
fi

# distro
if [[ $system == "Linux" ]]; then
  case $(lsb_release -si) in
    # Arch family: light blue
    Arch | ManjaroLinux | Manjaro-ARM) COLOR=$C_BOLD$C_ARCH;;
    # Debian family: orange
    Debian | Ubuntu | Linuxmint | Neon | Raspbian) COLOR=$C_BOLD$C_DEB;;
    # RedHat family: blue
    Fedora) COLOR=$C_BOLD$C_RH;;
    # SUSE family: green
    openSUSE) COLOR=$C_BOLD$C_SUSE;;
    # Other: white
    *) COLOR=$C_BOLD;;
  esac
elif [[ $system == "OpenBSD" ]]; then
  # OpenBSD: yellow
  COLOR=$C_BOLD$C_OBSD
  BSD="$C_BOLD(BSD) $C_RESET"
fi

# virtualization
if [[ $system == "Linux" ]]; then
  type systemd-detect-virt > /dev/null 2>&1
  if [[ $? -eq 0 ]]; then
    systemd-detect-virt --quiet -v
    if [ $? -eq 0 ]; then
      virt='/V'
    fi
    systemd-detect-virt --quiet -c
    if [ $? -eq 0 ]; then
      virt='/C'
    fi
  fi
fi

# hostname
# if hostname has 5 parts then host=.secondpart
if [[ $(hostname | grep -o "\." | wc -l) -eq 4 ]]; then
  host='.'$(hostname | cut -f 2 -d '.')
fi
hn="$COLOR\h$host$virt$C_RESET"

# current directory
cur_dir="$C_BOLD\W$C_RESET"

# set result
PS1=$C_RESET$BSD$user' '$hn' '$cur_dir' \$ '
########################################################################################################################

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
  test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
  alias ls='ls --color=auto'

  alias grep='grep --color=auto'
  alias fgrep='fgrep --color=auto'
  alias egrep='egrep --color=auto'
fi

# some more aliases
if [[ $system == "Linux" ]]; then
  alias ll='ls -lhX'
  alias la='ls -lahX'
  alias dh='df -h'
elif [[ $system == "OpenBSD" ]]; then
  alias ll='ls -lh'
  alias la='ls -lah'
  alias dh='df -h'
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# useful exports
export EDITOR=vim
export GPG_TTY=$(tty)

# unsetting $TMUX enables sessions nesting
unset TMUX

# Saving touchpad (hp8)
alias touchpad='xinput set-prop 12 "Device Enabled" 1'

# Set VNC aliases
X_user=$(who | grep \(: | cut -d " " -f1)
X_display=$(who | grep \(:  | cut -d ":" -f2 | cut -b 1)
if id -u gdm > /dev/null 2>&1; then
  X_gdm_id=$(id -u gdm)
elif id -u Debian-gdm > /dev/null 2>&1; then
  X_gdm_id=$(id -u Debian-gdm)
fi
alias vnc-mine='x11vnc -localhost -once -nopw -display :0'
alias vnc-desktop="sudo -H -u $X_user x11vnc -localhost -once -nopw -display :0"
alias vnc-gnome="sudo -H -u $X_user x11vnc -localhost -once -nopw -display :$X_display"
alias vnc-sddm='sudo x11vnc -auth /var/run/sddm/* -localhost -once -nopw -display :0'
alias vnc-gdm="sudo x11vnc -auth /run/user/$X_gdm_id/gdm/Xauthority -localhost -once -nopw -display :0"
alias vnc-lightdm='sudo x11vnc -auth /var/run/lightdm/root/:0 -localhost -once -nopw -display :0'

# load any additional configuration
for f in ~/.bashrc-*; do
  [ -e "$f" ] && . ~/.bashrc-*
  break
done