#!/bin/bash
#
# dir_date_serial_rename_all.sh
#
# Version : 0.2
# Latest version is always available at http://www.ruwenzori.net/code/dir_date_serial_rename_all/
#
# What :
# This script takes all files in the current directory and renames them
# using the following pattern :
#
#       date.time.personal_extension.camera_serial.current_directory_name
#
# Example :
#
#    "my current directory/img_6051.jpg" taken the 2nd of December
#    2005 at 9:07:59 AM (according to the embedded EXIF metadata)
#
#    becomes
#
#    "my current directory/20051202.090759.JML.6051.my_current_directory.jpg"
#
# The defaults used in the user-configurable variables
# (file_name_extension=jpg and camera_image_file_prefix=img_) work for
# files from Canon digital cameras. Adjust these settings according to
# your own files.
#
# Dependancies :
# - jhead
# - sed
# - awk
#
# Author : Jean-Marc Liotier
# Inspired by Ralph Jensen at http://www.photo.net/bboard/q-and-a-fetch-msg?msg_id=00ERah
#
# Changelog :
# 0.1   20051209        Initial version
# 0.1.1 20051211        Spaces in directory name are now converted to underscores in file name.
# 0.2   20070322        Field order has been changed in order to fit even more situations
#                       The script now tolerates upper and lower case file name extensions
#                       Prefix can be personalized : your initials probaly make more sense than "IMG"
#                       Extension case set is now set according to user configuration


#
#
# License
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.

# User configurable variables (case insensitive)
file_name_extension=jpg
camera_image_file_prefix=img_
personal_image_file_prefix=JML

current_dir_name=`pwd | sed s/\\\//\\\n/g | tail -n 1 | sed s/\ /_/g`

ls -1 . | grep -i $file_name_extension | while read zefile
    do
        zename=`echo $zefile | awk -F "." '{print($1)}' \
                | sed s/$camera_image_file_prefix//g`
        zedate=`jhead $zefile | grep 'Date/Time' \
                | awk -F " " '{print($3"."$4)}' \
                | sed s/://g`
        zenewname=$zedate.$personal_image_file_prefix.$zename.$current_dir_name.$file_name_extension
        mv $zefile $zenewname
    done