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