
# variables
xpathPath="/usr/bin/xpath"
+ xpathPath=/usr/bin/xpath
scriptPID=$$
+ scriptPID=78703
batchRipSupportPath="${HOME}/Library/Application Support/Batch Rip"
+ batchRipSupportPath='/Users/ema/Library/Application Support/Batch Rip'
currentItemsList="${batchRipSupportPath}/currentItems.txt"
+ currentItemsList='/Users/ema/Library/Application Support/Batch Rip/currentItems.txt'
actionPath=`dirname "$0"`
dirname "$0"
++ dirname '/Applications/Batch Rip Actions for Automator.app/Contents/Library/Automator/Batch Rip.action/Contents/Resources/main.command'
+ actionPath='/Applications/Batch Rip Actions for Automator.app/Contents/Library/Automator/Batch Rip.action/Contents/Resources'
curlCmd=$(echo "curl -Ls --compressed")
echo "curl -Ls --compressed"
++ echo 'curl -Ls --compressed'
+ curlCmd='curl -Ls --compressed'

# create action temp folder
tmpFolder="/tmp/batchRipLauncher-${scriptPID}"
+ tmpFolder=/tmp/batchRipLauncher-78703
if [ ! -e "$tmpFolder" ]; then
	mkdir "$tmpFolder"
fi
+ '[' '!' -e /tmp/batchRipLauncher-78703 ']'
+ mkdir /tmp/batchRipLauncher-78703

# Make batchRip folder
if [ ! -d "$batchRipSupportPath" ]; then
	mkdir "$batchRipSupportPath"
fi
+ '[' '!' -d '/Users/ema/Library/Application Support/Batch Rip' ']'

# multi-disc prompt
if [[ ! "${autoRun}" ]]; then autoRun=0; fi
+ [[ ! -n 0 ]]
	# Get current state of Batch Rip Dispatch LaunchAgent; if enabled continue.
	currentState=`launchctl list com.batchRip.BatchRipDispatcher 2> /dev/null`
launchctl list com.batchRip.BatchRipDispatcher 2> /dev/null
++ launchctl list com.batchRip.BatchRipDispatcher
+ currentState=
	if [ ! -z "$currentState" ]; then
		# Get count of optical drives
		deviceCount=`ioreg -iSr -w 0 -c IODVDBlockStorageDevice | grep "Device Characteristics" | sed -e 's|.*"Product Name"="||' -e 's|".*||' | grep -c ""`
		if [[ deviceCount -gt 1 && "$autoRun" -eq 0 ]]; then
		launchAction=`appleScriptDialogContinue`
		if [[ "$launchAction" = "Cancel" || -z "$launchAction" ]]; then
			if [ -d "$tmpFolder" ]; then
				rm -rf $tmpFolder
			fi
			exit 0
		elif [[ "$launchAction" = "Ignore" ]]; then
			df -T udf | grep "Volumes" | awk -F\ / {'print $2'} | sed -e 's|^|\/|g' -e 's|$|:Ignore|g' > "$currentItemsList"
			if [ -d "$tmpFolder" ]; then
				rm -rf $tmpFolder
			fi
			exit 0
		fi
	fi
fi
+ '[' '!' -z '' ']'
if [ -e "$currentItemsList" ]; then
	rm -f "$currentItemsList"
fi
+ '[' -e '/Users/ema/Library/Application Support/Batch Rip/currentItems.txt' ']'

input=`cat`
cat
++ cat
+ input=

if [ ! -z "$input" ]; then
	input=`echo "$input" | tr ' ' '\007'`
else
	input=`df -T udf | grep "Volumes" | awk -F\ / {'print $2'} | sed 's|^|\/|g' | tr ' ' '\007' | sort -f`	
fi
+ '[' '!' -z '' ']'
df -T udf | grep "Volumes" | awk -F\ / {'print $2'} | sed 's|^|\/|g' | tr ' ' '\007' | sort -f
++ df -T udf
++ grep Volumes
++ awk '-F /' '{print $2}'
++ sed 's|^|\/|g'
++ tr ' ' '\007'
++ sort -f
+ input=/Volumes/TANGLED

for eachItem in $input
do
	eachItem=`echo "$eachItem" | tr '\007' ' '`
	itemCount=$((itemCount + 1))	
done
+ for eachItem in '$input'
echo "$eachItem" | tr '\007' ' '
++ echo /Volumes/TANGLED
++ tr '\007' ' '
+ eachItem=/Volumes/TANGLED
+ itemCount=1

processDisc=0
+ processDisc=0

for thePath in $input
do
	thePath=`echo "$thePath" | tr '\007' ' '`
	# Check disc type and set variables
	discType=`diskutil info "$thePath" | grep "Optical Media Type" | sed 's|.*: *||'`
	if [[ "$discType" = "BD-ROM" && bdRom -eq 1 || "$discType" = "DVD-ROM" && dvdRom -eq 1 ]]; then
		if [[ ! "${skipDuplicates}" ]]; then skipDuplicates=0; fi
		if [[ ! "${autoRun}" ]]; then autoRun=0; fi
		if [[ ! "${saveLog}" ]]; then saveLog=0; fi
		if [[ ! "${bdRom}" ]]; then bdRom=0; fi
		if [[ ! "${dvdRom}" ]]; then dvdRom=0; fi
		if [[ ! "${growlMe}" ]]; then growlMe=0; fi
		if [[ ! "${useOnlyMakeMKV}" ]]; then useOnlyMakeMKV=0; fi
		if [[ ! "${ejectDisc}" ]]; then ejectDisc=0; fi
		if [[ ! "${scriptPath}" ]]; then scriptPath=""; fi
		if [[ ! "${tvPath}" ]]; then tvPath=""; fi
		if [[ ! "${moviePath}" ]]; then moviePath=""; fi
		if [[ ! "${fairmountPath}" ]]; then fairmountPath=""; fi
		if [[ ! "${makemkvPath}" ]]; then makemkvPath=""; fi
		if [[ ! "${videoKind}" ]]; then videoKind="0"; fi
		if [[ videoKind -eq 0 ]]; then videoKind="Movie"; fi
		if [[ videoKind -eq 1 ]]; then videoKind="TV Show"; fi
		if [[ ! "${tvMinTime}" ]]; then tvMinTime=0; fi
		if [[ ! "${tvMaxTime}" ]]; then tvMaxTime=0; fi
		if [[ ! "${movieMinTime}" ]]; then movieMinTime=0; fi
		if [[ ! "${movieMaxTime}" ]]; then movieMaxTime=0; fi
		if [[ ! "${discDelay}" ]]; then discDelay=20; fi
		if [[ ! "${copyDelay}" ]]; then copyDelay=20; fi
		if [[ ! "${fullBdBackup}" ]]; then fullBdBackup=0; fi
		if [[ ! "${renameDisc}" ]]; then renameDisc=1; fi
		
		# Set path to batchRip.sh
		scriptPath="${actionPath}/batchRip.sh"

		# Check if this disc is currently being processed
		bashPID=`ps uxc | grep -i "Bash" | awk '{print $2}'`
		for eachPID in $bashPID
		do
			if [ -e "/tmp/batchRip-${eachPID}/currentItems.txt" ]; then
				if grep "$thePath" < /tmp/batchRip-$eachPID/currentItems.txt ; then
					continue
				fi
			fi
		done

		# Temporarily replace spaces in paths
		fairmountPath=`echo "$fairmountPath" | tr ' ' ':'`
		makemkvPath=`echo "$makemkvPath" | tr ' ' ':'`
		moviePath=`echo "$moviePath" | tr ' ' ':'`
		tvPath=`echo "$tvPath" | tr ' ' ':'`
		
		# Set scriptArgs
		scriptArgs="--skipDuplicates $skipDuplicates --encodeHdSources $bdRom --saveLog $saveLog --fairmountPath $fairmountPath --makemkvPath $makemkvPath --movieOutputDir $moviePath --tvOutputDir $tvPath --encodeDvdSources $dvdRom --growlMe $growlMe --onlyMakeMKV $useOnlyMakeMKV --ejectDisc $ejectDisc --minTrackTimeTV $tvMinTime --maxTrackTimeTV $tvMaxTime --minTrackTimeMovie $movieMinTime --maxTrackTimeMovie $movieMaxTime --discDelay $discDelay --copyDelay $copyDelay --fullBdBackup $fullBdBackup"
					
		# Process discs if set to not run automatically
		if [[ ! autoRun -eq 1 ]]; then
			
			# Get user input for action
			getVideoKind=`appleScriptDialogVideoKind "$thePath" "$discType"`

			# If video kind is returned, setup and launch batchRip. If ignore is returned, set to ignore.
			if [[ ! "$getVideoKind" = "Cancel" && ! -z "$getVideoKind" ]]; then
				videoKind="$getVideoKind"
				processDisc=1
				# If renameDisc is set to yes, get disc name from user input
				if [[ $renameDisc -eq 1 ]]; then
					# reset variables
					newDiscName=""
					theTitle=""
					theSeriesName=""
					nameWithSeasonAndDisc=""
					discName=`basename "$thePath" | tr '_' ' ' | sed 's| ([0-9]*)||'`
					getDiscName=`displayDialogGetMovieName "$thePath" "$discName" "$videoKind"`
					if [[ ! -z "$getDiscName" && ! "$getDiscName" = "Skip" ]]; then
						if [ "$videoKind" = "Movie" ]; then
							titleList=`tmdbGetMovieTitles "$getDiscName"`
							if [ ! "$titleList" = "" ]; then
								theTitle=`displayDialogChooseTitle "$titleList"`
								if [[ ! "$theTitle" = "false" && ! "$theTitle" = "" ]]; then
									newDiscName=`echo "$theTitle" | sed 's|\&amp;|\&|g'`
								fi
							else
								theTitle=`displayDialogCustomDiscName "$thePath" "$getDiscName" "$videoKind"`
								if [[ ! "$theTitle" = "false" && ! "$theTitle" = "" ]]; then
									newDiscName=`echo "$theTitle" | sed 's|\&amp;|\&|g'`
								else
									newDiscName="$discName"
								fi
							fi
						elif [ "$videoKind" = "TV Show" ]; then
							titleList=`tvdbGetSeriesTitles "$getDiscName"`
							if [ ! "$titleList" = "" ]; then
								theSeriesName=`displayDialogChooseTitle "$titleList"`
								if [[ ! "$theSeriesName" = "false" && ! "$theSeriesName" = "" ]]; then
									theSeriesName=`echo "$theSeriesName" | sed -e 's| - First Aired.*$||g' -e 's|\&amp;|\&|g'`
									nameWithSeasonAndDisc=`displayDialogGetSeasonAndDisc "$theSeriesName" "$thePath"`
									if [ ! -z "$nameWithSeasonAndDisc" ]; then
										newDiscName="${theSeriesName} - ${nameWithSeasonAndDisc}"
									fi
								fi
							else
								theSeriesName=`displayDialogCustomDiscName "$thePath" "$getDiscName" "$videoKind"`
								if [[ ! "$theSeriesName" = "false" && ! "$theSeriesName" = "" ]]; then
									nameWithSeasonAndDisc=`displayDialogGetSeasonAndDisc "$theSeriesName" "$thePath"`
									if [ ! -z "$nameWithSeasonAndDisc" ]; then
										newDiscName="${theSeriesName} - ${nameWithSeasonAndDisc}"
									else
										newDiscName="$discName"
									fi
								else
									newDiscName="$discName"
								fi
							fi
						fi
					else
						newDiscName="$discName"
					fi
				fi
				
				if [ ! "$newDiscName" = "" ]; then
					echo "${thePath}:${videoKind}:${newDiscName}" >> "$currentItemsList"
				else
					echo "${thePath}:${videoKind}" >> "$currentItemsList"
				fi
			else
				echo "${thePath}:Ignore" >> "$currentItemsList"
			fi
			if [[ $itemCount -eq 1 && $processDisc -eq 1 ]]; then
				#osascript -e "tell application \"Automator Runner\" to display dialog \"$itemCount\""
				runScript "$scriptPath" "$scriptArgs"
			fi
		else
			# Process discs if set to run automatically
			echo "${thePath}:${videoKind}" >> "$currentItemsList"			
			if [[ $itemCount -eq 1 ]]; then
				#osascript -e "tell application \"Automator Runner\" to display dialog \"RUN SCRIPT\""
				runScript "$scriptPath" "$scriptArgs"
			fi
		fi	
	else
		exit 0
	fi
	itemCount=$((itemCount - 1))
done
+ for thePath in '$input'
echo "$thePath" | tr '\007' ' '
++ echo /Volumes/TANGLED
++ tr '\007' ' '
+ thePath=/Volumes/TANGLED
diskutil info "$thePath" | grep "Optical Media Type" | sed 's|.*: *||'
++ diskutil info /Volumes/TANGLED
++ grep 'Optical Media Type'
++ sed 's|.*: *||'
+ discType=
+ [[ '' = \B\D\-\R\O\M ]]
+ [[ '' = \D\V\D\-\R\O\M ]]
+ exit 0
