#! /usr/bin/perl
#
# Rename "cr2" (raw) files from img_ABCD.cr2 to YYYY-MM-DD_img_ABCD.jpg, to
# match what we do when renaming files on import into Aperture.
#
# Usage:
# cd /photo/org/unsorted && renamecr2withdate | sh -x
#
# Or:
# cd /photo/org/unsorted && renamecr2withdate go
#
# Written by Ewen McNeill <ewen@naos.co.nz>, 2014-06-08
#---------------------------------------------------------------------------

use POSIX;                      # strftime()

$forreal = defined($ARGV[0]);   # Any argument is "go"

foreach my $file (glob "*.cr2") { 
    my $mtime = (stat($file))[9]; 
    my $fd = strftime("%Y-%m-%d", localtime($mtime)); 
    my $dest = "${fd}_${file}";
    if ($forreal) { 
        rename($file, $dest);
    } else {
        print "mv ${file} ${dest}\n"; 
    }
}
