#!/bin/sh

# Depends: socat

SOCK="/tmp/umpv-fifo"

help() {
	B=$(tput bold)
	U=$(tput smul)
	N=$(tput sgr0)

	cat << EOF
${B}NAME${N}
	play - mpv playback functions

${B}SYNOPSIS${N}
	${B}play${N} [${U}OPTION${N}] [${U}ARGS${N}...]

${B}DESCRIPTION${N}
	play is a script to manage different mpv viewing bahavior.
	If no ${U}URLS${N} argument is provided, it is read from clipboard instead.
	The queue option starts a mpv window with a playlist, which all videos of
	subsequent queue calls get added to.

${B}OPTIONS${N}
	${B}help${N}
		Display usage message and exit.

	[${B}play${N}] [${U}URLS${N}...] (default)
		Plays videos in a new mpv window.

	${B}shuffle${N} [${U}URLS${N}...]
		Shuffle audio playlist (disables video playback).

	${B}queue${N} [${U}URLS${N}...]
		Add multiple videos to the unique mpv's queue.
EOF
}

CLIP="$(xclip -selection clipboard -out)"

play() {
	MPV="mpv --ytdl-raw-options=add-metadata=,external-downloader=aria2c"
	if [ -z "$1" ]; then
		MPV="$MPV $CLIP"
		# Cut off everything after space
		LINK="$(echo "$CLIP" | sed -nE 's/^(\S+).*/\1/p')"
	else
		MPV="$MPV $*"
		# Determain which argument holds the urls
		[ "$1" = "${1#-}" ] && DISPLAY="$1" || DISPLAY="$2"
		# Cut off everything after space
		LINK="$(echo "$DISPLAY" | sed -nE 's/^(\S+).*/\1/p')"
	fi

	notify-send -t 2500 "Loading video: $LINK"
	# Attempt to load video
	[ "$($MPV)" ] && notify-send -u critical -t 4000 "Loading video failed.."
}

shuffle() {
	# Skip first argument
	shift 1
	setsid -f mpv --no-video --shuffle \
		   --ytdl-format='bestaudio[ext=m4a]' \
		   "${@:-$CLIP}"
}

queue() {
	OPTIONS="--no-terminal --force-window --input-ipc-server=$SOCK --"

	# Create mpv cache directory
	DIR="$(dirname "$SOCK")"
	[ ! -d "$DIR" ] && mkdir -p "$DIR"

	# Delete socket if no umpv is running
	if ! pgrep -f "mpv $OPTIONS" > /dev/null; then
		rm -f "$SOCK"
	fi

	# Skip first argument
	shift 1
	# Set url to argument if provided, clipboard otherwise
	URLS="${*:-$CLIP}"

	if [ -S "$SOCK" ]; then
		notify-send -t 2500 "Added video to queue.."
		# Add video to named pipe
		echo "$URLS" | awk -v RS=' ' '{ print "raw loadfile "$1" append" }' \
			| socat UNIX-CONNECT:"$SOCK" -
	else
		# Play video
		notify-send -t 2500 "Loading video: $URLS"
		# shellcheck disable=2086
		setsid -f mpv $OPTIONS $URLS
	fi
}

case "$1" in
	help)
		help
		;;
	shuffle)
		shuffle "$@"
		;;
	queue)
		queue "$@"
		;;
	*)
		play "$@"
		;;
esac
