#!/usr/bin/env perl

use strict;
use warnings;

our $VERSION = '0.10';

use Getopt::Long 'HelpMessage';
use MARC::Parser::RAW;
use MARC::Parser::XML;
use MARC::Schema;

GetOptions(
    'file|f=s'   => \my $file,
    'type|t=s'   => \( my $type = 'RAW' ),
    'schema|s=s' => \my $schema_file,
    'help|h'     => sub { HelpMessage() },
) or HelpMessage();

$file = shift unless defined($file);

HelpMessage() unless defined $file and -e $file;

my $schema = MARC::Schema->new({file => $schema_file});

my $parser;
if ( $type eq 'RAW' ) {
    $parser = MARC::Parser::RAW->new($file);
}
elsif ( $type eq 'XML' ) {
    $parser = MARC::Parser::XML->new($file);
}
else {
    print q{type '$type' not supported. Use 'RAW' or 'XML'};
}

my $record_count = 0;
while ( my $record = $parser->next() ) {
    $record_count++;
    my $id = _id($record);
    my @error = $schema->check($record);
    if (@error > 0) {
        foreach my $error (@error) {
            if (exists $error->{value}) {
                print qq{$id\t$error->{tag}\t$error->{error}\t$error->{value}\n};
            } else {
                print qq{$id\t$error->{tag}\t$error->{error}\t\n};
            }
        }
    }
}

sub _id {
    my ($record) = @_;
    my ($id) = map { $_->[-1] } grep { $_->[0] eq '001' } @$record;
    $id = defined $id ? $id : $record_count;
    return $id;
}

__END__

=encoding utf-8

=head1 NAME

marcvalidate - Validate a file with MARC21 records

=head1 SYNOPSIS

  $ marcvalidate [options] FILE

  options:
  --type,-t         Type of MARC21 serialization (RAW|XML, default: RAW)
  --schema,-s       Location MARC JSON schema
  --help,-h         Print this help

=head1 DESCRIPTION

C<marcvalidate> validates a file with MARC21 records against L<MARC::Schema> 
and prints out errors as tab separated list (id, field tag, error, value). 
The ID is extracted from MARC21 field L<001|https://www.loc.gov/marc/bibliographic/bd001.html>.
For a detailed description of the (default) schema see L<"MARC21 structure in JSON"|https://pkiraly.github.io/2018/01/28/marc21-in-json/>.


=head1 AUTHOR

Johann Rolschewski E<lt>jorol@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright 2018- Johann Rolschewski

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 SEE ALSO

L<Catmandu::Validator>

L<JSON::Schema>

L<PICA::Schema>

L<MARC::Lint>

=cut
