#!/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,max(modDate) modDate
from    opl_item where barcode not regexp '^\_\_\_'  
group by rid
order by rid asc  
_SQL_
    $sth->execute;
    my $dir;
    while (my ($rid,$modDate) = $sth->fetchrow_array) {

            addMissing005($zdbDir , $rid,$modDate);
    }
    $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 addMissing005 {
    my ($zdbDir, $rid,$modDate) = @_;

    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;
    $modDate =~ s/[:\s\-]//g;
    $modDate .=".1";
    open MARCXML, "<$zdbDir/$dir/$rid.xml";
    my $fixed=0;
    while (<MARCXML>) {
        if($_ =~ m/<controlfield tag="00([\d])">/){
            if($1==5){
                return;
            }
            if($1>5 && !$fixed){
                $xml .="  <controlfield tag=\"005\">$modDate</controlfield>\n";
                $fixed=1;
            }
        }
        $xml .= $_;

    }
    #print $xml;
    print "$rid\n";
    open  RECORD, ">$zdbDir/$dir/$rid.xml";
    print RECORD $xml;
    close RECORD;

    close MARCXML;
   
      
}
############################################################

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