#!/usr/bin/perl

use warnings;
use strict;
use autodie;
use utf8;

use JSON;
use Data::Dump qw(dump);
use MARC::Record;

open(my $json, '<', 'report_62836_1046_ZavrsniRadovi.json');
{
	undef $/;
	my $data = <$json>;
	close($json);
	$json = decode_json($data);
}
#warn dump($json->[0]);

my $stat;

foreach my $rad ( @$json ) {

	print dump($rad), "\n";

	if ( ! $rad->{naslovhr} ) {
		$stat->{no_naslovhr}++;
		next;
	}
	$stat->{radova}++;

	my $marc = MARC::Record->new;
	$marc->encoding( 'UTF-8' );

	$marc->add_fields(
		grep { ref($_) eq 'ARRAY' }
		[ '100', '1', '#', a => $rad->{prezime} . ', ' . $rad->{ime}, g => 'JMBAG:' . $rad->{jmbag} ],
		[ '245', '1', '0', a => $rad->{naslovhr} ],
		[ '246', '1',
			$rad->{nasloven} =~ m/The / ? 4 :
			$rad->{nasloven} =~ m/A /   ? 2 : ' ' # FIXME?
			, a => $rad->{nasloven}, i => 'Naslov na engleskom: '
		],
		$rad->{'brojstranica'} ? [ '300', '#', '#', a => $rad->{'brojstranica'} . ' str. ;', c => '30 cm' ] : '',
		[ '502', '#', '#', b => $rad->{studij} . ' studij', c => 'Fakultet elektrotehnike i računarstva u Zagrebu', g => 'smjer: ' . $rad->{smjer}, g => 'šifra smjera: ' . $rad->{sifsmjer}, d => $rad->{datumzavrsetka} ],
		$rad->{sazetakhr} ? [ '520', '3', '#', a => 'Sažetak na hrvatskom: ' . $rad->{sazetakhr} ] : '',
		$rad->{sazetaken} ? [ '520', '3', '#', a => 'Sažetak na engleskom: ' . $rad->{sazetaken} ] : '',
		$rad->{keywords}  ? [ '653', '#', '#', map { ( a => $_ ) } split(/;\s*/, $rad->{keywords}  ) ] : '',
		$rad->{keywords_en} ? [ '653', '#', '#', map { ( a => $_ ) } split(/;\s*/, $rad->{keywords_en}) ] : '',
		[ '700', '1', '#', a => $rad->{prezmentor} . ', ' . $rad->{imementor}, 4 => 'ths' ],
		$rad->{prezmentor2} ? [ '700', '1', '#', a => $rad->{prezmentor2} . ', ' . $rad->{imementor2}, 4 => 'ths' ] : ''
	);

	print $marc->as_formatted, "\n\n"; # FIXME

#	last if $stat->{radova} == 10; # FIXME
}

print "# stat = ",dump($stat);

=for import

($recordid, $biblioitemnumber) = AddBiblio($marc_record, $framework);

=cut
