#! /bin/bash

# USE AT YOUR OWN RISK!

# This script will go through your iPhoto Library 
# original files and copy them to $EXPORTDIR.
# The script will organize the files in directories
# named YYYY/YYYY-MM and will rename the files using 
# EXIF data ("YYYY-MM-DD HH.MM.SS.EXT", like dropbox 
# camera import does).

# Written by Panayotis Vryonis, 2013.
# http://blog.vrypan.net/2013/5/20/leaving-iphoto-for-dropbox/

# Distributed under MIT License. 
# <http://opensource.org/licenses/MIT>

# USE AT YOUR OWN RISK!

# Make sure you have exiftool installed first
# http://www.sno.phy.queensu.ca/~phil/exiftool/

# UNCOMMENT AND EDIT THE FOLLOWING TWO LINES!!!!!!
# IPHOTOPATH="$HOME/Pictures/iPhoto Library"
# EXPORTDIR="$HOME/photo_export"

if [ ! "$IPHOTOPATH" ] || [ ! "$EXPORTDIR" ]; 
then
	echo Please edit script and set \$IPHOTOPATH and \$EXPORTDIR.
	exit
fi

find "$IPHOTOPATH/Masters" -type f | grep -v .DS_Store > filelist.txt

total_files=`cat filelist.txt | wc -l | tr -d ' '`

fn=0

echo 
echo Copying $total_files media files media files 
echo from \"$IPHOTOPATH/Masters\"
echo to \"$EXPORTDIR\".
echo

cat filelist.txt | while read fullfile ; do
	let fn+=1
	let percent=100*$fn/$total_files

	# Thanks:
	# http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
	filename="${fullfile##*/}"
	extension="${filename##*.}"
	filename="${filename%.*}"

	#echo -ne "\033[K"
	printf "Progress: %3d%% " $percent 
	echo -ne "[$fn/$total_files]\n"
        echo -e " > $fullfile\033[K"

	dates=(`exiftool \
		-CreateDate \
		-MediaCreateDate \
		-DateTimeOriginal \
		-FileModifyDate \
		-d '%Y-%m-%d %H.%M.%S' \
		-S -s "$fullfile"`)

	thedate="${dates[0]} ${dates[1]}" 

	filename2=$thedate
	path2="$EXPORTDIR/${thedate:0:4}/${thedate:0:7}"


	if [ ! -d "$path2" ];
	then
		mkdir -p "$path2"
	fi

	if [ ! -f "$path2/$filename2.$extension" ] ; 
	then
		newfile="$path2/$filename2.$extension"
	else
		i=1
		until [ ! -f "$path2/$filename2-$i.$extension" ]; do
			let i+=1
		done
		newfile="$path2/$filename2-$i.$extension"
	fi

        echo -e " < $newfile\033[K"
        echo -ne "\r\033[3A"

	cp "$fullfile" "$newfile"

	newfile=""
done

echo -e "\nDone.\033[K"
echo -e "Check $EXPORTDIR.\033[K"
