#!/bin/sh
#
#	Author: Timothy J. Luoma
#	Email:	luomat at gmail dot com
#	Date:	2009-03-05
#
#	Purpose: 


PATH=$HOME/bin:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/X11R6/bin:/root/bin:/usr/X11/bin:/usr/local/git/bin


# We use this later if something goes wrong

die () {

	echo "$@"
	exit 1
}




PURPOSE="

$NAME: 
The purpose of this program is to illustrate a bug in AtomicParsley 
which I have outlined here:

http://tntluoma.com/?p=1285

Summary: If you take a file (.m4v) which was ripped with HandBrake and apply
metadata tags to it using AtomicParsley...

...and then apply new tags to the second file...

the file that you are left with will be left without video.

"

if [ "$#" != "1" ]
then

	echo "Usage: $NAME <FILE>
	where <FILE> is a filename that you want to add tags to with AtomicParsley.
	
	Use \"$NAME --help\" for an explanation as to the purpose of this program
	"
		
		exit 1
fi

if [ "$#" = "--help" ]
then

	echo "$PURPOSE"
	exit 0
fi


# Make sure that AtomicParsley is installed
which AtomicParsley > /dev/null

if [ "$?" != "0" ]
then

	echo "$NAME: Could not find AtomicParsley installed in"
	echo "$PATH" | tr ':' '\012'
	echo "
	You can download it here:
	https://sourceforge.net/project/showfiles.php?group_id=153768
	"
	exit 1

fi

if [ ! -f "$1" ]
then

	echo "$NAME: $1 does not exist or is not a file"
	exit 1

fi


# Get the extension
EXT=`echo $1 | sed 's#.*\.##g'`

FILENAME=`basename $1 .$EXT`


TIME1=`date '+%s'`

sleep 2

TIME2=`date '+%s'`

FILE1=$FILENAME.$TIME1.$EXT

FILE2=$FILENAME.$TIME2.$EXT


if [ -f "$FILE1" -o -f "$FILE2" ]
then

	echo "$NAME: Hrm. Found a file named $FILE1 or $FILE2"
	echo "That's very strange. Try running \"$NAME $@\" again."
	exit 1

fi

echo "$NAME: I am now going to create $FILE1 and $FILE2. This will take a few minutes."

AtomicParsley "$1" \
	--AppleTV \
	--TVShowName "This is a Test" \
	--output  "$FILE1" && \
	AtomicParsley "$FILE1" -t && \
AtomicParsley "$FILE1" \
	--AppleTV \
	--year 2009 \
	--output  "$FILE2" && \
	AtomicParsley "$FILE2" -t 	|| die "$NAME: FATAL ERROR Converting $1 Error code: $?"

echo "$NAME: Done

I will now attempt to open $FILE1 and $FILE2 in Quicktime Player. 

If video WORKS in
	$FILE1
and
If video DOES NOT WORK in 
	$FILE2
then the bug is confirmed.

"

sleep 3

open -a "Quicktime Player" "$FILE1" "$FILE2"


exit 0
# EOF

