fingerprinter: removed --incoming and --incumbent. now only one set of defaults....
[migration-tools.git] / marc-cleanup
index 641c4bf..e298bda 100755 (executable)
@@ -17,10 +17,10 @@ initialize($conf);
 populate_trash() if ($conf->{trashfile});
 
 # set up files, since everything appears to be in order
+my $EXMARC = '';
 open MARC, '<:utf8', (shift || 'incoming.marc.xml');
 open my $NUMARC, '>:utf8', $conf->{output};
-open my $EXMARC, '>:utf8', $conf->{exception};
-open my $OLD2NEW, '>', 'old2new.incoming.map'
+open my $OLD2NEW, '>', 'old2new.map'
   if ($conf->{'renumber-from'} and $conf->{'original-subfield'});
 print $NUMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
 print $NUMARC '<collection xmlns="http://www.loc.gov/MARC21/slim">',"\n";
@@ -61,7 +61,6 @@ while ( buildrecord() ) {
     }
 
     do_automated_cleanups();
-    stow_record_data();
 
     $ptr = 0;
     until ($ptr == $#record) {
@@ -109,6 +108,9 @@ print $OUT "\nDone.               \n";
 sub do_automated_cleanups {
     $ptr = 0;
     until ($ptr == $#record) {
+        # get datafield/tag data if we have it
+        stow_record_data();
+
         # catch empty datafield elements
         if ($record[$ptr] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
             if ($record[$ptr + 1] =~ m|</datafield>|) {
@@ -269,6 +271,13 @@ sub write_record {
     my ($FH) = @_;
     my $trash = $conf->{trash};
 
+    # uninitialized $EXMARC
+    if ($FH eq '') {
+        $EXMARC = undef;
+        open $EXMARC, '>:utf8', $conf->{exception};
+        $FH = $EXMARC;
+    }
+
     $conf->{rocount}++ if ($FH eq $NUMARC);
     print $FH '<!-- ', $recmeta{explanation}, " -->\n"
       if(defined $recmeta{explanation});
@@ -313,8 +322,14 @@ sub write_record {
       { s/\n// for (@record) }
 
     # write to old->new map file if needed
-    print $OLD2NEW $recmeta{oid}, "\t", $recmeta{nid}, "\n"
-      if ($conf->{'renumber-from'} and $conf->{'original-subfield'});
+    if ($conf->{'renumber-from'} and $conf->{'original-subfield'}) {
+        unless (defined $recmeta{oid}) {
+            my $msg = join(' ', "No old id num found");
+            dump_record($msg);
+        } else {
+            print $OLD2NEW $recmeta{oid}, "\t", $recmeta{nid}, "\n"
+        }
+    }
 
     # and finally, actually write the record
     print $FH @record,"\n";
@@ -325,6 +340,7 @@ sub print_fullcontext {
     print $OUT $conf->{editmsg},"\n";
     print $OUT "\r    Tag:",$recmeta{tag}, " Ind1:'",
       $recmeta{ind1},"' Ind2:'", $recmeta{ind2}, "'";
+    print $OUT " @ ", $conf->{ricount}, "/", $conf->{rocount} + 1,"\n";
     print_context();
     return 0;
 }
@@ -352,11 +368,11 @@ sub message {
 #-----------------------------------------------------------------------------------
 
 sub substitute {
-    my ($line_in, @chunks) = @_;
+    my (@chunks) = @_;
 
     my $ofrom = shift @chunks;
-    if ($ofrom =~ /^'/ or !@chunks) {
-        until ($ofrom =~ /'$/)
+    if ($ofrom =~ /^'/) {
+        until ($ofrom =~ /'$/ or !@chunks)
           { $ofrom .= join(' ','',shift @chunks) }
         $ofrom =~ s/^'//; $ofrom =~ s/'$//;
     }
@@ -428,6 +444,12 @@ sub yank_line {
     return 0;
 }
 
+sub insert_original {
+    $record[$ptr] = $recmeta{origline};
+    print_context();
+    return 0;
+}
+
 sub display_lines {
     print $OUT "\nOrig. edit line  :", $recmeta{origline};
     print $OUT "Current flip line:", $recmeta{prevline} if $recmeta{prevline};
@@ -437,7 +459,7 @@ sub display_lines {
 }
 
 sub dump_record {
-    my ($line_in, @explanation) = @_;
+    my (@explanation) = @_;
     $recmeta{explanation} = join(' ', 'Tag', $recmeta{tag}, @explanation);
     write_record($EXMARC);
     return 1;
@@ -616,9 +638,9 @@ sub initialize {
                          'nocollapse|n',
                          'renumber-from|rf=i',
                          'renumber-tag|rt=i',
-                         'renumber-subfield|rs=i',
+                         'renumber-subfield|rs=s',
                          'original-tag|ot=i',
-                         'original-subfield|os=i',
+                         'original-subfield|os=s',
                          'script',
                          'strip-nines',
                          'trashfile|t=s',
@@ -663,7 +685,7 @@ Options
                            it appears in the trash file
   --original-subfield -os  Original id subfield code. If this is specified
                            and renumbering is in effect, an old-to-new mapping
-                           file (old2new.incoming.map) will be generated.
+                           file (old2new.map) will be generated.
 
   --nocollapse -n  Don't compress records to one line on output
   --autoscrub  -a  Automatically remove non-numeric tags in data