better docstring
[migration-tools.git] / renumber_marc
index 2f7d094..fe2bfa7 100755 (executable)
@@ -5,16 +5,20 @@ use warnings;
 use MARC::Batch;
 use Getopt::Long;
 #use MARC::Record;
-#use MARC::File::XML ( BinaryEncoding => 'utf-8' );
+use MARC::File::XML ( BinaryEncoding => 'utf-8' );
 #use MARC::Field;
 
+$| = 1;
+
 my $count = 0;
 my $conf  = {}; # configuration hashref
 initialize($conf);
 
-binmode(STDOUT, ':utf8');
 binmode(STDIN, ':utf8');
 
+open RENUMBER, '>', $conf->{output};
+binmode(RENUMBER, ':utf8');
+
 foreach my $input ( @ARGV ) {
     print STDERR "Processing $input, starting record id at ",
       $conf->{'renumber-from'},"\n";
@@ -35,9 +39,10 @@ foreach my $input ( @ARGV ) {
                                              ' ',
                                              $conf->{subfield} => $new_id );
         $record->append_fields($new_id_field);
-        print $record->as_xml;
+        print RENUMBER $record->as_xml;
+        print STDERR "\rLast record: $count";
     }
-    print STDERR "Processed $count records.  Last record id at ",
+    print STDERR "\rProcessed $count records.  Last record id at ",
       ($conf->{'renumber-from'} + $count - 1), "\n";
 }
 
@@ -57,6 +62,8 @@ sub initialize {
     binmode(STDIN, ':utf8');
 
     my $rc = GetOptions( $c,
+                         'incoming',
+                         'incumbent',
                          'output|o=s',
                          'renumber-from|rf=i',
                          'subfield|s=s',
@@ -64,6 +71,20 @@ sub initialize {
                          'help|h',
                        );
     show_help() unless $rc;
+    show_help() if ($c->{help});
+
+    # set defaults if told to do so
+    if ($c->{incoming}) {
+        $c->{tag} = 903 unless defined $c->{tag};
+        $c->{subfield} = 'a' unless defined $c->{subfield};
+        $c->{output} = 'incoming.renumbered.marc.xml'
+           unless defined $c->{output};
+    } elsif ($c->{incumbent}) {
+        $c->{tag} = 901 unless defined $c->{tag};
+        $c->{subfield} = 'c' unless defined $c->{subfield};
+        $c->{output} = 'incumbent.renumbered.marc.xml'
+          unless defined $c->{output};
+    }
 
     my @keys = keys %{$c};
     show_help() unless (@ARGV and @keys);
@@ -74,7 +95,6 @@ sub initialize {
         show_help();
     }
 
-    show_help() if ($c->{help});
 }
 
 
@@ -92,6 +112,12 @@ Req'd Arguments
   --tag=N                  -t  Which tag to use
   --subfield=X             -s  Which subfield to use
   --output=<file>          -o  Output filename
+Options
+  --incoming     Set -t, -s, -o to incoming defaults
+  --incumbent    Set -t, -s, -o to incumbent defaults
+
+  Example: '$0 --incoming' is equivalent to
+           '$0 -t 903 -s a -o incoming.renumbered.marc.xml'
 
 Any number of input files may be specified; one output file will result.
 HELP