#!/usr/bin/perl -w
=item
command to update all the sites:

for i in `ls /etc/opals/conf/`; do cp -p /www/opals/script/update_restoreTitle /tmp/urt; perl -pi -e "s/_MY_SITE_/$i/" /tmp/urt; sudo /tmp/urt; done

=cut
use lib '/www/opals/module';
use Opals::Context("/etc/opals/conf/mjbha_mjbha");

use strict;
use DBI;
use Opals::MarcXml qw(
    mxml_update
    mxml_addDatafield
    mxml_delDatafield
);
use JSON;
#use Getopt::Std;
use POSIX qw(
    ceil
);
my $dbh = Opals::Context->dbh();
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;
# Codes start...

my $zdbDir = Opals::Context->config('zRoot') .'/'
           . Opals::Context->config('zPort') .'/'
           . 'record' .'/'
           . Opals::Context->config('zDatabase');

my $indexDir;
my $attTbl;
my $query_insert ;

my $sth = $dbh->prepare(<<_SQL_);
select distinct rid from opl_item where rid in(19679,19686,19689,19672)  order by rid  

_SQL_

    $sth->execute;
    my $fixed=0;
    my $count =0;
    while (my ($rid) = $sth->fetchrow_array) {
        #print "$rid\n";
        my $dir =$zdbDir ."/" .ceil($rid/1000);
        my $xml =getRecXml($dir,$rid);
        my $bcList =getBcList($dbh,$rid);
        $xml=mxml_delDatafield($xml,{bcList=>$bcList});
        foreach my $bc (@$bcList){
            my $param={tag=>'852',ind1=>' ',ind2=>' ',sfList=>get852Sflist($dbh,$bc)};
            #print to_json($param,{pretty=>1}), "\n";
            $xml=mxml_addDatafield($xml,$param);
        }
        mxml_update($dbh, {rid=>$rid,marcXml=>$xml});
    }
    $sth->finish;
# Codes end.

exit 0;

sub getBcList{
    my($dbh,$rid)=@_;
    my $bcList=[];
    my $sth =$dbh->prepare("select barcode from opl_item where rid =? && barcode not regexp '^___'");
    $sth->execute($rid);
    while(my ($bc)=$sth->fetchrow_array){
        push @$bcList,$bc;
    }
    $sth->finish;
    return $bcList;

}

sub get852Sflist{
    my ($dbh,$barcode) =@_;
    my $sfList=[{code=>'p',data=>$barcode}];
    my $sth =$dbh->prepare("select sf852Code code,sf852Data data from opl_itemInfo where barcode=?");
    $sth->execute($barcode);
    while(my $sf =$sth->fetchrow_hashref){
        push @$sfList,$sf;
    }
    $sth->finish;
    return $sfList;

}

sub makeConnection {
    my ($config) = @_;
    if (!$config) {
        return;
    }
    my ($db_driver, $db_name, $db_host, $db_port, $db_user, $db_password);

    $db_driver   = $config->{'db_driver'} || 'mysql';
    $db_name     = $config->{'db_name'};
    $db_host     = $config->{'db_host'};
    $db_port     = $config->{'db_port'}   || '3306';
    $db_user     = $config->{'db_user'};
    $db_password = $config->{'db_password'};

    my $dsn = "dbi:$db_driver:$db_name:$db_host:$db_port";

    return DBI->connect($dsn, $db_user, $db_password);
}
############################################################


sub loadConfig {
    my ($configFile) = @_;
#    print "Enter the config filename of Opals: ";
#    $configFile = <STDIN>;
    my $config = {};

    open CONF, $configFile || die "Cannot open file $configFile";
    while (<CONF>) {
        chomp;
        s/#.*//;                # remove comments
        next if /^\s*$/;        # ignore blank lines

        if (/^\s*(\w+)\s*=\s*(.*?)\s*$/) {
            $config->{$1} = $2;
        }
    }
    close CONF;
    
    return $config;
}





sub getRecXml{
    my ($zdbDir,$rid) =@_;
    my $content="";
    if (-f "$zdbDir/$rid.xml"){
        open REC_DEL, "<$zdbDir/$rid.xml";
        while (<REC_DEL>) {
            $content .= $_;
        }
        close REC_DEL;
    }
    return $content;
}
#////////////////////////////////////////////////////////////////////////////
