#
# ~/.bashrc
#

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

# Load Environment from systemd configs
[[ -d ${XDG_CONFIG_HOME:-~/.config}/environment.d ]] && source <(cat ${XDG_CONFIG_HOME:-~/.config}/environment.d/*.conf 2>/dev/null | grep -Ev '^\s*(#|$)' | sed -rn 's/((.*)=(.*))$/export \2=\3/p')
[[ -d /etc/environment.d ]] && source <(cat /etc/environment.d/*.conf | grep -Ev '^\s*(#|$)' 2>/dev/null | sed -rn 's/((.*)=(.*))$/export \2=\3/p')
[[ -d /etc/systemd/system/user@.service.d ]] && source <(cat /etc/systemd/system/user@.service.d/*.conf 2>/dev/null | grep -Ev '^\s*(#|$)' | sed -rn 's/^Environment=("(.*)=(.*)")|((.*)=(.*))$/export \2\5="\3\6"/p')

# A shortcut function that simplifies usage of xclip.
# - Accepts input from either stdin (pipe), or params.
# ------------------------------------------------
cb() {
  local _scs_col="\e[0;32m"; local _wrn_col='\e[1;31m'; local _trn_col='\e[0;33m'
  # Check that xclip is installed.
  if ! type xclip > /dev/null 2>&1; then
    echo -e "$_wrn_col""You must have the 'xclip' program installed.\e[0m"
  # Check user is not root (root doesn't have access to user xorg server)
  elif [[ "$USER" == "root" ]]; then
    echo -e "$_wrn_col""Must be regular user (not root) to copy a file to the clipboard.\e[0m"
  else
    # If no tty, data should be available on stdin
    if ! [[ "$( tty )" == /dev/* ]]; then
      input="$(< /dev/stdin)"
    # Else, fetch input from params
    else
      input="$*"
    fi
    if [ -z "$input" ]; then  # If no input, print usage message.
      echo "Copies a string to the clipboard."
      echo "Usage: cb <string>"
      echo "       echo <string> | cb"
    else
      # Copy input to clipboard
      echo -n "$input" | xclip -selection c
      # Truncate text for status
      if [ ${#input} -gt 80 ]; then input="$(echo $input | cut -c1-80)$_trn_col...\e[0m"; fi
      # Print status.
      echo -e "$_scs_col""Copied to clipboard:\e[0m $input"
    fi
  fi
}
# Aliases / functions leveraging the cb() function
# ------------------------------------------------
# Copy contents of a file
function cbf() { cat "$1" | cb; }
# Copy SSH public key
alias cbssh="cbf ~/.ssh/id_rsa.pub"
# Copy current working directory
alias cbwd="pwd | cb"
# Copy most recent command in bash history
alias cbhs="cat $HISTFILE | tail -n 1 | cb"

alias dnf="dnf --releasever=35"
alias pkgto="pkgtop -d arch -c yellow -pacman yay"
alias ls='ls --color=auto'
export XZ_DEFAULTS="-T 6"
export GPG_TTY=$(tty)

export PS1='\[\033[01;37m\]$? $( if [[ $? == 0 ]]; then echo "\[\033[01;32m\]\342\234\223"; else echo "\[\033[01;31m\]\342\234\227"; fi) \[\033[1;32m\]\u\[\033[31m\]@\[\033[1;34m\]\h\[\033[1;33m\]:\[\033[1;35m\]\w\[\033[0m\]\$ \[\]'

if [[ $SHLVL -eq 1 ]]; then
    if [[ ${LANG} == "ru_RU.UTF-8" ]]; then
	FONT="$(grep "FONT" /etc/vconsole.conf | cut -d= -f2)"
	[[ -n $(tty | grep -q "/dev/tty") && -n ${FONT} ]] && setfont ${FONT}
	screenfetch | sed 's/OS:/Система:/' | sed 's/Kernel:/Ядро:/g' | sed 's/Uptime:/Время работы:/g' | \
	    sed 's/Packages:/Установлено пакетов:/g' | sed 's/Shell:/Оболочка:/g' | sed 's/Resolution:/Разрешение:/g' | sed 's/DE:/Среда рабочего стола:/g' | \
	    sed 's/WM:/Оконный менеджер:/g' | sed 's/WM Theme:/Тема оконного менеджера:/g' | sed 's/GTK Theme:/Тема GTK:/g' | sed 's/Icon Theme:/Тема значков:/g' | sed 's/Font:/Шрифт:/g' | \
	    sed 's/Disk:/Диск:/g'  | sed 's/RAM:/Оперативная память:/g'
    else
	screenfetch 
    fi
fi
