#!/usr/bin/perl -w

#SITE=ztest; INFILE=/tmp/textbook.mrc; \cp -fp script/util/tbk_marc_import /tmp/ttt; perl -pi -e "s/_SITECODE_/$SITE/" /tmp/ttt; /tmp/ttt $INFILE
#
#SITE=ztest
#INFILE=/tmp/textbook.mrc
#\cp -fp script/util/tbk_marc_import /tmp/ttt
#perl -pi -e "s/_SITECODE_/$SITE/" /tmp/ttt
#/tmp/ttt $INFILE

use lib "/www/opals/module";
use Opals::Context("/etc/opals/conf/onc_la");
use Time::localtime;

use strict;
use Opals::Equipment qw(
    eq_def_getList_brief

);

use MARC::Field;
use MARC::File::USMARC;


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


my $tm = localtime;
my $dateToday = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday, $tm->hour, $tm->min, $tm->sec);

$| = 1;
# Codes start...
my $mapRecord = {

    '0eqName'  => {
                    245 => {a => 1 }, 
               },
    2       => {
                    100 => {a => 1},
                },
    3       => {    245 => { b=>1},
            },
    6      => {
                    300 => {
                            3 => 1,
                            6 => 2,
                            8 => 3,
                            a => 4, 
                            b => 5,
                            c => 6,
                            e => 7,
                            f => 8,
                            g => 9
                            },
        },
};

my $mapItem = {
    '0barcode'  => { barcode => {p=>1}},
    '0itemType' => { itemType => {'itemType' => 'Circulation'}},
    14           => { 852 => {9 => 1,}},
    13          => { 852 => {c => 1,}},

};

my $eqDef = eq_def_getList_brief($dbh);

#print $ARGV[0], "\n";
my $marcfile = MARC::File::USMARC->in($ARGV[0]);
my ($r,$i) =(0,0);
my $max = 30;
my $headline;
foreach my $id (sort keys %{$mapRecord}){

    if ($id =~ m/eqName/i){
        $headline .=  "Equipemnt Name" ;
    }
    else{
        $headline .=  $eqDef->{$id} ;
    }
    $headline .= ",";
}

foreach my $id (sort keys %{$mapItem}){
    if ($id =~ m/barcode/i ){
        $headline .= "barcode" ;
    }
    elsif ($id =~ m/itemType/i){
        $headline .= "itemType";
    }
    else{
        $headline .=  $eqDef->{$id} ;
    }
    $headline .= ",";
}

    $headline =~ s/,$//;
print $headline, "\n";
my $rec ;
my $item;
my $recEmpty;
my $data;
my $tmpBC;
#while ((my $record = $marcfile->next()) && ($r < $max)) {
while (my $record = $marcfile->next() ) {
    $r++;
    $recEmpty = '';
    $rec = '';
    foreach my $id (sort keys %{$mapRecord}){
        foreach my $tag ( keys %{$mapRecord->{$id}}){
            $rec .= '"';
            $recEmpty .= ',';
            foreach my $sf (sort {$mapRecord->{$id}{$tag}{$a} <=> $mapRecord->{$id}{$tag}{$b} } keys %{$mapRecord->{$id}{$tag}}){
                $rec .=  $record->subfield($tag,$sf)? $record->subfield($tag,$sf):'';
            }
            $rec .= '"';
            $rec .= ",";
        }
   }
    my $c = 0;
    foreach my $f ($record->field('852')) {

       $i++; 
       $c++;
       $item = '';
       if (! $f->subfield('p')){
            $tmpBC = 'TMP_BC_' . $r . '_'.$i ;
        }
       foreach my $id (sort keys %{$mapItem}){
            foreach my $tag ( keys %{$mapItem->{$id}}){
                if ($tag =~ m/itemType/){
                    $item .= '"';
                    $item .=  $mapItem->{$id}{$tag}{$tag};
                    $item .= '"';
                }
                elsif ($tag =~ m/barcode/){
                    $item .= '"';
                    $item .=  $f->subfield('p')?$f->subfield('p'):$tmpBC;
                    $item .= '"';
                }
                else{
                    $item .= '"';
                    foreach my $sf (sort {$mapItem->{$id}{$tag}{$a} <=> $mapItem->{$id}{$tag}{$b} } keys %{$mapItem->{$id}{$tag}}){
                        $item .=  $f->subfield($sf)? $f->subfield($sf):'';
                    }
                    $item .= '"';
                }
            }
            $item .=  ",";
       } 
       $data = '';
       if ($c == 1){
            $data =  $rec . $item ;
       }
       else{
            $data =  $recEmpty . $item ;
       }
    $data =~ s/,$//;
    print $data . "\n";
    }
}
#print "total records " .  $r .  "\t total holdings " .  $i . "\n";
$marcfile->close();
# Codes end.

exit 0;


############################################################
