#!/bin/bash

# EXIF to name, photo renamer v0.8c
# 08.06.2005 by bojster
# Please send any comments you may have to bojster@roger4.net

E_BADARGS=65

if [ -z "$1" ]
then
	echo "EXIF to name - converts filename to \"YYYY.MM.DD_hh:mm:ss.jpg\" format"
	echo "depending on image's EXIF data. Requires 'strings', 'grep' and 'uniq'"
	echo "to be in the path, which should be true for any *NIX system."
	echo "Usage: `basename $0` file [file2] ... [fileN]"
	echo ""
	echo "Warning: this script can (and will) produce trash when used on .bmp files!"
	echo "It was only intended for use with digital photos in JPEG format."
	echo "This issue will be addressed in future releases."
	exit $E_BADARGS
fi

echo -n "Do you want to delete original files after renaming them? (yes/no) "
read IFDELETE

until [ -z "$1" ]
do
	FDATETIME=`strings "$1" | grep ....:..:.....:..:.. | grep -v "  " | uniq | sed s/:/./1 | sed s/:/./1 | tr " " _`
	if [ -z "$FDATETIME" ]
	then
		echo "$1 contains no EXIF data, skipped."
	else	
	FNAME="$FDATETIME".jpg
	IFOW=$NULL
	
	if [ -e "$FNAME" ]
	then
		echo -n "File $FNAME exists. Overwrite? "
		read OW
		case "$OW" in
			[yY] | [yY][eE][sS]) IFOW=$NULL;;
			* ) IFOW=dont;;
		esac
	fi
	
	if [ -z "$IFOW" ]
	then
		cp "$1" "$FNAME"
		if [ "$?" -ne 0 ]
		then
			echo "Error while saving file $FNAME, skipped."
		else
			echo -n "$1 saved as $FNAME"
			case "$IFDELETE" in
				[yY][eE][sS] ) rm "$1"; echo " and deleted.";;
				* ) echo ".";;
			esac
		fi
	else
		echo "$1 skipped."
	fi
	fi
	shift
	
done
	
exit 0

