#!/opt/star/bin/perl -w

use lib "/WhereverYourModulAPIisInstalled";
use FileCatalog;

$SITE  = "BNL";
$limit = "10000";    # N at a time

$fC    = FileCatalog->new();
$fC->connect_as("Admin");

$count   = 0;
$confirm = 1;

print "Possibly restricting to $SITE\n";

do {
    undef(%KNOWN);
    print "-- $count so far (by $limit)\n";

    $offset = 0;
    $fC->clear_context();
    $fC->set_context(
                     "limit= $limit",
                     "all  = 1",
                     "rfpid= 0",
                     "nounique=1");

    @output = $fC->run_query("path");
    print "\tbatch of ".($#output+1)." records\n";

    #$fC->set_delayed();
    foreach $el (@output){
        #@items = split("::",$el);

        # faster to handle it ourselves in small buffered mode
        # and user nounique=1
        if ( $KNOWN{$el} ){ next;}

        $KNOWN{$el} = 1;
        print "\t[$el]\n";

        #
        # Since Xpath does not exist at all at this stage, and is a
        # value which is really an association with a dictionnary,
        # we need to insert it explicitly
        #
        $fC->set_context("Xpath = '".$el."'");
        if ( ($id = $fC->check_ID_for_params("Xpath")) == 0) {
            $fC->insert_dictionary_value("Xpath");
            $id = $fC->check_ID_for_params("Xpath");
        }
        $fC->clear_context();
        $fC->set_context("path =  $el",
                         "all   = 1",
                         "limit = $limit",
                         #"site = $SITE",
                         "rfpid = 0");

        #$fC->debug_on();
        if ( $n = $fC->update_location("rfpid",$id,$confirm) ){
            print "\t$n rfpid updated for path [$el]\n";
            $offset += $n;
        }
        #$fC->debug_off();
        #die "";
    }
    $count += $offset;
    $fC->print_delayed();
} while ($#output == ($limit-1) );

#$fC->print_delayed();
$fC->destroy();