#!/usr/bin/perl

use strict;
use DBI;
use Getopt::Std;
use POSIX qw(
    ceil
);
use JSON;
my %options = ();
getopts("c:t:",\%options);
my $indexDir = $options{c};
my $outfile  = $options{t};
if (!$indexDir || (! -f $indexDir && ! -d $indexDir)) {
    print "Usage: $0 -c directory|xml file\n";
    exit 1;
}


$| = 1;
# Codes start...


open out,">$outfile";
my @marcjsonList=();
doParse($indexDir);
print out "[\n" ,join(",\n",@marcjsonList) ,"\n]";
close out;
exit 0;
sub doParse(){
    my($fName)=@_;
    if(-d $fName){
        foreach my $f(<$fName/*>){
            doParse($f);
        }
    }
    else{
        push @marcjsonList, getMarcJson($fName),;

    }

}
############################################################
sub getMarcJson {
    my ($fname) = @_;
    my $marc={leader=>"",fields=>[]};
    my $xml = '';
    if (! -f $fname) {
        print "ERROR: $fname: not found.\n";
        return;
    }

    open MARCXML, "<$fname";
    while (<MARCXML>) {
        $xml .= $_;
    }
    close MARCXML;
    if($xml =~ s/<leader>(.*?)<\/leader>//){
        $marc->{'leader'}=$1;
    }
    while($xml =~ s/<controlfield tag="(\d{3})">(.*?)<\/controlfield>//){
        push @{$marc->{'fields'}},{$1=>$2}
    }
    while($xml =~ s/<datafield tag="(\d{3})" ind1="(\s|\d)" ind2="(\s|\d)">(.*?)<\/datafield>//s){
        my($tag,$ind1,$ind2,$sf)=($1,$2,$3,$4);
        my $sfList=[];
        while($sf =~s/<subfield code="(.?)">(.*?)<\/subfield>//){
            push @$sfList,{$1=>$2};
        }
        push @{$marc->{'fields'}},{$tag=>{ind1=>$ind1,ind2=>$ind2,subfields=>$sfList}};
    }
    return to_json($marc);
}

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




