#! /bin/sh

# 'rename_photos.sh'
#
# A script to rename EXIF jpg files with their date/time stamp.
#
# Most digital cameras store photos with the date/time stamp when
# the picture was taken.
# The present script searches for a string that "looks like a date"
# and renames the file based on ISO8601 notation (yyyyMMddThhss).
#
# Potential duplicates (files taken at the same second) are
# renamed as file.jpg, filea.jpg, fileaa.jpg ... you get the idea.
#
# -----------------------------------------------------------------
# NOTE: the date/time information is obtained simply by filtering
# suitable strings from the file, and keeping the first of these.
# This MAY lead to errors, since EXIF knows at least the three tags
# CreateDate, Date/Time Original, and ModifyDate.
#
# If this causes problems to you, use 'exiftool' (from the Perl module
# Image::ExifTools) and a command line like:
#
# exiftool '-FileName<${CreateDate}' -d %Y%m%dT%H%M%S.%%e *.jpg
#
# -----------------------------------------------------------------
# Arguments: Name or wildcard of the file(s) to convert.
# -----------------------------------------------------------------
# Creation:  2001-12-16, JHa (first "draft").
# Revisions: 2002-01-23, JHa (automatic setting of year)
# 	     2002-08-24, JHa (now includes also the seconds)
# 	     2003-02-20, JHa (minor fix in cmd line reading)
# 	     2003-12-23, JHa (fix for multiple dates in file)
# 	     2004-08-04, JHa (better date recognition)
# 	     2007-05-08, JHa (added info about 'exiftool';
#                         license now GPL v2)
#        2008-10-04, JHa (added "head -1" to deal with Ricoh images)
#        2011-01-05, JHa, fix renaming inside directory structures
# -----------------------------------------------------------------
# Copyright (c) 2001...2011 Joerg Hau <joerg.hau(at)dplanet.ch>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# -----------------------------------------------------------------

if (( $# < 1 )) ;  then
    echo "Syntax: ${0##*/} file1 [file2 ...] "
    exit 1
fi

for i in "$@"; do
    # get file date/time from EXIF data and build new filename
    # (change this if photos date before y2k or after 2099 ;-)
    # 'grep "20[0-9][0-9]:[0-9][0-9]:[0-9][0-9]"' searches for a date,
    # 'tail -1' prints only the last entry, in case there are
    # multiple unique date strings in the file (e.g. introduced
    # by the image treatment software). Usually the "last" string
    # is the date-time stamp we want ... but YMMV!
    #
    #FN=`strings $i | grep "20[0-9][0-9]:[0-9][0-9]:[0-9][0-9]" | uniq | head -1 | tr -d : | tr ' ' T | tail -1`
    FN=`strings $i | grep "20[0-9][0-9]:[0-9][0-9]:[0-9][0-9]" | uniq | tr -d : | tr ' ' T | tail -1`

    # avoid overwriting of files with same date-time stamp
    # (simply attach an 'a' to the file name)
    #
    # this should rarely happen anyway, as it requires the same second ;-)
    #
    while [ -e ${FN}.jpg ]
    do
	FN=${FN}a
    done

    # now rename the file. A simple "mv" would not preserve the path!
    #
    DEST=`dirname $i`/${FN}.jpg 
    echo -n "Moving $i to $DEST ..."
    mv $i ${DEST}

    # check status of "mv", abort if not OK
    #
    if [ $? != 0 ]; then
        return="1"
        break
    fi
    echo " done."
done

# set an exit status.
test "$return" = "0" || exit 1
echo $return
exit 0

