# environment variables

## system
BROWSER=firefox; export BROWSER
CC=cc; export CC
EDITOR=nvim; export EDITOR
LD_LIBRARY_PATH="$HOME/.local/lib:$LD_LIBRARY_PATH"; export LD_LIBRARY_PATH
PAGER=less; export PAGER
PATH="$HOME/.local/bin:$HOME/.local/games:$PATH"; export PATH

## XDG
XDG_CACHE_HOME="$HOME/.var/cache"; export XDG_CACHE_HOME
XDG_CONFIG_HOME="$HOME/.local/etc"; export XDG_CONFIG_HOME
XDG_DATA_HOME="$HOME/.local/share"; export XDG_DATA_HOME
XDG_STATE_HOME="$HOME/.local/state"; export XDG_STATE_HOME

XDG_DESKTOP_DIR="$HOME/desktop"; export XDG_DESKTOP_DIR
XDG_DOCUMENTS_DIR="$HOME/documents"; export XDG_DOCUMENTS_DIR
XDG_DOWNLOAD_DIR="$HOME/downloads"; export XDG_DOWNLOAD_DIR
XDG_MUSIC_DIR="$HOME/music"; export XDG_MUSIC_DIR
XDG_PICTURES_DIR="$HOME/pictures"; export XDG_PICTURES_DIR
XDG_PUBLICSHARE_DIR="$HOME/public"; export XDG_PUBLICSHARE_DIR
XDG_SCREENSHOTS_DIR="$XDG_PICTURES_DIR/screenshots"; export XDG_SCREENSHOTS_DIR
XDG_TEMPLATES_DIR="$HOME/templates"; export XDG_TEMPLATES_DIR
XDG_VIDEOS_DIR="$HOME/videos"; export XDG_VIDEOS_DIR

## programming environment
CARGO_HOME="$XDG_DATA_HOME/cargo"; export CARGO_HOME
GOPATH="$XDG_DATA_HOME/go"; export GOPATH
RUSTUP_HOME="$XDG_DATA_HOME/rustup"; export RUSTUP_HOME

CMAKE_EXE_LINKER_FLAGS="-fuse-ld=mold"; export CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS="-fuse-ld=mold"; export CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS="-fuse-ld=mold"; export CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS="-fuse-ld=mold"; export CMAKE_STATIC_LINKER_FLAGS

## misc
LIBVA_DRIVER_NAME=radeonsi; export LIBVA_DRIVER_NAME
MOZ_ENABLE_WAYLAND=1; export MOZ_ENABLE_WAYLAND
QT_QPA_PLATFORM=wayland; export QT_QPA_PLATFORM
QT_QPA_PLATFORMTHEME=qt5ct; export QT_QPA_PLATFORMTHEME
GTK_IM_MODULE=ibus; export GTK_IM_MODULE
QT_IM_MODULE=ibus; export QT_IM_MODULE
XMODIFIERS=@im=ibus; export XMODIFIERS

# functions
bmv() {
	if test -z "$1"; then
		printf "Usage: %s [files pattern]...\n" "$0" 1>&2
		return 64 # sysexits.h(3) EX_USAGE
	fi

	while test -n "$2"; do
		for file in $1; do
			mv "$file" "$(printf "%s" "$file" | sed "s/\$2//g")"
		done

		shift 2
	done
}

ix() {
	curl -F 'f:1=<-' ix.io
}

myip() {
	curl icanhazip.com
}

paclean() {
	for package in `pacman -Qtq`; do
		pacman -Qi "$package"

		printf "Uninstall? [y/N] "
		read resp

		# TODO: Add undo
		if [ "$resp" != "y" ] && [ "$resp" != "Y" ]; then
			printf "Skipped %s.\n\n" "$package" 1>&2
		else
			printf "Queued %s for uninstall.\n\n" "$package" 1>&2
			packages="$packages$package "
		fi
	done
	su -c "pacman -Rns $packages"
}


sc() {
	if test -z "$1"; then
		printf "Usage: %s URI...\n" "$0" 1>&2
		return 64 # sysexits.h(3) EX_USAGE
	fi

	while "$1"; do
		scdl --original-name --debug -l "$1" >> log 2>&1
	done
}

yt() {
	if test -z "$1"; then
		printf "Usage: %s URI...\n" "$0" 1>&2
		return 64 # sysexits.h(3) EX_USAGE
	fi

	while "$1"; do
		yt-dlp -vx --split-chapters -o \
			"chapter:%(fulltitle)s - %(section_number)s %(section_title)s.%(ext)s" \
			"$1" --audio-quality 0 >> log 2>&1
		shift
	done
}

# aliases
alias weather="curl 'wttr.in/?0'"
alias vi="busybox vi"

# zsh configuration settings
case "$SHELL" in
	/bin/sh || */dash )
		set -o vi
		PS1='$ '
		;;

	*/zsh )
		HISTFILE="$XDG_DATA_HOME/zsh/histfile"
		HISTSIZE=999999999
		SAVEHIST="$HISTSIZE"

		unsetopt autocd
		bindkey -v
		zstyle :compinstall filename "$HOME/.zshrc"
		autoload -Uz compinit
		compinit -u -d "$XDG_CACHE_HOME/zsh/zcompdump"
		setopt rmstarsilent

		# Key (zshmisc(1)):
		#
		# '%F{$color}$x%f': Change foreground color of $x to $color
		# '%B$x%b': Make $x bold
		# '%n': $USER
		# '%M': $HOST
		# '%1~': $PWD, but shows '~' if at $HOME
		# '%(?.#.%?)':
		# '%n(x.true.false)' evaluates ternary expression for condition x, where n
		# is an integer, by default '0':
		# '?': If the last command’s exit status was 0, true
		# '#': Prints literal '#' to the prompt
		# '[%?]': Prints the last exit status with literal brackets surrounding it

		ERR='[%f%F{green}%?%f%F{blue}]%f'
		ERR_LINE="%(?.#.$ERR)"

		LINE1='%F{blue}╭%f %B%F{blue}%n%f@%F{magenta}%M%f%b'
		LINE2='%F{blue}│%f %F{green}%B%0~%b%f'
		LINE3="%F{blue}╰─$ERR_LINE%f "

		PS1="$(printf "%s\n%s\n%s" "$LINE1" "$LINE2" "$LINE3")"; export PS1

		plugindir="/usr/share/zsh/plugins"
		for plugin in \
			"$plugindir/zsh-autosuggestions/zsh-autosuggestions.zsh" \
			"$plugindir/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh" \
			"$plugindir/zsh-history-substring-search/zsh-history-substring-search.zsh"
		do
			source "$plugin"
		done

		bindkey -a 'k' history-substring-search-up
		bindkey -a 'j' history-substring-search-down

		alias which="$HOME/.local/bin/which"
		;;
esac
