#!/usr/bin/perl -w
#
# sudo /www/opals/script/removeBcPrefix -c /etc/opals/conf/ztest
#

use lib '/www/opals/module';
use Opals::Context("/etc/opals/conf/cegep_jac");
use strict;
use DBI;
use Getopt::Std;
use POSIX qw(
    ceil
    floor
);



my $dbh = Opals::Context->dbh();
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;
# fix -- update barcodes
#my ($oldHost,$newHost)=('digital.films.com','digital.films.jac.orc.scoolaid.net');
my ($oldHost,$newHost)=('www.nfb.ca','nfb.jac.orc.scoolaid.net');
#$oldHost =~ s/\./\\\./g;

print "$oldHost \n";
my $sth = $dbh->prepare(<<_SQL_);
select distinct rid 
from opl_item
order by rid  
_SQL_


    $sth->execute;
    my $dir;
    while (my ($rid) = $sth->fetchrow_array) {
        if($rid){
            change856uHost($dbh,$rid,$oldHost,$newHost);
            #add856sf3($dbh,$rid,$oldHost,"Click to view video");
        }
    }
    
    $sth->finish;


############################################################
sub change856uHost{
    my($dbh,$rid,$oldHost,$newHost) =@_;
    my $zRoot   =  Opals::Context->config('zRoot');
    my $zPort   =  Opals::Context->config('zPort');
    my $zDatabase = Opals::Context->config('zDatabase');
 
    my $dir     = "$zRoot/$zPort/record/$zDatabase/" . 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 $found=0;

            #http://0-www.ln.naxosmusiclibrary.com.innopac.ln.edu.hk/streamcat.asp?s=4014%2flnu01&item_code=ABCD244
            #http://asfa.naxosmusiclibrary.com/streamcat.asp?s=55986%2fasfa05&item_code=ABCD244 
            
            my $change=0; 
            while ($xml =~ s/([\s]*<datafield tag="856" ind1="[\d ]" ind2="[\d ]">([\s]*<subfield code="[\w\-]">.*<\/subfield>)+[\s]*<\/datafield>)//) {
                my $tmp856 =$1;
                if($tmp856 =~ s/<subfield code="u">(http:\/\/)($oldHost)(.*)<\/subfield>/<subfield code="u">$1$newHost$3<\/subfield>/gi){
                     print "$rid \n";   
                     $change=1;              
                }
                $newSubFields .= $tmp856;
            }
            close RECORD;
            if($change){
                $xml =~ s/[\s]*<\/record>/$newSubFields\n<\/record>/;
                open  RECORD, ">$dir/$rid.xml";
#                open  RECORD, ">/tmp/$rid.xml";
                print RECORD $xml;
                close RECORD;
            }

    }
}
############################################################
sub add856sf3{
    my($dbh,$rid,$host,$data) =@_;
   my $zRoot   =  Opals::Context->config('zRoot');
    my $zPort   =  Opals::Context->config('zPort');
    my $zDatabase = Opals::Context->config('zDatabase');
 
    my $dir     = "$zRoot/$zPort/record/$zDatabase/" . 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>) {
                $xml .= $_;
            }
            my $newSubFields="";
            my $found=0;
            #http://0-www.ln.naxosmusiclibrary.com.innopac.ln.edu.hk/streamcat.asp?s=4014%2flnu01&item_code=ABCD244
            #http://asfa.naxosmusiclibrary.com/streamcat.asp?s=55986%2fasfa05&item_code=ABCD244 
            
            my $change=0; 
            while ($xml =~ s/([\s]*<datafield tag="856" ind1="[\d ]" ind2="[\d ]">([\s]*<subfield code="[\w\-]">.*<\/subfield>)+[\s]*<\/datafield>)//) {
                my $tmp856 =$1;
                if($tmp856 !~ m/<subfield code="3">(.*)<\/subfield>/ &&
                   $tmp856 =~ m/<subfield code="z">(.*)<\/subfield>/ && 
                   $tmp856 =~ m/<subfield code="u">(http:\/\/)digital.films.jac.orc.scoolaid.net(.*)<\/subfield>/gi ){
                     $tmp856 =~ s/(<datafield tag="856" ind1="[\d ]" ind2="[\d ]">)/$1\n    <subfield code="3">$data<\/subfield>/gi;
                     print "$rid \n";   
                     $change=1;              
                }
                $newSubFields .= $tmp856;
            }
            close RECORD;
            if($change){
                $xml =~ s/[\s]*<\/record>/$newSubFields\n<\/record>/;
                open  RECORD, ">$dir/$rid.xml";
#                open  RECORD, ">/tmp/$rid.xml";
                print RECORD $xml;
                close RECORD;
            }

    }

}
############################################################
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;
}
############################################################


exit 0;

