#!/bin/sh
# http://www.berklix.com/~jhs/bin/.sh/noupper
echo "$0 may be Obsolete, consider ~/public_html/src/bsd/jhs/bin/public/mvexp/"
# bourne shell script by Julian H. Stacey
# See Also (and run after or before some of these in):    ~/bin/.sh/
#       nobracket nocolon nolower noquote nospace noupper
#	Run nospace Before noupper Or nolower Or nocolon
#	JJLATER Decide Priority Of nobracket And noquote And nospace
# Suggestion: first run noquote then run noupper

# Moves all files to lower case filenames (useful after using Mtools mread)
# Syntax	noupper filename[s]

# Warning:
#	Only converts last bit of path to lower case eg
#		DIRECTORY/FILE to DIRECTORY/file
#	So if you want to convert a whole tree use:
#		find . -depth -name \*\[A-Z\]\* -exec  ~/bin/.sh/noupper {} \;

for i in $*
do
	if [ "$i" != "`basename $i | tr "[:upper:]" "[:lower:]"`" ]; then
		# echo "Already lower case, no need to convert."
	# else ;
		# For trees called from find,
		# Need to progressively operate just on basename
		#	dirname  ./A.B.C/D.E.F/G.H.I	# ./A.B.C/D.E.F
		# 	basename ./A.B.C/D.E.F/G.H.I	# G.H.I
		#	dirname  ./A.B.C/D.E.F      	# ./A.B.C
		# 	basename ./A.B.C/D.E.F      	# D.E.F
		#	dirname  ./A.B.C		# .
		#	dirname    A.B.C		# .
		#	dirname    A			# .
		#	basename ./A.B.C		# A.B.C
		old=`basename $i`
		new=`echo $old | tr "[:upper:]" "[:lower:]"`
		dir=`dirname $i`
		mv $dir/$old    $dir/${new}X
		mv $dir/${new}X $dir/${new}
		# Notes on 'X'
		#	Above without the X used to fail (on 9.3-RELEASE) on
		#	file systems mounted as msdosfs
		#	(which maps one case to another)
		#	(as 'mkdir DUMMY;mv DUMMY dummy' also fails with:
		#	mv: rename DUMMY to dummy/DUMMY: Invalid argument
		#	See man mount_msdosfs
		#       X is at right end of path name not left,
		#	because although X at either end works for files just
		#	in current directory, it breaks if find provides
		#	deep paths eg:
		#		 mv: X./tech/bafug/amd64/bafug2-Peter-3.mov: \
		#		 No such file or directory
		#	I use X not .X in case a DOS FS may not like a
		#	double dot as in prog_ram.c.X
	fi
shift
done
