#!/bin/sh
# renlctree.sh v0.1
# Copyright 2008 by Jeff D. Hanson
# Released under GNU General Public License v3
# Recursively rename all files in a tree to lower case
if test ! -n "$1"
then
    echo "This is a simple script for renaming all files and directories in a"
    echo "directory tree to lower case.  Standard disclaimer applies - no"
    echo "warranty and back up existing directories first."
    echo "Usage:  renlctree.sh path"
    exit 1
fi

# Find and rename files with caps in names
echo "Renaming files..."
find $1 -type f -execdir rename 'y/A-Z/a-z/' {} \;

# Check for directories with caps in names
echo "Exhaustively renaming directories..."
capped_dirs=$(find $1 -type d -regex '.*[A-Z].*')
while
test -n "$capped_dirs"
do
# Find and rename directories with caps in names
# Matching child directories will be skipped if matching parent is renamed
find $1 -type d -regex '.*[A-Z].*' -exec rename 'y/A-Z/a-z/' {} \;
# Check for target directories skipped because parent was renamed first
capped_dirs=$(find $1 -type d -regex '.*[A-Z].*')
done
exit 0
