################### ALIASES ##################
alias hc='herbstclient'

################## FUNCTIONS ##################

# power off after finishing mpd playlist
mpd-sleep() {
	mpc play
	while true; do
		mpc status
		mpc idle player
		if [[ -z "$(mpc status | grep playing)" ]]; then
			poweroff
		fi
	done
}

# make small cover versions for mpc-notify.sh
# leaks memory for some reason, so don't run on a clean library
# or it'll crash everything
mpd-cover-convert() {
	local d
	find ~/music -type f -regextype posix-extended -regex ".*cover\.(png|jpg)"\
		| while read file; do
		d=$(dirname "$file")
		if [[ ! -f "$d/cover-small.png" ]]; then
			convert "$file" -resize 250x "$d/cover-small.png"
		fi
	done
}

# list stats about mpd library file types, to motivate me
# to do better!
mpd-filetypes() {
	local pattern

	# if args exist, read them as file extensions in a pattern
	if 
	if [[ ! -z "$@" ]]; then
		pattern=$(echo "$@" | sed -e 's/\s/|/g' -e 's/\(.*\)/(\1)/')
	else
	# else hackily yoink recognised types from mpd --version
		pattern=$(mpd --version \
			| sed -n '1h; 1!H; ${g; s/.*Decoders plugins:\(.*\)Output.*/\1/g; p}' \
			| sed -e 's/.*\]//' | tr -d '\n' \
			| sed -e 's/^ //' -e 's/\s/|/g' -e 's/\(.*\)/(\1)/')
	fi

	# find file counts
	local i=1
	local total=0
	local totalstr='total'
	local counts=()
	local types=()
	find ~/music -type f -regextype posix-extended \
		-regex ".*\.$pattern" | grep -oE "\.$pattern$" \
		| sort | uniq -c | sed -e 's/^\s*\([0-9]* \)\./\1/' | \
	while read -A line; do
		counts[$i]=${line[1]}
		types[$i]=${line[2]}
		total=$(( $total + ${line[1]} ))
		i=$(( $i + 1 ))
	done

	# calculate percentages
	local twidth=0
	local cwidth=0
	local percentages=()
	for i in {1..${#types}}; do
		percentages[$i]="%$(calc -p \
			"a=${counts[$i]} * 100 / $total; round(a, 5-digits(a), 16)" \
			| tr -d '~')"

	done

	types=( 'type' "${types[@]}" 'total' )
	counts=( 'count' "${counts[@]}" $total )
	percentages=( 'percent' "${percentages[@]}" '       ')

	# calculate padding widths
	for i in {1..${#types}}; do
		if [[ ${#types[$i]} -gt $twidth ]]; then
			twidth=${#types[$i]}
		fi

		if [[ ${#counts[$i]} -gt $cwidth ]]; then
			cwidth=${#counts[$i]}
		fi
	done


	# print results
	local linelen=$(( $twidth + $cwidth + 13 ))
	local j
	echo -n "┌"
	for (( j=0; j < $linelen; j++ )); do
		echo -n "─"
	done
	echo "┐"

	for i in {1..${#types}}; do
		if [[ $i -eq 2 || $i -eq ${#types} ]]; then
			echo -n "├"
			for (( j=0; j < $linelen; j++ )); do
				echo -n "─"
			done
			echo "┤"
		fi

		if [[ $i -eq 1 ]]; then
			printf "│ %${twidth}s  " "${types[$i]}"
		else
			printf "│ %${twidth}s: " "${types[$i]}"
		fi

		printf "%${cwidth}s" "${counts[$i]}"

		if [[ $i -eq 1 || $i -eq ${#types} ]]; then
			printf "  %-7s │\n" "${percentages[$i]}"
		else
			printf ", %-7s │\n" "${percentages[$i]}"
		fi
	done

	echo -n "└"
	for (( j=0; j < $linelen; j++ )); do
		echo -n "─"
	done
	echo "┘"
}

# capture webm
scap() {
	archey3
	sleep .1
	if [[ -f '/tmp/out.webm' ]]; then
		rm /tmp/out.webm
	fi
	echo 'recording...'
	ffmpeg -video_size 1920x1080 -framerate 30 -f x11grab -i :0.0 \
		-c:v libvpx -b:v 4M -threads 4 /tmp/out.webm > /dev/null 2>&1
}

# quickly send a file to hosted tmp dir
send() {
	local name
	local basename
	if [[ "$1" ]]; then
		scp $@ shmibbles.me:http/tmp/
		if [[ $? -eq 0 ]]; then
			for name in "$@"
			do
				basename=$(basename $name)
				ssh shmibbles.me "cd http/tmp; chmod o+r '$basename'"
				name=$(echo "https://shmibbles.me/tmp/$basename"\
				| sed -e 's/ /%20/g' -e 's/?/%3f/g')
				echo $name | tr -d '\n' | xclip -i -selection clipboard
				echo $name | tr -d '\n' | xclip -i -selection primary
			done
		fi
	else
		echo "specify at least one file to send"
	fi
}

# take a screenshot, upload to /img/scrot, and update current symlink
ssh-scrot() {
	archey3

	local name
	if [[ "$1" != "" ]]; then
		name=$1
	else
		echo -n "name: "
		read name
	fi
	
	local date=$(date +'%Y-%m-%d')
	
	local folder="http/img/scrot"
	ssh shmibbles.me "mkdir -p $folder/$date"
	
	if [[ "${?#0}" != "" ]]; then
		return 1
	fi

	ssh shmibbles.me "rm $folder/current 2>/dev/null; ln -s $folder/$date $folder/current"
	
	for i in {3..1}; do
		echo -n "$i "
		sleep 1
	done
	
	echo 'cheese!'
	sleep .1

	scrot /tmp/$name.png
	convert -scale 250x /tmp/$name.png /tmp/${name}_small.png

	scp /tmp/$name.png /tmp/${name}_small.png shmibbles.me:http/img/scrot/$date

	echo "https://shmibbles.me/img/scrot/$date/$name.png" | tr -d '\n' | xclip -i -selection clipboard
	echo "https://shmibbles.me/img/scrot/$date/$name.png" | tr -d '\n' | xclip -i -selection primary
	echo "https://shmibbles.me/img/scrot/$date/${name}_small.png" | tr -d '\n' | xclip -i -selection clipboard
	echo "https://shmibbles.me/img/scrot/$date/${name}_small.png" | tr -d '\n' | xclip -i -selection primary

	echo 'sent!'

	rm /tmp/$name.png /tmp/${name}_small.png

}

# export a section of a video to gif
make-gif() {

	if [[ -z "$1" ]]; then
		break
	fi

	rm -f make-gif-palette.png
	rm -f make-gif-palette.png
	rm -f make-gif-in
	
	rm -f make-gif-in
	
	ln -s "$1" make-gif-in
	
	local start
	echo -n "start [00:00:00]: "
	read start
	if [[ -z "$start" ]]; then
		start="00:00:00"
	fi
	
	local length
	echo -n "length [full]: "
	read length
	if [[ -z "$length" ]]; then
		t=""
		length=""
	else
		t="-t"
	fi

	local fps
	echo -n "fps [10]: "
	read fps
	if [[ -z "$fps" ]]; then
		fps="10"
	fi
	
	local width
	echo -n "width [480]: "
	read width
	if [[ -z "$width" ]]; then
		width="480"
	fi

	local subs
	echo -n "use subtitles? [y/N]: "
	read subs
	if [[ "$subs" == "y" || "$subs" == "Y" ]]; then
		subs="true"
	else 
		subs=""
	fi
	
	if [[ $subs ]]; then
		ffmpeg -y -ss "$start" $t "$length" -i "$1" \
			-copyts -vf "subtitles=make-gif-in,fps=$fps,scale=$width:-1:flags=lanczos,palettegen" make-gif-palette.png
		ffmpeg -ss "$start" $t "$length" -i "$1" -i make-gif-palette.png \
			-copyts -filter_complex \
			"subtitles=make-gif-in,fps=$fps,scale=$width:-1:flags=lanczos[x];[x][1:v]paletteuse" \
			out.gif
	else
		ffmpeg -y -ss "$start" $t "$length" -i "$1" \
			-vf "fps=$fps,scale=$width:-1:flags=lanczos,palettegen" make-gif-palette.png
		ffmpeg -ss "$start" $t "$length" -i "$1" -i make-gif-palette.png -filter_complex \
			"fps=$fps,scale=$width:-1:flags=lanczos[x];[x][1:v]paletteuse" \
			out.gif
	fi

	rm -f make-gif-palette.png
	rm -f make-gif-in
}

# um...
fuck() {
	local fuck="fuck"
	while true; do
		echo -en "\e[$((RANDOM%2));$((RANDOM%8+30))m"

		for i in {1..4}; do
			if [[ $((RANDOM%2)) -eq 1 ]]; then
				echo -n $fuck[$i] | tr '[:lower:]' '[:upper:]'
			else
				echo -n $fuck[$i]
			fi
		done

		for i in {1..$((RANDOM%20))}; do
			echo -n " "
		done
	done
}