#!/bin/bash

# script have to:
# launch tutubedl with a know name in background
# an mplayer that is playing through the known name suffix .part and cache=32
dirzik=${dirzik:-~/ziks/_fromFLV}
if [ ! -e "$dirzik" ]; then
	dirzik=./
fi
if [ -z "$1" ]; then
	echo "no url given"
	exit 1
fi

url=$@
shift

tmp=${url/*v=/}
id=${tmp/&*/}
echo $id
#'[download] Destination:'
( youtube-dl --no-part -o '%(id)-s.ytb' $url >/dev/null ; read ) &
limit=0
while [ ! -f ${id}.ytb ]; do
	((limit++))
	if [ $limit -gt 15 ]; then
		echo "failed in process"
		exit 1
	fi
	sleep 0.3s
	echo -n .
done
mplayer -quiet -cache 32 -ao jack:name=mpplive -- ${id}.ytb >/dev/null
echo -n "Keep it or not?"
TMOUT=10
read
if [ "$REPLY" == "o" -o "$REPLY" == "y" -o -z "$REPLY" ]; then
	echo -n "Type in Author - Title : "
	TMOUT=0
	read
	if [ "$REPLY" == "" ]; then
		exit 0
	fi
	destname=$dirzik/$REPLY.flv
	mv -vi -- ${id}.ytb "$destname"
	sleep 3s
else
	rm -- ${id}.ytb
fi
