#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use Text::CSV_XS;
use Digest::SHA qw(
    sha1_base64
    sha1_hex
);

use Opals::Context;
use Opals::Template_ajax qw(
    tmpl_read
    tmpl_write
);

use Opals::Date qw(
    date_f005
);

use MARC::Record;
use MARC::Field;
use MARC::File::XML;
use MARC::File::USMARC;
use Opals::Marc::Record;

my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi          = CGI->new;
my $input        = $cgi->Vars();

my $fname = $input->{'file'};
my $fdata = $cgi->param('file');
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'ajax/util/createTmpEbookMarc.tmpl',
        }
    );
    my $marcStr = getDefaultEbookTmp($dbh);
    my $marc = Opals::Marc::Record::newFromXml($marcStr);
    my $title= $input->{'title'};
    my $author= $input->{'author'};
    my $isbn= $input->{'isbn'};
    my $summary= $input->{'description'};
    updateField($marc,'title',$title) if($title);
    updateField($marc,'author',$author) if($author);
    updateField($marc,'isbn',$isbn) if($isbn);
    updateField($marc,'summary',$summary) if($summary);


    my $status =1;   
    $template->param(status => $status );
    if($status){
        my @fields = $marc->field('001');
        $marc->delete_fields(@fields);
        @fields = $marc->field('852');
        $marc->delete_fields(@fields);
        my $f001 = MARC::Field->new( '001', '');
        $marc->insert_fields_ordered(($f001));
        
        my $marcXml = MARC::File::XML::record($marc);
        umask 002;
        my $tmpFile=`/bin/mktemp`;
        $tmpFile =~ s/\n$//gi;
        system("chown apache.apache $tmpFile");
        system("chmod 664 $tmpFile");
        open    MARCXML, ">$tmpFile" ;
        binmode MARCXML;
        print MARCXML $marcXml;
        close MARCXML;
        $template->param(filePath =>$tmpFile );
    }

tmpl_write($dbh, $cgi, $cookie, $template);

#------------------------------------------------------------------------------

sub updateField{
    my($marc,$fName,$fData)=@_;

    my $fMap={
             title  =>{tag=>'245',ind1=>0,ind2=>0,sfCode=>'a'},
             author =>{tag=>'100',ind1=>0,ind2=>0,sfCode=>'a'},
             isbn   =>{tag=>'020',ind1=>' ',ind2=>' ',sfCode=>'a'},
             summary=>{tag=>'520',ind1=>' ',ind2=>' ',sfCode=>'a'}
        };
    if($fMap->{$fName}){
        my $tag=$fMap->{$fName}->{'tag'};
        my $ind1=$fMap->{$fName}->{'ind1'};
        my $ind2=$fMap->{$fName}->{'ind2'};
        my $sfCode=$fMap->{$fName}->{'sfCode'};

        my @fields = $marc->field($tag);
        $marc->delete_fields(@fields);     
        my $field = MARC::Field->new($tag, $ind1,$ind2,$sfCode => $fData);
        $marc->insert_fields_ordered($field);
    }
    

   
}
#------------------------------------------------------------------------------
sub getDefaultEbookTmp{
    my ($dbh)=@_;
  my $xml="";
  my ($xml) = $dbh->selectrow_array("select  content from opl_template where name regexp 'ebook|e-book|Electronic' limit 1");

  if(!defined $xml || $xml eq ''){
      ($xml) = $dbh->selectrow_array("select  content from opl_template order by rank limit 1");
  }

    return   $xml 
}
#------------------------------------------------------------------------------
