#! /bin/sh
# Tidy up photo filenames in a directory hierachy
#
# In particular this is designed to fix up imports from FAT file systems
# which are in upper case and put them into lower case for sane interactive
# use.
#
# .JPG
# .CR2
#
# files are renamed to the lower case variant.  All image files have their
# permissions corrected.
#
# Written by Ewen McNeill <ewen@naos.co.nz>, 2007-01-20
# Updated by Ewen McNeill <ewen@naos.co.nz>, 2014-06-01
#---------------------------------------------------------------------------

# Fix filenames
find . -type f -name "*.[JC][PR][G2]" | 
  while read FILE; do
    mv "$FILE" $(echo "$FILE" | tr 'A-Z' 'a-z')
  done

# Fix permissions
find . -type f -name "*.[JjCc][PpRr][Gg2]" | 
  while read FILE; do 
    chmod 644 "${FILE}"
  done

# Remove empty unsorted directories, if present
if [ -d "unsorted" ]; then
  if [ "$(find unsorted | wc -l)" -eq 1 ]; then
    rmdir unsorted
  fi
fi
