ceap80
8/30/2010 - 8:58 AM

Replaces all occurences of .bind(bind, args) with .pass(args, bind) in the target files or directories.

Replaces all occurences of .bind(bind, args) with .pass(args, bind) in the target files or directories.

#!/bin/sh

usage(){
	echo 'Replaces all occurences of .bind(bind, args) with'
	echo '.pass(args, bind) in the target files or directories.'
	echo
	echo "Usage: $0 [options] [paths...]"
	echo
	echo 'options:'
	echo '  -h'
	echo '  --help    Show this help.'
	echo '  -n'
	echo "  --dry-run Don't do anything, only show the files and lines"
	echo '            that would be affected.'
	echo
	echo 'arguments:'
	echo '  [paths]   Files and/or directories to search and replace.'
	echo '            Defaults to the current working directory.'
}

while true; do
	case "$1" in
		-h|--help) usage; exit;;
		-n|--dry-run) DRYRUN=1; shift;;
		-*) echo "Unknown option '$1', ignoring." >&2; shift;;
		--) shift; break;;
		*) break;;
	esac
done

ARGS=$@

if [ -z "$ARGS" ]; then
	ARGS='./'
fi

FILES=`grep -lr 'bind([^,)]\+,' $ARGS`

if [ -z "$FILES" ]; then
	echo 'No occurences of .bind(bind, args) found.'
	exit 1
fi

if [ -n "$DRYRUN" ]; then
	for F in $FILES; do
		echo "$F:"
		grep -nr 'bind([^,)]\+,' "$F"
	done
	exit
fi

echo "Replacing in files:"
for F in $FILES; do
	echo "- $F"
	sed -i'.orig' 's/\.bind(\([^,]*\), *\([^)]*\))/\.pass(\2, \1)/g' "$F"
done