#!/bin/sh

# will convert specified files to lower names

if [ "$1" = "-r" ] ; then
	recurse=1; shift;
else
	recurse=0;
fi;

for x in $* ; do
	dir=`echo "$x" | sed 's@\(.*\)/.*@\1@'`;
	file=`echo "$x" | sed 's@\(.*\)/@@'`;
	pushd "$dir" > /dev/null;
	if [ -r "$file" ] ; then
		conv="`echo $file | tr '[A-Z]' '[a-z]'`";
		if [ "$file" != "$conv" ] ; then
			if [ -e "$conv" -a ! "$file" -ef "$conv" ] ; then
				echo "ERROR: $conv already exists (from $x)";
				echo "exiting..."
				exit 1;
			fi;
			#wish i could do this:
			# mv "$x" "$conv";
			#but cygwin doesn't like that...
			mv "$file" "$file"_tmp;
			mv "$file"_tmp "$conv";
		fi;
		if [ $recurse -gt 0 -a -d "$conv" ] ; then
			"$0" -r "$conv"/*;
		fi;
	fi;
	popd > /dev/null;
done;
