OLDIFS=$IFS
IFS=$'\n'
cd "/Volumes/Mac Pro Storage/9-Sync Library"

if [ -e "/Volumes/Mac Pro Storage/0-Temp Files/flag.sync-library" ] ;
	then
 # 		echo Sync in process!
  		exit
	else
		touch "/Volumes/Mac Pro Storage/0-Temp Files/flag.sync-library"
fi

for SOURCE in $(find * -maxdepth 0 -print); do
   echo "Syncing $SOURCE"
   sleep 1
filename=$(basename "$SOURCE")
filename2=$((basename "$SOURCE") | sed s/" - "/";"/g )
echo "$filename2" > "/Volumes/Mac Pro Storage/0-Temp Files/synclibrary.txt"
comment=$((((grep "$filename2" "/Volumes/Mac Pro Storage/0-Temp Files/synclibrary.txt" ) | cut -d \; -f 4 ) | sed s/".m4v"//g ) | sed s/".mp4"//g )
genre=$(((((grep "$filename2" "/Volumes/Mac Pro Storage/0-Temp Files/synclibrary.txt" ) | cut -d \; -f 6 ) | sed s/".m4v"//g ) | sed s/".mp4"//g ) | cut -c 1-5 )
if [ -f "/Volumes/Master Bedroom iTunes Media/_Master Bedroom Unadded/.flag" ] ;
	then
		if [ "$genre" = "Adult" ] ;
			then
			echo "**********SYNCING TO MASTER BEDROOM ATV SERVER"
			rsync -ru --progress "$SOURCE" "/Volumes/Master Bedroom iTunes Media/Automatically Add to iTunes/" --exclude="*iTouch*" --exclude=".*" --exclude=".DS*" --exclude="*.txt"
		fi
	else
		echo "$filename NOT ADDED TO Master Bedroom SERVER" >> "/Users/joel/Documents/Optimize Log.txt"
		cp -Rv "$SOURCE" "/Volumes/Mac Pro Storage/7-Waiting Remote Storage/"
fi
if [ -f "/Volumes/Living Room iTunes Media/_Living Room Unadded/.flag" ] ;
	then
		echo "**********SYNCING TO LIVING ROOM ATV SERVER"
		rsync -ru --progress "$SOURCE" "/Volumes/Living Room iTunes Media/Automatically Add to iTunes/" --exclude="*iTouch*" --exclude="* - Adult*" --exclude=".*" --exclude=".DS*" --exclude="*.txt"
	else
		echo "$filename NOT ADDED TO Living Room SERVER" >> "/Users/joel/Documents/Optimize Log.txt"
		if [ -f "/Volumes/Mac Pro Storage/7-Waiting Remote Storage/$filename" ] ;
			then
				echo "" > ".echo"
			else
				cp -Rv "$SOURCE" "/Volumes/Mac Pro Storage/7-Waiting Remote Storage/"
		fi
fi
if [ -f "/Volumes/Mac Pro eSata Array 01/.flag" ] ;
	then
		echo "***********Syncing to Main Library"
		rsync -ru --progress "$SOURCE" "/Volumes/Mac Pro eSata Array 01/iTunes Media/Automatically Add to iTunes/"
#		mv "$SOURCE" "/Volumes/Mac Pro Storage/8-Main Storage/"
#		mv "$SOURCE" "/Volumes/Mac Pro Storage/7-Waiting Remote Storage/"
	else
		echo "***********Main Library NOT Available"
		cp "$SOURCE" "/Volumes/Mac Pro Storage/8-Main Storage/"
fi
if [ -f "/Volumes/Back-Up Encoded Files/.flag" ] ;
	then
		echo "**********SYNCING TO Encoded Files Back-Up"
		rsync -ru --progress "$SOURCE" "/Volumes/Back-Up Encoded Files/"
	else
		echo "***********Encoded Files Drive NOT Available"
		mv "$SOURCE" "/Volumes/Mac Pro Storage/10-Encoded Files Storage/"
fi
rm -v "$SOURCE"
echo "*********DELETING TEMPORARY FILES"
rm ".echo"
done
rm "/Volumes/Mac Pro Storage/0-Temp Files/synclibrary.txt"
rm "/Volumes/Mac Pro Storage/0-Temp Files/flag.sync-library"
IFS="$OLDIFS"
