# Source other files
[ -r ~/.prompt ] && . ~/.prompt
[ -r ~/.devspec ] && . ~/.devspec
[ -r ~/.config/colorscheme ] && . ~/.config/colorscheme

[ "$TERM" == "screen.linux" ] && export TERM="screen"

export EDITOR="vim"
export TERM=xterm-256color

# History stuff
export HISTCONTROL=ignoredups:erasedups
export HISTFILESIZE=1000
export HISTSIZE=1000

export GPG_TTY=$(tty)

clear() {
	printf '\033c' # faster than ncurses clear by a lot
}

# Other aliases
alias c='clear'
alias ls='ls --color=auto -FAh'
alias ll='ls -l'
alias nf='neofetch'
alias ga='git add'
alias gc='git commit -sS'
alias gp='git push'
alias mpv='mpv --no-audio-display'
alias wv='swayhide mpv $($PASTE_COMMAND)'
alias curl='curl -L'
alias prof='profanity'
alias gomuks="http_proxy='socks5://127.0.0.1:9050' gomuks"
alias tut="http_proxy='socks5://127.0.0.1:9050' tut"

# Random string (passwords)
randstr() {
	tr -cd '[:graph:]' </dev/urandom | head -c ${1:-256}
}

# Get CPU usage
cpusage() {
        bc<<<"scale=3;$(ps aux | awk 'BEGIN {sum=0} {sum+=$3}; END {print sum}') / $(nproc --all)"
}

# Update and upgrade system
update() { sh -c "$UPDATE"; }

# Im too lazy to type
copy() {
	printf "%s" "$(</dev/stdin)" > >($COPY_COMMAND)
}

# URL-encode stdin
urlencode() {
	LC_COLLATE_OLD=$LC_COLLATE
	LC_COLLATE=C

	local TEXT="$(</dev/stdin)"
	local LENGTH="${#TEXT}"
	for (( i = 0; i < LENGTH; i++ )); do
		local c="${TEXT:$i:1}"
		case $c in
			[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
			*) printf '%%%02X' "'$c" ;;
		esac
	done
	LC_COLLATE=$LC_COLLATE_OLD
}

diffhome() { find . -type f -regex '\./\.[^g].*' -exec diff -q ~/{} {} 2> /dev/null \;; }

fixdh() {
	diffhome | while read -r _ SRC _ DEST _; do
		cp $SRC $DEST;
	done;
}

strlen() {
	N=$(wc -c <<< "$*")
	((N--))
	printf '%u\n' "$N"
}

uppweb() {
	pushd ~/git/cobra.vern.cc
	local DIR="/home/cobra/git/cobra.vern.cc"
	rsync -rtvzP --exclude ".git" --exclude ".gitignore" --exclude 'README.md' --exclude 'LICENSE' $DIR/* vern.cc:~/public_html/
	popd
}

upvweb() {
	pushd ~/git/website
	rsync --rsh='ssh -p1813' -rtvzP --exclude '.git' --exclude 'LICENSE' /home/cobra/git/website/ root@vern.cc:/var/www/website
	ssh vern chown -R www-data:www-data /var/www/website
	popd
}

upvgem() {
	pushd ~/git/gemini
	rsync --rsh='ssh -p1813' -rtvzP --exclude '.git' --exclude 'LICENSE' /home/cobra/git/gemini/ root@vern.cc:/home/gemini/gemini/vern.cc
	popd
}

pubdate() {
	date +'%a, %d %b %Y %T %z'
}

caption() {
	[ -z "$3" ] && {
		echo "Usage: caption [input] [output] [text...]"
		return 1
	}
	local FILE="$1"
	local OUT="$2"
	shift 2
	local TEXT="$*"
	local WIDTH="$(identify -format "%[w]" "$FILE")"
	local HEIGHT="$(identify -format "%[h]" "$FILE")"
	local FONT_SIZE="$(((HEIGHT+WIDTH)/20))"
	convert -gravity north \( -size "${WIDTH}x" -pointsize "$FONT_SIZE" -font 'Liberation-Sans-Bold' caption:"$TEXT" -gravity Center -extent "${WIDTH}x" \) "$FILE" -append "$OUT"
}

subcipher() {
	local COLOR PERM L1 line grep word
	if [ $# -lt 3 ]; then
		if [ $# -lt 2 ] || [[ "$1" != "0" && "$1" != "1" ]]; then
			printf "Usage: [0|1] subcipher file [string...]\n"
			return 1
		fi
	fi
	COLOR="$1"
	shift
	PERM="$1"
	shift
	[ -z "$*" ] && set -- "$(</dev/stdin)"
	L1="$(head -1 "$PERM")"
	while read -r line; do 
		tr "${L1^^}${L1}" "${line^^}${line}" <<< "$*"
	done < "$PERM" | \
	while read -r line; do
		for word in $line; do
			[ $COLOR -eq 1 ] && grep="$(grep --color=always -xF "$word" /etc/dictionaries-common/words)"
			printf %s "${grep:-$word} "
		done
		printf '\n'
	done
}

playcd() {
	mpv --start=00:00:00.0000000 av://libcdio:/dev/cdrom --cache=yes
}

playdvd() {
	mpv --start=00:00:00.0000000 dvd:///dev/sr0 --cache=yes
}

gpginfo() {
	gpg --with-colons --import-options show-only --import
}

lc() {
	fc -nl $HISTCMD | sed 's/^\s*//'
}

baseto() {
	if [ $# -ge 3 ]; then
		local i="$1"
		local o="$2"
		shift 2
		bc <<< "obase=$o;$(($i#$*))"
	else
		local line i o n
		while read -r line; do
			read -r i o n <<< "$line"
			bc <<< "obase=$o;$(($i#$n))"
		done
	fi
}

rebash() { . ~/.bashrc; }
bashrc() { "$EDITOR" ~/.bashrc && rebash; }

mnplot() {
	ssh mythos "${1:-/opt/gnuplot/gnuplot.sh}; cat /opt/gnuplot/${1:-$(date -u -I)}.svg" > ${1:-$(date -u -I)}.svg
}

vnplot() {
	ssh vern "${1:-/opt/gnuplot/net/gnuplot.sh}; cat /opt/gnuplot/net/${1:-$(date -u -I)}.svg" > ${1:-$(date -u -I)}.svg
}

vpplot() {
	ssh vern "${1:-/opt/gnuplot/power/gnuplot.sh}; cat /opt/gnuplot/power/${1:-$(date -u -I)}.svg" > ${1:-$(date -u -I)}.svg
}

inplot() {
	ssh iceberg "${1:-~/gnuplot/gnuplot.sh}; cat ~/gnuplot/${1:-$(date -u -I)}.svg" > ${1:-$(date -u -I)}.svg
}

cnplot() {
	ssh crescent "${1:-/opt/gnuplot/net/gnuplot.sh}; cat /opt/gnuplot/net/${1:-$(date -u -I)}.svg" > ${1:-$(date -u -I)}.svg
}

cpplot() {
	ssh crescent "${1:-/opt/gnuplot/power/gnuplot.sh}; cat /opt/gnuplot/power/${1:-$(date -u -I)}.svg" > ${1:-$(date -u -I)}.svg
}

reconf-sys () {
	$PRIV su - -c 'guix system reconfigure /etc/config.scm'
}

reconf-home () {
	guix home reconfigure ~/.config/guix/home.scm
}

alarm() {
	sleep $1; while :; do printf '\x77\x77\x77\x77\x77\x00\x00\x00'; done | aplay /dev/stdin
}
