#!/usr/bin/perl

use strict;
use warnings;
use OpenILS::Utils::Cronscript;
use OpenILS::Application::AppUtils;
use JSONPrefs;
use Data::Dumper;

my $utils = 'OpenILS::Application::AppUtils';

my $script = OpenILS::Utils::Cronscript->new({nolockfile=>1});
my $login = JSONPrefs->load("$ENV{HOME}/myprefs.d/evergreen.json");
my $authtoken = $script->authenticate($login->TO_JSON);

my $results = $utils->simplereq(
    'open-ils.search',
    'open-ils.search.biblio.multiclass.query.staff',
    {offset=>0,limit=>80},
    "title: Evening Tribune Lawrence",
    1
);

my @ids = map {$_->[0]} @{$results->{ids}};

my @items;
my $pos = 1;
for my $id (sort @ids) {
    my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new();
    $item->bucket(401934);
    $item->target_biblio_record_entry($id);
    $item->pos($pos++);
    $item->create_time('now');
    push(@items, $item);
}

$results = $utils->simplereq(
    'open-ils.actor',
    'open-ils.actor.container.item.create',
    $authtoken,
    'biblio',
    \@items
);

print Dumper $results;

END {
    $script->logout() if ($authtoken);
}
