#!/bin/sh

############################################################
# this script will change file name recursively with option
# -u: locase to upcase
# -l: upcase to locase
############################################################

hint () {
echo " Usage: $0 [-l|-u] DIR1 [DIR2 DIR3...]
-l to lowcase
-u to upcase"
exit 1
}

if test $# -lt 2; then
   echo "Too few arguments."
   hint
fi

while [ "$1" ]; do
   case $1 in
   -l) ACTION="lo"
      shift 1
      ;;
   -u) ACTION="up"
      shift 1
      ;;
   *) if test -d $1; then
        DIR="$DIR $1"
      else
        echo "no such directory --- $1"
        hint
      fi
      shift
      ;;
   esac
done

# echo $ACTION
# echo $DIR

#FOUND=`find ${DIR}/ | sort -r`
FOUND=`find ${DIR}/ -maxdepth 1 -mindepth 1 | sort -r`


for i in $FOUND; do
   DN=`dirname $i`
   BS=`basename $i`
   loBS=`echo $BS | tr [A-Z] [a-z]`
   upBS=`echo $BS | tr [a-z] [A-Z]`

   NAME1=$DN/$BS

   if [ "$ACTION" = "lo" ]; then
      NAME2=$DN/$loBS
   elif [ "$ACTION" = "up" ]; then
      NAME2=$DN/$upBS
   fi

   if [ "$NAME1" = "$NAME2" ]; then
      echo "****: $NAME1 ---x--- $NAME2 identical!"
   else
      echo "- renaming $NAME1 --> $NAME2"
      mv $NAME1 $NAME2
   fi
done
