#!/usr/bin/perl

# run:
# find /path/to/libo -name *.cxx | xargs perl replaceDelete.pl

# process multi file targets
foreach (@ARGV) {
	my $file = $_;
	my $data = "";

# grep file to $data
open(fh, "<$file");
while (<fh>) { $data .= $_; }
close(fh);

# replaces unnecessary if checks for delete, example:
#
# -    if (pTextPContext)
# -        delete pTextPContext;
# +    delete pTextPContext;


# check for: if ( testString ) delete testString;
while ($data =~ /if \(\s*([A-Za-z]+)\s*\)\s*\n\s*delete\s+[A-Za-z]+\s*\;/gs) {
	print "found: $1\n";

	# use intendation of if statement
	$data =~ s/(\n\s*)if \(\s*$1\s*\)\s*\n\s+(delete\s+$1\s*\;)/$1$2/gs;
}

# write file
open(fh, ">$file");
print fh $data;
close(fh);

}
