#!/bin/sh
# Fix common problems in java files
#  $Id: fix-files 50060 2008-07-09 19:07:59Z cxh $
#
printonly=no

while getopts nd-- opt
do
    case $opt in
	d) 	set -x;;
	n) 	printonly=yes;;
	\?)	echo "$0: Usage: $0 [-p] [-x] [filenames . . .]"
		echo " -n  Print only, do not modify any files "
		echo " -d  debug"
		exit 3;;
    esac
done
shift `expr $OPTIND - 1`


search() {
topdir=`pwd`
echo "************ $EGREPMATCH: $topdir"

for fullfile in $@
do
    cd $topdir
    echo "Now processing: $fullfile"
    egrep "$EGREPMATCH" $fullfile
    retval=$?
    if [ $retval = 0 ]; then
	# There was a difference, so we might want to check this sucker out
	file=`basename $fullfile`
	dirname=`dirname $fullfile`
	cd $dirname
	if [ -d SCCS -o -d RCS ]; then
	    echo "Error: SCCS or RCS directory"
	    exit 2
	fi
	doit
	if [ "$printonly" = "no" ]; then
	    cp /tmp/fix-files.tmp $file
	    if [ -d .svn ]; then
		#echo "NOT doing a svn commit, did a copy"
		svn commit -m "$LOGMESSAGE" $file 
	    fi	
	else
	    echo "Would update and commit"
	fi	
    fi
done
rm -f /tmp/fix-files.tmp
cd $topdir
}

doit() {
    sed -e 's/for(/for (/' \
	-e 's/){$/) {/' \
	-e 's/if( /if (/' \
	-e 's/if(/if (/' \
	-e 's/\}catch/\} catch/g' \
	-e 's/catch(/catch (/g' \
	-e 's/do{/do {/' \
	-e 's/}while/} while/' \
	-e 's/while(/while (/' \
	-e 's/try{/try {/' \
	-e 's/else{/else {/' \
	-e 's/)throws{/) throws/' \
	    $file > /tmp/fix-files.tmp
	diff -c $file /tmp/fix-files.tmp
}
EGREPMATCH='for\(|if\(|}catch|do{|}while|while\(|\){$|try{|else{|\)throws'
LOGMESSAGE='Added spaces in for, while, if, else, try, catch, throws'

search $@

doit() {
    sed -e 's#@throws#@exception#g' \
	    $file > /tmp/fix-files.tmp
	diff -c $file /tmp/fix-files.tmp
}
EGREPMATCH='@throws'
LOGMESSAGE='@throws -> @exception'

search $@
