#!/bin/env perl

use strict;
use warnings;
use Getopt::Long;
use Sophie::Scan;

$ENV{LC_ALL} = 'C';
require Sophie::Scan::RpmsPath;

my $scan = Sophie::Scan->new;
$scan->update_meta_paths;

my @pkey = $scan->list_unscanned_paths;

foreach my $pathkey (@pkey) {
    
    my $time = time;
    my $mark = 0;
    my @delta;

    {
        my $path = Sophie::Scan::RpmsPath
            ->new($pathkey, Sophie::Scan->new);
        #$path->set_no_needupdate;

        $mark = $path->get_needupdate;
        @delta = $path->find_delta;
    }

    while (my @d = splice(@delta, 0, 25)) {
        my $scan = Sophie::Scan->new;
        my $path = Sophie::Scan::RpmsPath->new($pathkey, $scan);
        $path->update_content(@d);
        last if (time > $time + 15 * 60);
    }

    if (!@delta) { # update only if we finished
        my $path = Sophie::Scan::RpmsPath->new($pathkey, Sophie::Scan->new);
        $path->set_updated;
        $path->set_no_needupdate
            if ($mark ne $path->get_needupdate);
    }
}

