improvements to bib nuking script
[migration-tools.git] / marc_cleanup
index 04b9760..e0c20a7 100755 (executable)
@@ -1,4 +1,21 @@
 #!/usr/bin/perl
+
+# Copyright 2009-2012, Equinox Software, Inc.
+#
+# 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, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
 require 5.10.0;
 
 use strict;
@@ -76,7 +93,7 @@ while ( buildrecord() ) {
     until ($ptr == $#record) {
         # get datafield/tag data if we have it
         $rc = stow_record_data() if ($c->{'renumber-from'} and $c->{'original-tag'});
-        return $rc if $rc;
+        next if $rc;
 
         # naked ampersands
         if ($record[$ptr] =~ /&/ && $record[$ptr] !~ /&\w+?;/)
@@ -176,6 +193,12 @@ sub do_automated_cleanups {
             message("Dollar sign corrected");
         }
 
+        # excessive trailing whitespace in subfield contents
+        if ($record[$ptr] =~ m|\s{10,}</subfield>|) {
+            $record[$ptr] =~ s|\s{10,}</subfield>|</subfield>|;
+            message("Trailing whitespace trimmed from subfield contents");
+        }
+
         # automatable subfield maladies
         $record[$ptr] =~ s/code=" ">c/code="c">/;
         $record[$ptr] =~ s/code=" ">\$/code="c">\$/;
@@ -186,7 +209,7 @@ sub do_automated_cleanups {
 sub stow_record_data {
     # get tag data if we're looking at it
     my $tag = 0;
-    if ($record[$ptr] =~ m/<(control|data)field tag="(.{3})"/) {
+    if ($record[$ptr] =~ m/<(?:control|data)field tag="(.{3})"/) {
         $recmeta{tag} = $1;
         $tag = $recmeta{tag};
         $record[$ptr] =~ m/ind1="(.)"/;
@@ -299,7 +322,7 @@ sub buildrecord {
     my $istrash = 0;
     my $trash = $c->{trash};
 
-    $l = <MARC> while (defined $l and $l !~ /<record>/);
+    $l = <MARC> while (defined $l and $l !~ /<record/);
     return $l unless defined $l;
     $c->{ricount}++;
 
@@ -603,7 +626,7 @@ sub initialize {
     show_trashhelp() if ($c->{trashhelp});
 
     # defaults
-    my $pfx = $c->{prefix} // "bibs";
+    my $pfx = defined($c->{prefix}) ? $c->{prefix} : "bibs";
     $c->{ricount} = 0;
     $c->{rocount} = 0;
     $c->{'renumber-tag'} = 903 unless defined $c->{'renumber-tag'};