#!/bin/bash

if [ "$1" = "" ]; then
	exit
fi

who=`whoami`
tmpfile=/tmp/$who-ffplay-gtk

audiosource=default
if [ -f $HOME/.ffplay-gtk ]; then
	audiosource=`cat $HOME/.ffplay-gtk | grep ^audiosource= | cut -f 2 -d '='`
fi

ffplay_kill(){
	#ps aux | grep 'ffplay ' | grep -v grep | tr -s ' ' | cut -f 2 -d ' ' | while read pidtokill; do
	#	kill -9 $pidtokill 2>/dev/null
	#done
	kill -9 `pidof ffplay` 2>/dev/null
}

xdo(){
	ffplay_pid=$!
	sleep 0.1

	if [ "`which xdotool`" != "" ]; then
		win=`xdotool search --all --name --pid $ffplay_pid ffplay 2>/dev/null`
		if [ "$win" = "" ]; then
			sleep 0.2
			win=`xdotool search --pid $ffplay_pid 2>/dev/null`
		fi
	fi
}

if [ "$1" = ffmpeg ]; then
	size=`ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$2"`
	height=`echo $size | cut -f 2 -d 'x'`
	width=`echo $size | cut -f 1 -d 'x'`
	echo $width > $tmpfile/file_width
	echo $height > $tmpfile/file_height
	exit
fi

if [ "$1" = percent ]; then
	p1=`echo "$2"`
	if [ "`echo $p1 | wc -c`" = 2 ]; then
		p1=0"$p1"
	fi
	if [ "$p1" = 100 ]; then
		p2=.99
	else	
		p2=`echo \."$p1"`
	fi
	new=`echo "$3"*"$p2" | bc -l`
	if [ "`pidof ffplay-gtk-timer`" = "" ]; then
		ffplay-gtk-timer $new $nofraction &
	fi
	if [ "$5" = video ]; then
		SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$new" "$4" `cat $tmpfile/streamstoplay` 2> $tmpfile/timecount &
	else
		SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$new" "$4" -nodisp 2> $tmpfile/timecount &
	fi
	ffplay-gtk-timer "$new" "$3" &
	exit
fi

if [ "$1" = specific ]; then
	search=`echo "$3" | cut -f 1 -d '|'`
	seconds=`cat $tmpfile/specifics | grep "$search|" | cut -f 2 -d '|' | cut -f 1 -d '.'`
	if [ "$5" = "" ]; then
		SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$seconds" "$2" -nodisp 2> $tmpfile/timecount &
	else
		#seconds=`expr "$seconds" + 4` #video doesn't seem to start at the exact entered time, will have to look in to this furthur
		SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$seconds" "$2" `cat $tmpfile/streamstoplay` 2> $tmpfile/timecount &
	fi
	ffplay-gtk-timer "$seconds" "$4" &
	$xdo
	exit
fi

if [ "$1" = check ]; then
	if [ "`cat $tmpfile/pause`" = even ]; then
		ffplay_kill
	else
		t1=`cat $tmpfile/timer | tr -c -d [0-9]`
		if [ "$t1" = "" ]; then
			t1=0
		fi
		if [ "$3" != video ]; then
			SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$t1" "$2" -nodisp 2> $tmpfile/timecount &
		else
			SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$t1" "$2" `cat $tmpfile/streamstoplay` 2> $tmpfile/timecount &
		fi
	fi
	$xdo
	exit
fi

if [ "$1" = audio ]; then
	if [ -f "$2" ]; then
		file="$2"
	else
		file="$3"
	fi
	time=`echo $2 | tr -c -d [0-9]-`
	if [ "$time" = "" ]; then
		time=0
	fi
	timer=`cat $tmpfile/timer | tr -c -d [0-9]`
	if [ "$timer" = "" ]; then
		timer=0
	fi
	new=`expr "$timer" + "$time"`
	if [ "$4" = video ]; then
		SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$new" "$file" `cat $tmpfile/streamstoplay` 2> $tmpfile/timecount &
	else
		SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$new" "$file" -nodisp 2> $tmpfile/timecount &
	fi
	$xdo
	exit
fi

if [ "$1" = even ]; then
	if [ -f $tmpfile/video ]; then #in case user pauses video with on screen command and then resumes play from the gui
		timer=`cat $tmpfile/timer | tr -c -d [0-9]` #
		if [ "$timer" = "" ]; then
			timer=0
		fi
		sleep 1 #
		timer2=`cat $tmpfile/timer | tr -c -d [0-9]` #
		if [ "$timer" != "$timer2" ]; then #
			echo even > $tmpfile/pause #
		else #
			echo odd > $tmpfile/pause #
		fi #
	fi #
	result=`cat $tmpfile/pause`
	if [ "$result" = even ]; then
		echo odd > $tmpfile/pause
	else
		echo even > $tmpfile/pause
	fi
	exit
fi

if [ "$1" = pausecheck ]; then
	if [ "`cat $tmpfile/pause | grep odd`" != "" ]; then
		echo even > $tmpfile/pause
		if [ "$2" != "" ]; then
			xdotool key --clearmodifiers --window "$2" p
		fi
	fi	
	exit
fi

sleep 0.5

if [ "$1" = timer ]; then
	seconds=`cat $tmpfile/timer | tr -c -d [0-9]`
else
	seconds=`cat $tmpfile/timecount | tr '\r' '\n' | tail -n 1 | tr -s ' ' | sed 's/^\ //' | cut -f 1 -d '.' | tr -c -d 0-9`
fi
if [ "$seconds" = "" ]; then
	seconds=0
fi
nofraction=`cat $tmpfile/nofraction`
if [ "$seconds" -gt "$nofraction" ]; then
	seconds=`expr "$nofraction" - 1`
fi

if [ "$4" != "" ]; then
	if [ "$4" != play ]; then
		s1=`echo "$4" | tr -c -d [0-9]`
		if [ "$s1" = "" ]; then
			s1=0
		fi
		seconds=`expr $seconds + "$s1"`
	else
		result=`cat $tmpfile/pause`
		if [ "$result" = odd ]; then
			exit
		fi
	fi
fi

if [ -f $tmpfile/chapter ]; then
	cat $tmpfile/chapter | cut -f 2 -d '|' > $tmpfile/chapterstart
fi

until [ "$seconds" = "$nofraction" ]; do
	if [ -f $tmpfile/chapter ]; then
		cat $tmpfile/chapterstart | while read start; do
			end=`cat $tmpfile/chapter | grep "|$start|" | cut -f 3 -d '|'`
			if [ "$seconds" -ge "$start" ]; then
				if [ "$seconds" -le "$end" ]; then
						title=`cat $tmpfile/chapter | grep "|$start|" | cut -f 1 -d '|'`
						echo "$title" > $tmpfile/chapter-title
				fi
			fi
		done
	fi
	s1=`date -u -d @$seconds +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	echo "$s1" > $tmpfile/human
	echo "$seconds" > $tmpfile/timer
	sleep 1
	seconds=`cat $tmpfile/timecount | tr '\r' '\n' | tail -n 1 | tr -s ' ' | sed 's/^\ //' | cut -f 1 -d '.'`
done

file=`cat $tmpfile/currentfile`
newfile=`cat $tmpfile/playlist | grep "$file" -A 1 | grep -v "$file" | tail -n 1`

if [ -f "$newfile" ]; then
	ffplay-gtk "$newfile"
fi

