#!/usr/bin/perl -w

use strict;
use DBI;
use Getopt::Std;
use POSIX qw(
    ceil
);
use File::stat;
use Time::localtime;
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_marcExport change eType eType enum('date','rid','all','modDate_r','modDate_h','ridList_r','ridList_h')");
 $dbh->do("alter table opl_marcExport add ridList text ");
 $dbh->do("alter table opl_marcRecord add modDate datetime default '0000-00-00 00:00:00' ");
 $dbh->do("alter table opl_item add modDate timestamp default  CURRENT_TIMESTAMP");

my $sth = $dbh->prepare(<<_SQL_);
select  rid
from    opl_marcRecord 
order by rid asc  
_SQL_
    $sth->execute;
    my $dir;
my $sth_update = $dbh->prepare(<<_SQL_);
update opl_marcRecord 
set modDate=?
where rid= ?
_SQL_
my $date="";
    while (my ($rid) = $sth->fetchrow_array) {
        $date =getFileModDate($zdbDir, $rid);
        $sth_update->execute($date,$rid);
        print "$rid: $date\n";        
    }
    $sth->finish;

#Update opl_item set modDate field
     $sth_update = <<_STH_;
update  opl_item
set     modDate= dateImport
_STH_
    my $sth = $dbh->prepare($sth_update);
    $sth->execute();


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

    my $dateStr = '';
    my $dir = ceil($rid/1000);
    if ( -f "$zdbDir/$dir/$rid.xml") {
        my $file;
        open $file, "<$zdbDir/$dir/$rid.xml";    
        #my($sec,$min,$hour,$day,$month,$year) = localtime();
        my $t= localtime(stat($file)->mtime);
        my ($year,$month,$day,$h,$m,$s) = ($t->year+1900 ,$t->mon+1,$t->mday, $t->hour,$t->min,$t->sec );
        $dateStr =sprintf ("%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$h,$m,$s);
        close $file;
    }
    return $dateStr;
    
}
############################################################


