#!/bin/sh
# http://www.berklix.com/~jhs/bin/.sh/nospace
echo "$0 is Obsolete, replaced by ~/public_html/src/bsd/jhs/bin/public/mvexp/"
# http://www.berklix.com/~jhs/bin/.sh/nospace
# Bourne shell script by Julian H. Stacey
# See Also:	lower nobracket nocolon noquote nospace upper
# 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

# How to call this:	find -d ./* -exec nospace {} \;

# Move horrid (Microsoft ubiquitous graphical tool generated/ manipulated)
# file or directory names with spaces in, to (command line interpreter and
# Unix friendly) names with underscores.

# Debugging Example:
#	mkdir "aa aa aa" ; date -u +%Y-%m-%dT%H:%M:%SZ > "aa aa aa/bb bb bb"
#	find    ./* | sort
#		# ./aa aa aa
#		# ./aa aa aa/bb bb bb
#	find -d ./*               -exec           nospace {} \;
#		# -d does sub {directories and files} before parent directories.
#		# find .
#		#	.
#		#	./aa_aa_aa
#		#	./aa_aa_aa/bb_bb_bb
#		# find above would be slower (as does more) than find below.
#	rm -rf aa_aa_aa
#	mkdir "aa aa aa" ; date -u +%Y-%m-%dT%H:%M:%SZ > "aa aa aa/bb bb bb"
#	find -d ./* -name \*' '\* -exec ~/bin/.sh/nospace {} \; -print
#		# ./aa aa aa/bb bb bb
#		# ./aa aa aa
#	find    ./* | sort
#		# ./aa_aa_aa
#		# ./aa_aa_aa/bb_bb_bb

# Warnings:
#	- Only takes 1 argument.
#	- Will likely fail on names with a ` or ' etc.
#       - Does not check if this will over-write a pre existing file.
#       - Will not cope with a file that has space at beginning or end
#	  of name, only those with one or more spaces in the middle.

	if [ "$1" == "`dirname \"$1\"`/`basename \"$1\" | \
		tr \"\\040\" _`" ] ; then
		# echo "Same, doing nothing for $1"
	else
		# echo Moving "$1" "`dirname \"$1\"`/`basename \"$1\" | tr \"\\040\" _`"
		mv "$1" "`dirname \"$1\"`/`basename \"$1\" | tr \"\\040\" _`"
	fi
exit

# -----------------------------------------------------------------------------
# This was too ambitious as the for loop broke into more parameters:
#	for i in $*
#	do
#		if [ "$i" = "`echo $i | tr "\040" _`" ]; then
#			echo "Already no spaces in name $i, no need to convert."
#		else
#			mv "$i" "`echo $i | tr "\040" _`"
#		fi
#	shift
#	done
#
#	touch     nospace 'has space'
#	nospace nospace 'has space'
#		Already no spaces in name nospace, no need to convert.
#		Already no spaces in name has, no need to convert.
#		Already no spaces in name space, no need to convert.

# if [ "$1" = "`echo $1 | tr "\040" _`" ]; then
#	# echo "No spaces in $1, no need to convert."
# else
#	# I gave up trying to use [:space:]
#	echo "Moving $1 to `echo $1 | tr "\040" _`"
# fi
