#!/usr/bin/perl

use strict;
use DBI;
use Getopt::Std;
use MARC::Record;
use MARC::File::USMARC;
use MARC::File::XML;
use MARC::Charset;
use Unicode::Normalize;
use POSIX qw(
    ceil
);
# hard-coded parameters
my $OUT_DIR = '/tmp/recovery'; 
if (! -d $OUT_DIR) {
    mkdir $OUT_DIR;
}

my %options = ();
getopts("c:",\%options);
my $configFile = $options{c};
if (!$configFile || ! -f $configFile) {
    print "Usage: $0 -c CONFIG_FILE\n";
    exit 1;
}

if (! -d $OUT_DIR) {
    mkdir $OUT_DIR;
}
my $config = loadConfig($configFile);
my $dbh = makeConnection($config);
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}
my $barcode_rid;
my $rid_barcode;
$| = 1;
my $sth = $dbh->prepare(<<_SQL_);
select distinct rid from opl_item where barcode regexp '^\_\_\_' && modDate like '2008-09-17 10%'
_SQL_
    $sth->execute();
    
    my $file;
    while ((my $rid) = $sth->fetchrow_array) {
        $file = "/data/idzebra/210/record/".$config->{'zDatabase'}.'/'.ceil($rid/1000).'/'.$rid.'.xml';
        if (-f $file) {
            print "Cannot recover: file exists: $file\n";
            next;
        }

        my $dest="$OUT_DIR/" . ceil($rid/1000);
        if (! -d $dest) {
             mkdir $dest;
        }
        my $sth_rec = $dbh->prepare("select content from opl_marcDelete where  content regexp('tag=\"001\">$rid<')");
        $sth_rec->execute();
        if((my $rec)=$sth_rec->fetchrow_array){
            open recfile, ">$dest/$rid"  .".xml";
            print recfile $rec;
            #print "$rec\n";
            close recfile;
        }
        $sth_rec->finish;
    }
    
    $sth->finish;


=item

my $barcode_rid = {
    'YGCN09813' => '3',
    'YGCN09814777' => '3',
    'YGCN09814888' => '3',
    'YGCN05710' => '5',
    'YGCN03605' => '11',
    'YGCN03606' => '11',
};

my $rid_barcode = {
    '3' => {
        'YGCN09813' => 'new',
        'YGCN09814777' => 'new',
        'YGCN09814888' => 'new',
    },
    '5' => {
        'YGCN05710' => 'new',
    },
    '11' => {
        'YGCN03605' => 'new',
        'YGCN03606' => 'new',
    },
};
=cut




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

