#! /bin/sh
# Remove trailing blank lines from files
#
# @Authors: Christopher Hylands
#
# @Version: $Id: rmtrailingline,v 1.8 2005/02/28 20:51:31 cxh Exp $
#
# @Copyright (c) 1999-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 sccs 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 sccs delget command
# Then try out fix-files with the -p option, which only prints, but
# does not actually make the change
# Usage: rmtrailinspace [-n] [-d]

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, don't modify files"
			echo " -d  debug"
			exit 3;;
	esac
done
shift `expr $OPTIND - 1`

TMPFILE=/tmp/rmtrailingspace.tmp.$$

doit() {
	awk '{
		if ($0 ~ /^[ 	]*$/) {
		    blankCount++
		} else {
		    for(;blankCount > 0; blankCount--) {
			printf("\n");
		    }
		    print $0
		}
	}' < $file > $TMPFILE
	diff $file $TMPFILE
}

#EGREPMATCH="[ 	]+$"
LOGMESSAGE="Removed trailing lines"

topdir=`pwd`
for fullfile in $@
do
    cd $topdir
    echo "Now processing: $fullfile"
    #egrep "$EGREPMATCH" $fullfile
    tail -1 $fullfile | egrep -s '^[ 	]*$'
    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
