#!/usr/bin/perl -w
use lib '/www/opals/module';
use Opals::Context("/etc/opals/conf/dcmo_do");

use strict;
use DBI;
#use Getopt::Std;

use POSIX qw(
    ceil
);

use Opals::MarcXml qw(
    mxml_update

);

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 $bkDir ='/data/backup/dcmo/2010/02/record/dcmo_do/';
my $indexDir;
my $attTbl;
my $query_insert ;

my $sth = $dbh->prepare(<<_SQL_);

select  distinct rid
from    tmp_rid_1 
order by rid asc 
_SQL_

    $sth->execute;
    my $dir;
    while (my ($rid) = $sth->fetchrow_array) {
        print "$rid\n";
        my $xml=getDelRecord($dbh,$bkDir, $rid);
        if($xml ne ""){
           restoreRecord($dbh,$zdbDir,$rid,$xml);
           $rid = mxml_update($dbh, {rid=>$rid, marcXml=>$xml});
        }
    }
    $sth->finish;



# Codes end.

exit 0;

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 getDelRecord{
    my ($dbh,$bkDir,$rid) = @_;

    my $dir = ceil($rid/1000);
    my $xml ="";
    print ">/$bkDir/$dir/$rid.xml\n";
    open MARCXML, "</$bkDir/$dir/$rid.xml";
    while(<MARCXML>){
        $xml .=$_;
    }
    
    close MARCXML;

    return $xml;
}

############################################################
sub restoreRecord {
    my ($dbh,$zdbDir,$rid,$recXml) = @_;
   restoreRecord_db($dbh,$rid,$recXml);
   restoreRecord_xml($zdbDir,$rid,$recXml);
}

sub restoreRecord_db {
    my ($dbh,$rid,$marcxml) = @_;
    my $barcode="";
    while ($marcxml =~ s/([\s]*<datafield tag="852" ind1="[\d ]" ind2="[\d ]">([\s]*<subfield code="[\w\-]">.*<\/subfield>)+[\s]*<\/datafield>)//) {
        my $tmp852 =$1;
        if ($tmp852 =~ m/<subfield code="p">(.*)<\/subfield>/) {
            $barcode = $1;
            print "$barcode\n";
            if ($barcode ne''){
                $dbh->do("update opl_item set barcode='$barcode', available=1 where barcode regexp '^\_\_\_$barcode\_' " );
                $dbh->do("update opl_itemstatus set barcode=''  where barcode regexp '^\_\_\_$barcode\_' " );
                $dbh->do("delete from opl_itemstatus where barcode ='$barcode' && status=5" );
                $dbh->do("update opl_loan set barcode='$barcode' where barcode regexp '^\_\_\_$barcode\_'  ");
            }

        }
    
    }
 
}



sub restoreRecord_xml {
    my ($zdbDir,$rid,$recXml) = @_;
   
    my $dir = ceil($rid/1000);
    print ">/$zdbDir/$dir/$rid.xml\n";
    open MARCXML, ">/$zdbDir/$dir/$rid.xml";
    #open MARCXML, ">/tmp/$rid.xml";
    print MARCXML $recXml;
    close MARCXML;
}

############################################################



#////////////////////////////////////////////////////////////////////////////
