#!/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_20090216 /tmp/urt; sudo perl -pi -e "s/_MY_SITE_/$i/" /tmp/urt; sudo perl /tmp/urt; done

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

use strict;
use DBI;
#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 $sth = $dbh->prepare(<<_SQL_);
select rid 
from opl_marcRecord
order by rid  
_SQL_


    $sth->execute;
    my $dir;
    while (my ($rid) = $sth->fetchrow_array) {
        if($rid){
            copy060ab2852hi_xml($dbh,$rid);
        }
    }
    
    $sth->finish;

# Codes end.

exit 0;

############################################################
sub copy060ab2852hi_xml{
   my($dbh,$rid) =@_;
   my $dir     = "$zdbDir/" . ceil($rid/1000);

    unless (-d $dir) {
            mkdir $dir, 0770;
    }        
     
    if (-f "$dir/$rid.xml") {
            my $xml = '';
            open  RECORD, "<$dir/$rid.xml";
            my $line;
            while (<RECORD>) {
                $line = $_;
                if ($line !~ m/<subfield code="-"/) {
                    $xml .= $line;
                }
            }
            my $newSubFields ="";
            my($h,$i)=get060ab($xml);
            while ($xml =~ s/([\s]*<datafield tag="852" ind1="[\d ]" ind2="[\d ]">([\s]*<subfield code="[\w\-]">.*<\/subfield>)+[\s]*<\/datafield>)//) {
                my $tmp852 =$1;
                my ($bc,$a,$b)=('','','');
                if($tmp852 =~ m/<subfield code="p">(.*)<\/subfield>/){
                    $bc=$1;
                }
               if($tmp852 =~ m/<subfield code="a">(.*)<\/subfield>/){
                    $a=$1;
                }
               if($tmp852 =~ m/<subfield code="b">(.*)<\/subfield>/){
                    $b=$1;
                }


                if($bc ne ''){
                    if($h ne ''){
                        $tmp852 =~ s/<subfield code="h">(.*)<\/subfield>/<subfield code="h">$h<\/subfield>/ ;
                    }
                    if($i ne ''){
                        $tmp852 =~ s/<subfield code="i">(.*)<\/subfield>/<subfield code="i">$i<\/subfield>/ ;
                    }
                    updateItemInfoTbl($dbh,$bc,$a,$b,$h,$i);
                }
                $newSubFields .= $tmp852;
            }
            $xml =~ s/[\s]*<\/record>/$newSubFields\n<\/record>/;
            close RECORD;
            open  RECORD, ">$dir/$rid.xml";
            #open  RECORD, ">/tmp/$rid.xml";
            print RECORD $xml;
            close RECORD;
            

    }
}

############################################################
sub get060ab{
    my($xml)=@_;
    my ($a,$b)=('','');
    if($xml =~ m/([\s]*<datafield tag="060" ind1="[\d ]" ind2="[\d ]">([\s]*<subfield code="[\w\-]">.*<\/subfield>)+[\s]*<\/datafield>)/){
        my $tmp  =$1;
        if($tmp =~ m/<subfield code="a">(.*)<\/subfield>/){
            $a=$1;
        }
       if($tmp =~ m/<subfield code="b">(.*)<\/subfield>/){
            $b=$1;
        }

    }

   return ($a,$b);
}




############################################################
sub updateItemInfoTbl {
    my ($dbh,$barcode,$a,$b,$h,$i) = @_;
    my $sth=$dbh->prepare("update opl_itemInfo set sf852Data=? where barcode =? && sf852Code=?");
    my $sth_callnum=$dbh->prepare("update opl_item set callNumber=? where barcode =? ");
    my $callNum= "$h $i";
    $sth->execute($a,$barcode,'a');
    $sth->execute($b,$barcode,'b');
    $sth->execute($h,$barcode,'h');
    $sth->execute($i,$barcode,'i');
    $sth_callnum->execute($callNum,$barcode);
    $sth->finish;
}
############################################################



