#!/bin/sh
#
# $Id$
#
# Rename a picture taken with the Digital Camera File specification to
# use a more Unix-friendly name.
#
# Usage:
#
USAGE='Usage: camrename [-d] [-v] jpeg-files'
#
# Each given file is renamed. If the existing name begins or ends in
# four digits (preference given to ending), those four digits will be
# preserved in the new name as "seq"; otherwise "seqn" will be set to
# ensure a unique filename, starting at 0001 or one more than the
# largest existing sequence number. The remainder of the name will be
# created as follows:
#
# Pyyyy.mm.dd-hh.mm.ss-seqn.jpg
# ^^^^ ^^ ^^ ^^ ^^ ^^ ^^^^
# | | | | | | |
# | | | | | | +--- Image sequence number
# | | | | | |
# | | | | | +------- Seconds of image creation date/time
# | | | | |
# | | | | +---------- Minutes of image creation date/time
# | | | |
# | | | +------------- Hours of image creation date/time
# | | |
# | | +---------------- Day of month image created
# | |
# | +------------------- Month image created
# |
# +----------------------- Year image created
#
#
# As a side effect, to compensate for MS-DOS stupidity, the execute
# permissions are turned off on all renamed files.
#
# Options:
#
# -d Debug: report what would be done, but don't do it.
# -v Verbose: report each rename as it is done.
#
# Modified by Bjoern Olausson to work with exiftool instead of Metacam
debug=false
verbose=false
#
# Argument processing
#
while [ $# -gt 0 ]
do
case "$1" in
-d)
debug=true
verbose=true
;;
-v)
verbose=true
;;
--)
shift
break
;;
-*)
echo "$USAGE" 1>&2
exit 2
;;
*)
break
;;
esac
shift
done
case "$#" in
0)
echo "$USAGE" 1>&2
exit 2
;;
esac
#
# Process all file arguments
#
TMP=/tmp/cre$$
trap "rm -f $TMP.*; exit 1" 1 2 15
trap "rm -f $TMP.*; exit 0" 13
for file ; do
dir=`dirname $file`
base=${file%.*}
origExtension=${file##*.}
extension=$(tr "A-Z" "a-z" <<< $origExtension)
#
# Extract file information with metacam.
#
if ! exiftool -TAG -CreateDate "$file" > $TMP.a 2>&1
then
echo "Can't extract exif-information from '$file', skipping" 1>&2
continue
fi
datePart=`awk '/Create Date/{print $4" "$5}' $TMP.a | sed -e 's/ /-/;s/:/./g'`
#
# Figure out new name, with *lots* of safety checks.
#
case "$base" in
*[0-9][0-9][0-9][0-9])
seq=`expr "$base" : '.*\([0-9][0-9][0-9][0-9]\)$'`
;;
[0-9][0-9][0-9][0-9]*)
seq=`expr "$base" : '\([0-9][0-9][0-9][0-9]\).*$'`
;;
*)
topFile=`ls $dir/P$datePart-*.$extension 2>/dev/null | tail -1`
case "$topFile" in
"$dir/P$datePart-*.$extension")
seq=0001
;;
*)
seq=`expr "$topFile" \
: ".*/P$datePart-\([0-9][0-9][0-9][0-9]\).$extension"'$'`
if [ "X$seq" = X ]
then
seq=0001
fi
;;
esac
while [ -e "$dir/P$datePart-$seq.$extension" ]
do
seq=`echo $seq | awk '{printf "%4.4d
", $1 + 1}'`
if [ $seq -gt 9999 ]
then
echo "Can't find unique sequence number to rename $file " \
"to $dir/P$datePart-*.$extension; not renamed" 1>&2
continue
fi
done
;;
esac
target="$dir/P$datePart-$seq.$extension"
if [ "$target" = "$dir/$base.$origExtension" ]
then
# Already in proper format
echo "$base is already in proper format; skipped" 1>&2
continue
elif [ -e "$target" ]
then
echo "$target already exists; $file not renamed" 1>&2
continue
fi
#
# Finally, we're ready to do the rename.
#
$verbose && echo mv "$file" "$target"
if ! $debug
then
mv "$file" "$target"
chmod 644 "$target"
fi
done
rm -f $TMP.*