#!/bin/bash

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

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

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
		#pkill -P $pidtokill
	done
	killall 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" = 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`
	ffplay_kill
	#ffplay-gtk-timer ffkill
	if [ "`ps aux | grep ffplay-gtk-timer | grep -v grep`" = "" ]; then
		ffplay-gtk-timer $new $nofraction &
	fi
	if [ "$5" = video ]; then
		ffplay -ss "$new" "$4" 2> $tmpfile/timecount &
	else
		ffplay -ss "$new" "$4" -nodisp 2> $tmpfile/timecount &
	fi
	ffplay-gtk-timer "$new" "$3" &
	exit
fi

if [ "$1" = ffkill ]; then
	ffplay_kill
	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
		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
		ffplay -ss "$seconds" "$2" 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`
		if [ "$3" != video ]; then
			ffplay -ss "$t1" "$2" -nodisp 2> $tmpfile/timecount &
		else
			ffplay -ss "$t1" "$2"  2> $tmpfile/timecount &
		fi
	fi
	$xdo
	exit
fi

if [ "$1" = audio ]; then
	timer=`cat $tmpfile/timer`
	new=`expr "$timer" + "$2"`
	if [ "$4" = video ]; then
		ffplay -ss "$new" "$3" 2> $tmpfile/timecount &
	else
		ffplay -ss "$new" "$3" -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` #
		sleep 1 #
		timer2=`cat $tmpfile/timer` #
		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" = kill ]; then
	tokill=`ps aux | grep '/usr/sbin/ffplay-gtk-timer' | grep -v grep | tr -s ' ' | cut -f 2 -d ' '`
	echo "$tokill" | while read pid; do
		kill -9 $pid 2>/dev/null
	done
	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

seconds=`cat $tmpfile/timecount | tr '\r' '\n' | tail -n 1 | tr -s ' ' | sed 's/^\ //' | cut -f 1 -d '.' | tr -c -d 0-9`
if [ "$seconds" = "" ]; then
	seconds=0
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 [ "`echo $seconds | grep '-'`" ] || [ "$seconds" = "" ]; then
	seconds=0
fi

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

until [ "$seconds" = "$2" ]; 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

