#!/bin/sh

#
# Filename: rename.sh
# Description: Renames files and folders to lowercase recursively
#              from the current directory
# Variables: Source = x
#            Destination = y

#
# Rename all directories. This will need to be done first.
#

# Process each directory.s contents before the directory  itself

# Convert filenames to lowercase
# and replace characters recursively
#####################################

if [ -z $1 ];then echo Give target directory; exit 0;fi

find "$1" -depth -name '*' | while read file ; do
    directory=$(dirname "$file")
    oldfilename=$(basename "$file")
    newfilename=$(echo "$oldfilename" | tr ' ' '_' | sed 's/_-_/-/g')
#    newfilename=$(echo "$oldfilename" | tr 'A-Z' 'a-z' | tr ' ' '_' | sed 's/_-_/-/g')
    if [ "$oldfilename" != "$newfilename" ]; then
    mv -i "$directory/$oldfilename" "$directory/$newfilename"
    echo ""$directory/$oldfilename" ---> "$directory/$newfilename""
    fi
    done
exit 0


#ls | while read -r FILE
#do
#    mv -v "$FILE" `echo $FILE | tr ' ' '_' `
#done
#
# Rename all files
#
#for x in `find * -type f`;
#do
#  # Translate Caps to Small letters
#  y=$(echo $x | tr '[A-Z]' '[a-z]');
#  if [ "$x" != "$y" ]; then
#    mv $x $y;
#  fi
#done

#exit 0
