#!/usr/bin/perl

use warnings;
use strict;
use autodie;

use Text::CSV qw( csv );
use Data::Dump qw( dump );
use JSON;
use Clone qw( clone );

my $file = 'keramika.csv';
my $aoh = csv (in => $file, headers => "auto");

open(my $fh, '>', 'keramika.js');
my $json;

foreach my $i ( @$aoh ) {
	my $source = sprintf( "ZBIRKA-NEOLITIK/%02d.png", $i->{ID} );
	my $image = sprintf( "image/%d.jpg" , $i->{ID} );
	my $thumb = sprintf( "thumb/%d.jpg" , $i->{ID} );
	system "convert -trim $source $image";
	system "convert -geometry 100x $image $thumb";
	$i->{image} = $image;
	$i->{thumbnail} = $thumb;
#	$i->{label} = $i->{opis_predmeta};
	$i->{label} = $i->{ID};
	warn "# i = ",dump($i);
	push @{ $json->{items} }, $i;
#	foreach my $n ( 1 .. 1000 ) {
#		my $i2 = clone $i;
#		$i2->{label} .= "_" . $n;
#		push @{ $json->{items} }, $i2;
#	}
}

print $fh encode_json( $json );
close($fh);

