move copy location mapping functionality into base.sql
[migration-tools.git] / extract_loadset
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use open ':utf8';
5
6 use Getopt::Long;
7
8 my $conf  = {}; # configuration hashref
9 initialize($conf);
10
11 # build exclusion hash
12 open FP, '<', shift or die "Can't open matchset file: $!\n";
13 my %exclude = ();
14 while (<FP>) {
15     my ($lead,$sub) = split /\t/;
16     $exclude{$sub}  = 1 unless ($sub < $conf->{lowerbound});
17 }
18 close FP;
19
20 # strip exclusions from marcxml file
21 open MI, '<', $conf->{marc} or die "Can't open input file: $!\n";
22 open MO, '>', $conf->{output} or die "Can't open output file: $!\n";
23 while (<MI>) {
24     m/tag="$conf->{tag}",+?<subfield code="$conf->{subfield}">(\d+)</;
25     print MO unless $exclude{$1};
26 }
27
28
29 sub initialize {
30     my ($c) = @_;
31     my @missing = ();
32
33     # set mode on existing filehandles
34     binmode(STDIN, ':utf8');
35
36     my $rc = GetOptions( $c,
37                          'lowerbound|l=i',
38                          'input|i=s',
39                          'output|o=s',
40                          'tag|t=i',
41                          'subfield|s=s',
42                          'help|h',
43                        );
44     show_help() unless $rc;
45     show_help() if ($c->{help});
46
47     $conf->{tag} = $conf->{tag} || 903;
48     $conf->{subfield} = $conf->{subfield} || 'a';
49
50     my @keys = keys %{$c};
51     show_help() unless (@ARGV and @keys);
52     for my $key ('output', 'lowerbound', 'input')
53       { push @missing, $key unless $c->{$key} }
54     if (@missing) {
55         print "Required option: ", join(', ', @missing), " missing!\n";
56         show_help();
57     }
58 }
59
60 sub show_help {
61     print <<HELP;
62 Usage is: extract_loadset -l BOUND -i INPUTXML -o OUTPUTXML MATCHSET
63
64   --lowerbound  -l  Lowest record ID which will be included in the loadset
65   --input       -i  MARCXML input file
66   --output      -o  MARCXML output file
67   --tag         -t  MARC tag to use as identifier (default: 903)
68   --subfield    -s  Subfield of --tag argument (default: 'a')
69 HELP
70 exit;
71 }