#!/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/cegep_jac");

use strict;
use DBI;
use Getopt::Std;
use POSIX qw(
    ceil
);
my $dbh = Opals::Context->dbh();
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}
use Opals::MarcXml qw(
    mxml_delete
    mxml_del_rec_holding
);

$| = 1;
# Codes start...
my %options = ();
getopts("f:",\%options);
my $bookIdFile = $options{f};
if(!$bookIdFile || ! -f $bookIdFile){
    print "Usage: $0 -f Ebsco_bookId_file \n";
    exit 1;
}

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

my $indexDir;
my $attTbl;

open BOOKID ,"</$bookIdFile"; 

my $bookId2Del={};
while(<BOOKID>){
    my $bookId=$_;
    chomp($bookId);
    next if(length($bookId)<1);
    $bookId2Del->{$bookId}=1 ;
}
print "\n";
close BOOKID;

my $query_insert ;

my $sth = $dbh->prepare(<<_SQL_);
select rid from opl_item where  typeId='E-Book' && available=1  
_SQL_

    $sth->execute;
    my $count=0;
    my @ridArr=();
    my $count=0;
    my $sth_log=$dbh->prepare("insert into tmp_item(rid,barcode,callNumber,typeId,ebookId ) select rid,barcode,callNumber,typeId,? from opl_item where rid=? ");
    while (my ($rid) = $sth->fetchrow_array) {
        my $xml=getRecXml($zdbDir,$rid);
        #if($xml =~ m/http:\/\/search.ebscohost.jac.orc.scoolaid.net\/login.aspx\?direct=true&amp;scope=site&amp;db=nlebk&amp;db=nlabk&amp;AN=([\d]+)/){
        if($xml =~ m/http:\/\/search.ebscohost.jac.orc.scoolaid.net\/login.aspx\?direct=true&amp;scope=site&amp;db=nlebk&amp;AN=([\d]+)/){
            if($bookId2Del->{$1}){
                my $ebId=$1;
                print "--- $rid -- $ebId\n";
                $sth_log->execute($ebId,$rid);
                $count++;
                my $ridBc=getRidBc($dbh,$rid);
                mxml_del_rec_holding($dbh,$ridBc,1,0);
            }
        }
    }
    print "total:$count\n";
    $sth->finish;



# Codes end.

exit 0;
sub getRidBc{
    my($dbh, $ridList)=@_;
my  $sth_get_ridBc = $dbh->prepare("select rid,barcode from opl_item where  rid in($ridList) order by rid");
     $sth_get_ridBc->execute();
         my $ridBc={};
         while(my ($rid,$bc)= $sth_get_ridBc->fetchrow_array){
                $ridBc->{$rid}->{$bc}=1;
         }
         
         return $ridBc; 

}

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 $dir=ceil($rid/1000);
    my $content="";
    if (-f "$zdbDir/$dir/$rid.xml"){
        open REC_DEL, "<$zdbDir/$dir/$rid.xml";
        while (<REC_DEL>) {
            $content .= $_;
        }
        close REC_DEL;
    }
    return $content;
}
#////////////////////////////////////////////////////////////////////////////
