#! /bin/sh
# Remove trailing space from files
#
# @Authors: Christopher Hylands
#
# @Version: $Id: rmtrailingspace,v 1.15 2005/02/28 20:50:48 cxh Exp $
#
# @Copyright (c) 1997-2005 The Regents of the University of California.
# All rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in all
# copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
# THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
# PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
# CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS.
#
# 						PT_COPYRIGHT_VERSION_2
# 						COPYRIGHTENDKEY


# This script can be used to fix many cvs managed files at once.
# Before running this script, you must:
# 1. Create a doit function that does the editing you want
# 2. Set up the files variable so it contains a list of the files
# 3. Set up a grep statement that will identify files you want to change
# 4. Adjust the log message for the cvs commit  command
# Then try out fix-files with the -n option, which only prints, but
# does not actually make the change.
#
# Usage: rmtrailinspace [-n] [-d]

printonly=no

# We don't use getopt here because it is not included in the basic
# cygwin bash download (12/01)

if [ "$1" = "-h" ]; then
    echo "$0: Usage: $0 [-d] [-n] [filenames . . .]"
    echo " -d  debug"
    echo " -n  Print only, don't modify files"
    exit 3
fi

if [ "$1" = "-n" ]; then
    printonly=yes
    shift
fi
if [ "$1" = "-d" ]; then
    set -x
    shift
fi

# Handle -d -n
if [ "$1" = "-n" ]; then
    printonly=yes
    shift
fi

TMPFILE=/tmp/rmtrailingspace.tmp.$$

# doit is a shell function
doit() {
	sed -e 's/[ 	]*$//g'  < $file > $TMPFILE
	diff $file $TMPFILE
}

EGREPMATCH="[ 	]+$"
LOGMESSAGE="Removed trailing spaces"

topdir=`pwd`
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 $TMPFILE $file
		if [ -d CVS ]; then
		    cvs commit -m "$LOGMESSAGE" $file
		fi
	    else
		echo "Would update and commit"
	    fi

    fi
done
rm -f $TMPFILE
