#!/usr/bin/perl -w

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

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);
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;
# Codes start...

my $zdbDir = $config->{'zRoot'} .'/'
           . $config->{'zPort'} .'/'
           . 'record' .'/'
           . $config->{'zDatabase'};

my $indexDir;
my $attTbl;
my $query_insert ;
#$dbh->do("alter table opl_marcRecord add leader varchar(24) default null, add cf_008 varchar(40) default null");
    my $sth = $dbh->prepare(<<_SQL_);
select distinct rid
from    opl_item where barcode not regexp '^\_\_\_'  
order by rid asc  
_SQL_
    $sth->execute;
    my $dir;
    while (my ($rid) = $sth->fetchrow_array) {
            dedup008($zdbDir , $rid);
    }
    $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 dedup008 {
    my ($zdbDir, $rid) = @_;

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

    #print "$rid\n";
    #return;

    open MARCXML, "<$zdbDir/$dir/$rid.xml";
    while (<MARCXML>) {
        $xml .= $_;
    }
    close MARCXML;
    my $marker_008="___OPALS_MARC_XML_controlfield008marker_f801d6faada54a60faa577bbb60f33___";
    if($xml =~ m/(\s*<controlfield tag="008">.*<\/controlfield>\s*\n)/){
        my $tmp =$1;
        $xml =~ s/\s*<controlfield tag="008">.*<\/controlfield>\s*\n/$marker_008/;
        if( $xml =~ s/[\t ]*<controlfield tag="008">.*<\/controlfield>\s*\n//g){
            print "$rid\n";
            $xml =~ s/$marker_008/$tmp/;
            open  RECORD, ">$zdbDir/$dir/$rid.xml";
            #open  RECORD, ">/tmp/$rid.xml";
            print RECORD $xml;
            close RECORD;
            system("chown apache.apache  $zdbDir/$dir/$rid.xml");
            system("chmod 664 $zdbDir/$dir/$rid.xml");
        }

    }

      
}
############################################################

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