#
# ~/.bashrc -- global .bashrc, includes ~/.bashrc.local
#

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

# Bash settings
set -o noclobber
shopt -s checkwinsize
export HISTCONTROL=ignoreboth
export PROMPT_DIRTRIM=3

# Color output and default options
export TERM=xterm-color
export GREP_OPTS='--color=auto'
export LS_OPTS='--color=auto -hFN --group-directories-first'
export LESS="-Ri"

# Colored man pages
man() {
	env \
		LESS_TERMCAP_mb=$(printf "\e[1;31m") \
		LESS_TERMCAP_md=$(printf "\e[1;31m") \
		LESS_TERMCAP_me=$(printf "\e[0m") \
		LESS_TERMCAP_se=$(printf "\e[0m") \
		LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
		LESS_TERMCAP_ue=$(printf "\e[0m") \
		LESS_TERMCAP_us=$(printf "\e[1;32m") \
	man "$@"
}

# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# Set default options
alias ls='ls ${LS_OPTS}'
alias grep='grep ${GREP_OPTS}'
alias diff='diff --color=auto'
alias ip='ip -color=auto'

# Common shortcut aliases
alias l='ls -l'
alias la='ls -a'
alias ll='ls -la'
alias sorth='sort -hr'
alias ps-grep='ps -ef | grep'

# Alias expansion for sudo
# https://unix.stackexchange.com/questions/148545/why-does-sudo-ignore-aliases/148548#148548
alias sudo='sudo '

# Push the queerfeminist agenda
alias woman='man'
alias enby='man'

# meow instead of ping
alias meow='ping'

# git shortcuts
alias git-stashpull="git stash && git pull && git stash pop"

# X application aliases
alias gvimr='gvim --remote-silent'
alias xclip_='xclip -selection clipboard'

# Open files according to MIME type
xo() {
	(nohup mimeopen "$@" 2>/dev/null 1>&2 &)
}

# Wine aliases
alias wine32='env WINEARCH=win32 WINEPREFIX="$HOME/.wine32" wine'

alias youtube-dl-mp3='youtube-dl -x --audio-format mp3'

# Stop Ctrl-S from doing terminally things in vim/vimdiff
vim() {
	local STTYOPTS="$(stty --save)"
	stty stop '' -ixoff
	command vim "$@"
	stty "$STTYOPTS"
}

vimdiff() {
	local STTYOPTS="$(stty --save)"
	stty stop '' -ixoff
	command vimdiff "$@"
	stty "$STTYOPTS"
}

# yay without parameters does "pacman -Syu" but I don't want it to do this
yay() {
	[[ $# -gt 0 ]] || { echo "error: no operation specified (use -h for help)"; return 1; }
	command yay "$@"
}

# Create and cd to tmp dir
alias cdtmp='cd $(mktemp -d -p ~/tmp/)'

# Make dir and cd to it (from Mara)
function mkcd {
	mkdir -p "$1"
	cd "$1"
}

# cd to dir + ll (ls -la)
function cdll {
	cd "$1"
	ls -la
}

calc() {
	echo "$@" | bc
}

# Show content of something, `ls` for dirs (and special files), `cat` for files
lscat() {
	[[ $# -eq 0 ]] && ls -la && return
	printnewline=""
	for f in "$@"; do
		[[ $printnewline ]] && echo
		printnewline=1
		if [[ -f $f ]]; then
			[[ $# -gt 1 ]] && echo "-- cat \"$f\""
			cat "$f"
		else
			[[ $# -gt 1 ]] && echo "-- ls \"$f\""
			ls -la "$f"
		fi
	done
}

# Load z
[[ -r "/usr/share/z/z.sh" ]] && source /usr/share/z/z.sh

# Define colors for bash prompt (will also be used in .bashrc.local)
   TRED='\[\e[0;31m\]'  # Red
 TGREEN='\[\e[0;32m\]'  # Green
  TBLUE='\[\e[0;34m\]'  # Blue
TGREENB='\[\e[1;32m\]'  # Green bold
 TCYANB='\[\e[1;36m\]'  # Cyan bold
TWHITEB='\[\e[1;37m\]'  # White bold
 TRESET='\[\e[0m\]'     # Text Reset

# Set default prompt
if [[ ${EUID} == 0 ]] ; then
	PS1="${TRED}\u@\h ${TBLUE}\w ${TWHITEB}\$ ${TRESET}"
else
	PS1="${TGREEN}\u@\h ${TBLUE}\w ${TWHITEB}\$ ${TRESET}"
fi

# Include host-specific bashrc
[[ -f ~/.bashrc.local ]] && source ~/.bashrc.local

# Clean up environment variables
unset TRED TGREEN TBLUE TGREENB TCYANB TWHITEB TRESET

