#!/bin/sh -
# rename files to lower/upper case...
#
# usage:
#       move-to-lower *
#       move-to-upper *
# or
#       move-to-lower -r .
#       move-to-upper -r .
#

help()
{
        cat << eof
Usage: $0 [-n] [-r] [-h] files...

-n      do nothing, only see what would be done
-r      recursive (use find)
-h      this message
files   files to remap to lower case

Examples

       $0 -n *              (see if everything is ok, then...)
       $0 *

       $0 -r .

eof
}


apply_cmd='sh'
finder='echo $* | tr " " "\n"'
files_only=

while :
do
        case "$1" in
                -n) apply_cmd='cat' ;;
                -r) finder='find $* -type f';;
                -h) help ; exit 1 ;;
                *) break ;;
        esac
        shift
done


[ "$1" ] || {

        echo Usage: $0 [-n] [-r] files...
        exit 1
}

LOWER='abcdefghijklmnopqrstuvwxyz'
UPPER='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

case `basename $0` in
        *to-lower*)
                FROM=$UPPER; TO=$LOWER ;;
        *to-upper*)
                TO=$UPPER; FROM=$LOWER ;;
        *lower*)
                FROM=$UPPER; TO=$LOWER ;;
        *upper*)
                TO=$UPPER; FROM=$LOWER ;;
        *)
                FROM=$UPPER; TO=$LOWER ;;
esac

eval $finder | sed -n '


# remove all trailing /s
s/\/*$//

# add ./ if there are no path, only filename
/\//!s/^/.\//

# save path+filename
h

# remove path
s/.*\///

# do conversion only on filename
y/'$FROM'/'$TO'/


# swap, now line contains original path+file, hold space contains conv filename
x

# add converted file name to line, which now contains something like
# path/file-name\nconverted-file-name
G

# check if converted file name is equal to original file name, if it is, do
# not print nothing
/^.*\/\(.*\)\n\1/b

# now, transform path/fromfile\ntofile, into mv path/fromfile path/tofile
# and print it
s/^\(.*\/\)\(.*\)\n\(.*\)$/mv \1\2 \1\3/p


' | $apply_cmd


