adding compact score
[migration-tools.git] / renumber_marc
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use MARC::Batch;
6 use Getopt::Long;
7 #use MARC::Record;
8 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
9 #use MARC::Field;
10
11 $| = 1;
12
13 my $count = 0;
14 my $conf  = {}; # configuration hashref
15 initialize($conf);
16
17 binmode(STDIN, ':utf8');
18
19 open RENUMBER, '>', $conf->{output};
20 binmode(RENUMBER, ':utf8');
21
22 foreach my $input ( @ARGV ) {
23     print STDERR "Processing $input, starting record id at ",
24       $conf->{'renumber-from'},"\n";
25
26     my $batch = MARC::Batch->new('XML', $input);
27     while ( my $record = $batch->next ) {
28         $count++;
29         my @warnings = $batch->warnings;
30         print STDERR "WARNINGS: Record $count : ",
31           join(":",@warnings), " : continuing...\n"
32             if ( @warnings );
33
34         while ($record->field($conf->{tag}))
35           { $record->delete_field( $record->field($conf->{tag}) ) }
36         my $new_id = $conf->{'renumber-from'} + $count - 1;
37         my $new_id_field = MARC::Field->new( $conf->{tag},
38                                              ' ',
39                                              ' ',
40                                              $conf->{subfield} => $new_id );
41         $record->append_fields($new_id_field);
42         print RENUMBER $record->as_xml;
43         print STDERR "\rLast record: $count";
44     }
45     print STDERR "\rProcessed $count records.  Last record id at ",
46       ($conf->{'renumber-from'} + $count - 1), "\n";
47 }
48
49
50 =head2 initialize
51
52 Performs boring script initialization. Handles argument parsing,
53 mostly.
54
55 =cut
56
57 sub initialize {
58     my ($c) = @_;
59     my @missing = ();
60
61     # set mode on existing filehandles
62     binmode(STDIN, ':utf8');
63
64     my $rc = GetOptions( $c,
65                          'incoming',
66                          'incumbent',
67                          'output|o=s',
68                          'renumber-from|rf=i',
69                          'subfield|s=s',
70                          'tag|t=s',
71                          'help|h',
72                        );
73     show_help() unless $rc;
74     show_help() if ($c->{help});
75
76     # set defaults if told to do so
77     if ($c->{incoming}) {
78         $c->{tag} = 903 unless defined $c->{tag};
79         $c->{subfield} = 'a' unless defined $c->{subfield};
80         $c->{output} = 'incoming.renumbered.marc.xml'
81            unless defined $c->{output};
82     } elsif ($c->{incumbent}) {
83         $c->{tag} = 901 unless defined $c->{tag};
84         $c->{subfield} = 'c' unless defined $c->{subfield};
85         $c->{output} = 'incumbent.renumbered.marc.xml'
86           unless defined $c->{output};
87     }
88
89     my @keys = keys %{$c};
90     show_help() unless (@ARGV and @keys);
91     for my $key ('renumber-from', 'tag', 'subfield', 'output')
92       { push @missing, $key unless $c->{$key} }
93     if (@missing) {
94         print "Required option: ", join(', ', @missing), " missing!\n";
95         show_help();
96     }
97
98 }
99
100
101 =head2 show_help
102
103 Display usage message when things go wrong
104
105 =cut
106
107 sub show_help {
108 print <<HELP;
109 Usage is: $0 [REQUIRED ARGS] <filelist>
110 Req'd Arguments
111   --renumber-from=N        -rf First id# of new sequence
112   --tag=N                  -t  Which tag to use
113   --subfield=X             -s  Which subfield to use
114   --output=<file>          -o  Output filename
115 Options
116   --incoming     Set -t, -s, -o to incoming defaults
117   --incumbent    Set -t, -s, -o to incumbent defaults
118
119   Example: '$0 --incoming' is equivalent to
120            '$0 -t 903 -s a -o incoming.renumbered.marc.xml'
121
122 Any number of input files may be specified; one output file will result.
123 HELP
124 exit 1;
125 }