#!/bin/bash
#	Clean Svn Diff
#	version 0.03
#	Copyright 2009 Scott Grizzard <scott@scottgrizzard.com>
#	http://www.scottgrizzard.com/
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#      
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#       
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
############
#
#	This script takes piped svn diffs, and removes all of the property changes.
#	It is designed to be piped from the svn diff command, or a "cat" of the
#	Output of an svn diff command.
#	
#	Version .03 was a rewrite to take an array of excluded regular expressions
#	and check them for exclusions.
#
#	Binaries are not marked as binaries until after the file is listed,
#	along with a line of equal signs.  If you want to pull these, you need
#	to mark the "Cannot display" and "svn:mime-type" lines first, and
#	then run the filter again /backwards/ to pull the line of equals and
#	the name of the binary file.  

# Declares the array of regular expressions that start the line.
declare -a REGEX_TO_IGNORE

####### SETTINGS ########
#Enable to remove property changes
REGEX_TO_IGNORE=( "${REGEX_TO_IGNORE[@]}" "^Property changes on" )

#Add any other regular expressions you want here.
#REGEX_TO_IGNORE=( "${REGEX_TO_IGNORE[@]}" "dso$" )
#REGEX_TO_IGNORE=( "${REGEX_TO_IGNORE[@]}" "data/gameData/therapyData\.txt$" )
#REGEX_TO_IGNORE=( "${REGEX_TO_IGNORE[@]}" "data/gameData/therapyDataSong[0-9]*\.txt$" )


#TODO: Allow script to strip binaries too.
#REMOVE_BINARIES=1

####### PROGRAM #########

#Sets delimiter to new line, so if you call the script from cat, it
#	will seperate by lines instead of spaces.
OLDIFS=${IFS}
IFS=$'\n'

# Variable to exclude the lines following a properties or regex match.
EXCLUDESERIES=0
# Variable that indicates whether or not the line is the start of a series
ISSTARTOFSERIES=1

#	Originally, the script had one top-level if...else statement in the for loop,
#	and only checked to see if the exclusion should end only if it was already
#	excluded.  This does not work well if you have multiple exclusion conditions,
#	and it makes for a cleaner program to simply check every line to see if the
#	EXCLUDELINE variable should be resest at the beginning.

for LINE in $( cat ${1} ); do
	# Start by checking if we are starting a new file, and if so,
	# cancel the exclusions.
	if ( echo $LINE | grep -q --regexp="^Index" || echo $LINE | grep -q --regexp="^Property" ); then
		EXCLUDESERIES=0
		ISSTARTOFSERIES=1
	fi
	# If we aren't already excluding the line, see if it should be excluded by executing
	# a for loop that checks to see if it matches a rule in the array.  If so, set the
	# exclude variable, and break out of the loop.
	if [ $ISSTARTOFSERIES = 1 ] && [ $EXCLUDESERIES = 0 ]; then
		for RULE in ${REGEX_TO_IGNORE[@]}; do
			if ( echo "$LINE" | grep -q --regexp=${RULE} ); then
				EXCLUDESERIES=1
				continue 2 #brake out of this loop and go to the top loop
			fi
		done
	fi
	
	# If the line is not to be excluded, print it.
	if [ $EXCLUDESERIES = 0 ]; then
		if [ $ISSTARTOFSERIES = 1 ]; then
			printf "\n\n"
		fi
		echo "$LINE"
	fi
	ISSTARTOFSERIES=0
done

# Reset the IFS variable
IFS=${OLDIFS}
