#!/usr/bin/perl 
use lib("/www/opals/module");
use Opals::Context('/etc/opals/conf/odev');
use strict;
use Opals::Constant; 
use Date::Calc::Object qw(
    :all
);
use Digest::SHA qw(
    sha1_hex
    sha512_hex
);

use POSIX qw(
    ceil
    floor
);

use Opals::POS;

use Opals::Search qw(
       srch_parseSearchTerm
);
use Time::localtime;


use Unicode::Normalize;

use Business::ISBN;

use CGI;
use DBI;

my $dbh = Opals::Context->dbh();
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}
my $ua = LWP::UserAgent->new;
   $ua->agent('Mozilla/8.0');

my $mimeType={"image/bmp"=>"bmp",
              "image/gif"=>"gif",
              "image/jpeg"=>"jpg",
              "image/png"=>"png"
             };


my $sth_update=$dbh->prepare("update opl_record set isbn13=? where rid=? limit 1"); 
my $sth=$dbh->prepare("select id,isbn from opl_record  where isbn is not null or isbn != '' order by rid ");  
$sth->execute();
my $isbn13;
while (my ($rid,$isbn10) = $sth->fetchrow_array) {
    $isbn13 =  formtatToIsbn13($isbn10);
    $sth_update($isbn13,$rid);
}
$sth->finish;
#============================================================================
sub formtatToIsbn13{
    my($isbn)= @_;
    $isbn=~ m/([\D^x^X]*)([\dxX][\dxX -]{8,}[\dxX])([\D^x^X]*)/;
    $isbn= $2;
    $isbn=~ s/[- ]//g;
    return $isbn if(length($isbn)== 13);
    eval{
            $isbn=~ s/^([\D]*)([0-9]+)(.*)/$2/g;
            my $isbn_obj = Business::ISBN->new($isbn) ;
            my $isbn13 = $isbn_obj->as_isbn13;
            $isbn13 = $isbn13->as_string([]);
            return $isbn13;
            } 
     or {
         return undef
      };
}


#============================================================================
sub getThumbnailPath(){
    my($id)=@_;
   
   return "/data/thumbnail/" . ceil($id/1000);

}
#============================================================================
$| = 1;


