#!/bin/sh

if [ "$1" = "-n" ]; then
  debug=yes
  shift
fi

function do_move() {

  if [ "$debug" = "yes" ]; then
    echo "mv $1 $2"
  else
    if [ -f "$2" ]; then
	if cmp "$1" "$2" ; then
		mv "$1" "$2"
	else
		echo "$2 exists, but is not the same as $1"
	fi
    else
	mv "$1" "$2"
    fi
  fi

}


for dir in "$*"; do 
(
#echo dir: "$dir"
cd "$dir"

ls -1a | while read i; do
  j=""
  case "$i" in
    *[A-Z]*)
      j=$(echo "$i" | \
	awk '{ print tolower($0); }' )

	if [ "$i" != "$j" ]; then
	      do_move "$i" "$j"
	fi

    ;;
    *)
    ;;
  esac
done
)
done

