#!/usr/bin/perl -w
=item
command to update all the sites:

for i in `ls /etc/opals/conf/`; do cp -p /www/opals/script/fixXmlRec_escapeXml /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/cesu_j47hs");

use strict;
use DBI;
#use Getopt::Std;
use POSIX qw(
    ceil
);

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

$| = 1;

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

my $indexDir;
my $attTbl;
my $query_insert ;
open OUT,">/tmp/out.xml";
print OUT "<collection>\n";
my $sth = $dbh->prepare(<<_SQL_);

select  distinct(rid)
from    opl_item where  barcode not regexp '^\_\_\_'
&& dateImport <'2012-11-28 09:00:00' 
order by rid asc 
_SQL_
    $sth->execute;
    my $dir;
    my $path;
    my $xml;
    while (my ($rid) = $sth->fetchrow_array) {
        print "$rid\n";
        $dir = ceil($rid/1000);        
        $path="$zdbDir/$dir/$rid.xml";
        next if (! -f $path);
        $xml =getRecXml($path);
        $xml = fixMarcXml($xml);
        print OUT "$xml\n";
    }
    $sth->finish;




print OUT "</collection>";

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 fixRecXml{
    my ($zdbDir, $rid) = @_;
    my $dir = ceil($rid/1000);
    if (! -f "$zdbDir/$dir/$rid.xml") {
        print "ERROR: $zdbDir/$dir/$rid.xml: not found.\n";
        return;
    }
}

sub getRecXml {
    my($path)=@_;
    open MARCXML, "<$path";
    my $xml ="";
    while (<MARCXML>) {   
        $xml .=$_;
        
    }
    close MARCXML;
    return $xml;
        
}
sub fixMarcXml{
    my($xml)=@_;
    my $ret="";
    my $preTag;
    my $sfTag="";
    my ($dfOpen,$dfClose)=(0,1);
    my $curTag=""; 
    while($xml ne ""  && $xml =~ s/^(.*\n)(.)/$2/m){
        my $tmp=$1;
        $tmp =~ s/^\n//;
        next if($tmp eq "\n" || $tmp =~ m/^<\?xml /);
       #        print ">>>>>[$tmp]\n";
        $tmp =~ s/<leader><\/leader>/<leader>00496nam  2200157 a 4500<\/leader>/g;
        if($tmp =~ m/^<record/ && !$dfClose){
            $dfClose=1;
            $tmp  = "  </datafield>\n$tmp";
        }
        elsif($tmp =~ m/^  <datafield tag="(\d\d\d)"/){
            $curTag=$1;
            if(!$dfClose){
                $tmp  = "  </datafiel>\n$tmp";
            }
            $dfOpen=1;
            $dfClose=0;
        }
        elsif($tmp =~ m/^  <\/datafield/){
            $dfClose=1;
        }
        elsif($tmp =~ m/^(    <subfield code=".?">)/ && $tmp !~ m/<\/subfield>$/){
                $sfTag=$1;
                $tmp =~ s/$/<\/subfield>/;
        }
        elsif($tmp =~ m/^<\/subfield>/){
            $tmp="";
        }
        elsif($tmp !~ m/^<record/ && $tmp ne "" && $tmp !~ m/^[ ]+</){
                $tmp =~ s/^(.*)$/$sfTag$1/;
		$tmp =~ s/^(.*)$/$1<\/subfield>/g if($tmp !~ /(.*)<\/subfield>$/);

        }
        $tmp =~ s/&/&amp;/g;
        $tmp =~ s/&amp;(#[\d]+|amp|quot|gt|lt|apos);/&$1;/g;
        $ret .=$tmp if ($tmp ne  "");
               # print "\n<<<<<<$tmp\n";
    }
    return $ret . $xml;
}
sub saveRecXml{
    my ($path,$xml) = @_;
    #open  RECORD, ">$zdbDir/$dir/$rid.xml";
    open  RECORD, ">$path";
    print RECORD $xml;
    close RECORD;
    print "$path" ,"\n";


}
sub escapeXml {
    my ($str) = @_;
    $str =~ s/&/&amp;/g;
    $str =~ s/&amp;(#[\d]+|amp|quot|gt|lt|apos);/&$1;/g;
    return $str;
}

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




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