#!/bin/bash
# This script emulates "unique application" functionality on Linux. When starting
# playback with this script, it will try to reuse an already running instance of
# mpv.
# Copyright Jean-Philippe Guillemin - zenwalk.org <h1p8r10n@gmail.com> - GPL v2

if [ "$(pidof mpv)" ] ; then 
	echo "raw loadfile \"$@\"" > ~/.mpv_fifo
	exit 0
else
	rm -f ~/.mpv_fifo
	mkfifo --mode=666 ~/.mpv_fifo
	if [ "$@" ] ; then
		if [ "$(xdg-mime query filetype "$@" | grep -i 'video\|realmedia' )" ] ; then
			exec /usr/bin/mpv --geometry=720x480 --profile=pseudo-gui --input-file=~/.mpv_fifo "$@"
		else
			exec /usr/bin/mpv --geometry=300x100 --profile=pseudo-gui --input-file=~/.mpv_fifo "$@"
		fi
	else	
		exec /usr/bin/mpv --force-window --geometry=720x480 --profile=pseudo-gui --input-file=~/.mpv_fifo "$@"
	fi	
fi
