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