#!/bin/sh
# http://www.berklix.com/~jhs/bin/.sh/nobracket.old
# Bourne shell script by Julian H. Stacey
# See Also ~/bin/.sh/noms

# How to call this:	
#	find -d ./*    -name \*\(\*                   -exec nobracket {} \;
#	find -d ./* \( -name \*\(\* -o -name \*\)\* ) -exec nobracket {} \;
#		-d      = depth-first

# Move horrid (Microsoft ubiquitous graphical tool generated/ manipulated)
# file or directory names with bracket '(' ')' in, to (command line 
# interpreter & # Unix friendly) names with underscores.
# Note it does not remove [] {}

# Debugging Example:
#	mkdir "aa(aa)aa" ; date -u +%Y-%m-%dT%H:%M:%SZ > "aa(aa)aa/bb(bb)bb"
#	ls -la . *
#		aa(aa)aa/
#		bb(bb)bb
#	find    ./* | sort
#		./aa(aa)aa
#		./aa(aa)aa/bb(bb)bb
#	find -d -s ./*
#		./aa(aa)aa/bb(bb)bb
#		./aa(aa)aa
#	find -d -s ./* -exec nobracket {} \;
#		# -d does sub {directories & files} before parent directories.
#		# find .
#		#	.
#		#	./aa_aa_aa
#		#	./aa_aa_aa/bb_bb_bb
#	find -d -s ./*
#		./aa(aa)aa/bb(bb)bb
#		./aa(aa)aa

# Warnings:
#	- Only takes 1 argument.
#	- Will likely fail on names with a ` or ' etc.
#       - JJLATER Does notyet check if this will over-write a pre existing file.
#       - Will not cope with a file that has bracket at beginning or end
#	  of name, only those with one or more brackets in the middle.
# man ascii: 050 ( 051 ) 133 [ 134 \ 135 ]
if [ "$1" == "`dirname \"$1\"`/`basename \"$1\" | tr \"\\050\" _ | tr \"\\051\" _`" ] ; then
	# echo "Same, doing nothing for $1"
else
	mv "$1" "`dirname \"$1\"`/`basename \"$1\" | tr \"\\050\" _ | tr \"\\051\" _`"
fi
