#!/bin/bash
# Title:  lowercase
# Author: S.A. (stevenaaus@netscape.net)
# 14/12/02, 28/3/03, 0.9.1->10/6/03, 0.9.5->12/6/03, 0.9.9->9/11/03

#########
# usage #
#########
function Usage () {
echo "usage: $Name [option(s)] file(s)"
echo "options:  -r recurse
          -f force
          -q quiet
          -t test" 
}

##############
# renamefile #
##############

function renamefile () {
# arg(1) is space padding, arg(2) is filename
# mv_lowercase:		mv  "FiLeName 	-> "filename"
# mv_uppercase:		mv  "filename"	-> "FILENAME"
# mv_spaces2underscore:	mv  "file name"	-> "file_name"
# mv_spaces2null:	mv  "file name"	-> "filename"
# mv_capitalise:	mv  "filename"	-> "Filename"

    if [ ! -e "$2" ] ; then
        echo_out ${1}\"$2\" no such file
    else
        case $Name in
            mv_lowercase|lowercase )
	        newname=`echo "$2" | tr '[:upper:]' '[:lower:]'` ;;
            mv_uppercase )
	        newname=`echo "$2" | tr '[:lower:]' '[:upper:]'` ;;
	    mv_spaces2underscore)
	        newname=`echo "$2" | tr '[:blank:]' '_'` ;;
	    mv_spaces2null)
	        newname=`echo "$2" | tr -d '[:blank:]'` ;;
            mv_capitalise )
	        newname=`echo "$2" | cut -c 2- | tr '[:upper:]' '[:lower:]'`
		newname=`echo "$2" | cut -c 1  | tr '[:lower:]' '[:upper:]'`$newname ;;
	    *) echo "$0 : unresolved program link error !" ; exit 1 ;;
        esac

        if [ "$2" != "$newname" ] ; then
 
            ### done in two stages
            ### as "mv FileA filea" fails on vfat
            ### - the TEST feature will wrongly report fails on vfat

            [ $TEST ] || mv "$2" lowercase$$

            if [ ! -e "$newname" ] ; then
                echo_out "${1}$2->$newname"
                [ $TEST ] || mv lowercase$$ "$newname"
            else
                if [ $FORCE ] ; then
                    if [ ! -d "$newname" ] ; then
                        [ $TEST ] || rm "$newname"
                        echo_out "${1}${2}->$newname (overwritten)"
                        [ $TEST ] || mv lowercase$$ "$newname"
                    else
                        echo_out "${1}**fail** $2 is a directory"
                        [ $TEST ] || mv lowercase$$ "$2"
                    fi
                else
                    # not unique or forced, so fail and backtrack
                    echo_out "${1}**fail** $2"
                    [ $TEST ] || mv lowercase$$ "$2"
                fi
            fi
        fi
    fi
}

function echo_out () {
    [ $QUIET ] || echo "$*"
}
    
###########
# recurse #
###########

function recurse () {
### arg(1) is space padding, arg(2) is file/dir
### recurses if directory , then lowersfiles

    if [ -d "$2" ] ; then
        echo_out "$1$2:"
        cd "$2"
        for i in * ; do
            recurse "$1${space}" "$i"
        done
        cd ..
    fi
    renamefile "$1" "$2"
}

########
# main #
########

Name=`basename $0`

### getopt can't handle the '-?' option, so have a special case

if [ "$1" == '-?' ] ; then
	Usage
	exit
fi

shopt -s nullglob   # expand "*" to null string
space="    "        # to change tabulation, change this variable

### process options

FORCE= ; RECURSE= ; QUIET= ; TEST=

while getopts "frqth" option ; do
  case $option in
    f ) FORCE=1 ;;
    r ) RECURSE=1 ;;
    q ) QUIET=1 ;;
    t ) TEST=1 ;;
    h ) Usage ; exit ;;
    \? ) exit 1 ;;
  esac
done

# OPTIND set by getopt
shift $(($OPTIND - 1))

if [ "$1" == "" ] ; then
   echo "$Name: missing args(s)" 
   exit 1
fi

### start main loop

for i in "$@" ; do 

    current_dir=$PWD

    # these two lines are to allow "lowercase SomeDir/FileA"
    # else: "mv SomeDir/FileA somedir/filea" -> "mv:somedir , no such dir"
    # in most cases they do nothing
    # the added complexity probably outweighs any functional gain

    cd "`dirname \"$i\"`"
    file="`basename \"$i\"`"

    if [ $RECURSE ] ; then
        recurse "" "$file"
    else
        renamefile "" "$file"
    fi

    cd $current_dir

done
