#!/usr/bin/perl -w
#
# This script is used to remove all duplicate barcodes.
#
=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/_MY_SITE_");
#use Opals::Context("/etc/opals/conf/ztest");
use Opals::Context("/etc/opals/conf/sll_mos");

use strict;
use DBI;
use Getopt::Std;
use POSIX qw(
    ceil
    floor
);
#my %options = ();
#getopts("c:",\%options);
#my $configFile = $options{c};
#if (!$configFile || ! -f $configFile) {
#    print "Usage: $0 -c CONFIG_FILE\n";
#    exit 1;
#}
#
#my $config = loadConfig($configFile);
#my $dbh = makeConnection($config);
my $dbh = Opals::Context->dbh();
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;
# fix -- update barcodes
my $zdbDir = Opals::Context->config('zRoot') .'/'
           . Opals::Context->config('zPort') .'/'
           . 'record' .'/'
           . Opals::Context->config('zDatabase');


my $sth = $dbh->prepare(<<_SQL_);
select  distinct rid
from    opl_ge852record 
where   req_id in (5,6,7) 
order by rid asc 
 
_SQL_

    $sth->execute;
    my $dir;
    while (my ($rid) = $sth->fetchrow_array) {
        if($rid){
            print "$rid\n";
            updateRecTitle_xml($zdbDir,$rid);
        }
    }
    
    $sth->finish;


############################################################
sub updateRecTitle_xml{
    my($zdbDir,$rid) =@_;
    my $dir = ceil($rid/1000);
 
    if (! -f "$zdbDir/$dir/$rid.xml") {
        print "ERROR: $zdbDir/$dir/$rid.xml: not found.\n";
        return;
    }

  print "$rid\n";
   
     
   # if (-f "$dir/$rid.xml") {
            my $xml = '';
            open  RECORD, "<$zdbDir/$dir/$rid.xml";
            my $line;
            while (<RECORD>) {
                $line = $_;
                if ($line !~ m/<subfield code="-"/) {
                    $xml .= $line;
                }
            }
            my $newSubFields="";
            my $found=0;
            my $i=0;
            my $tmp245="";
            while ($xml =~ s/([\s]*<datafield tag="245" ind1="[\d ]" ind2="[\d ]">([\s]*<subfield code="[\w\-]">.*<\/subfield>)+[\s]*<\/datafield>)/TRAN_THI_THU_HA/) {
                my $tmp =$1;
                if ($tmp =~ m/<subfield code="a">/i) {
                   $tmp245=$tmp;
                }
            }
            $xml =~ s/(TRAN_THI_THU_HA)+/$tmp245/g;
            #$xml =~ s/TRAN_THI_THU_HA/$tmp245/;
            close RECORD;
            open  RECORD, ">$zdbDir/$dir/$rid.xml";
            #open  RECORD, ">/tmp/root/$rid.xml";
            print RECORD $xml;
            close RECORD;
            

   # }
}

############################################################
sub fixBc{
    my($bc)=@_;
    $bc =~ s/^\*+|\*+$//g;
    return $bc;
}


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

