#!perl

use lib "$ENV{HOME}/.cpan/build/MARC-0.91/";
use MARC;

$i = shift; #"0201422190";
$xr = "rawmarc/$i.marc";
$xi = "cookmarc/$i.xml";
$xf = "cookmarc/$i-frag.html";
$xnf = "cookmarc/$i-newfrag.html";
$xfull = "cookmarc/$i-full.html";
if (! -s $xi) {
    $x=MARC->new($xr);
    $x->output({file=>">$xi",format=>"xml"});
}
if (! -s $xfull) {
    $x=MARC->new($xr) if not defined $x;
    $x->output({file=>">$xfull",format=>"html"});
}
if (-s $xf && -s $xnf) {
    print "$i already known\n";
    exit 0;
}
package slurp_biblio;
require HTML::Filter;
@ISA=qw(HTML::Filter);
# $self->start($tag, \%attr, \@attrseq, "$eaten>");

##  <field type="100" i1="1" i2=" ">
##     <subfield type="a">Boutell, Thomas.</subfield>
##  </field>
##  <field type="245" i1="1" i2="0">
##     <subfield type="a">CGI programming in C &amp; Perl /</subfield>
##     <subfield type="c">Thomas Boutell.</subfield>
##  </field>
##  <field type="246" i1="3" i2=" ">
##     <subfield type="a">CGI programming in C and Perl</subfield>
##  </field>

sub start
{
    my $self = shift;
    my $tag = $_[0];
    my $attrs = $_[1];
    

    if ($tag eq "field") {
	$infield = $attrs->{"type"};
    } elsif ($tag eq "subfield") {
	$insubfield = $attrs->{"type"};
    }

#    if (defined $_[1]->{"align"} or defined $_[1]->{"valign"}) {
#	 my $eaten = $_[3];
#	 $eaten =~ s{(<T.) .*ALIGN=[^>]*(>)}{$1$2}gi;
#	 $eaten =~ s{<TD>}{<TH>}gi if $trcount == 1;
#	 return $self->SUPER::output($eaten);
#    }
#    $self->SUPER::start(@_);
}

sub end
{
    my $self = shift;
    my $tag = $_[0];
    my $origtext = $_[1];
    undef $infield if $tag eq "field";
    undef $insubfield if $tag eq "subfield";
#    if ($tag eq "td") {
#	 $origtext =~ s{</TD>}{</TH>}gi if $trcount == 1;
#	 return $self->SUPER::output($origtext);
#    }
#    $self->SUPER::end(@_);
}

sub text
{
    my $self = shift;
    my $t = shift;

    if (defined $infield && defined $insubfield) {
	my ($p) = "$infield:$insubfield";
	# print "TEXT: $p, ";
	if ($p eq "100:a") {
 # "MAIN ENTRY--PERSONAL NAME" : "Personal name"
	    $author1 .= $t;
	} elsif ($p eq "245:c") {
 # "Remainder of title page transcription/statement of responsibility"
	    $author2 .= $t;
	} elsif ($p eq "245:a") {
 # "TITLE STATEMENT": "Title"
	    $title1 .= $t;
	} elsif ($p eq "245:b") {
 # "Remainder of title"
	    $title1 .= $t;
	} elsif ($p eq "246:a") {
 # "VARYING FORM OF TITLE" : "Title proper/short title"
	    $title2 .= $t;
	}
    }

#    $t =~ s/&nbsp;//g;
#    $self->SUPER::text($t);
}



package main;

$p = slurp_biblio->new->parse_file("$xi");

# print "ISBN: $i\n";
# print "Author1: $slurp_biblio::author1\n";
# print "Author2: $slurp_biblio::author2\n";
# print "Title1: $slurp_biblio::title1\n";
# print "Title2: $slurp_biblio::title2\n";

$title = $slurp_biblio::title1;
$author = $slurp_biblio::author2;
$author = $slurp_biblio::author1 if not defined $author;
$title =~ s{/$}{};
$title =~ s{:$}{};

print "ISBN: $i\n";
print "Title:  $title\n";
print "Author: $author\n";

open(XF, ">$xf");
print XF qq{<li><a href="marc$i.html"><i>$title</i></a><br>\n};
print XF qq{<br>\n$author<br>\nISBN $i</li>\n};
close(XF);

open(XNF, ">$xnf");
print XNF qq{<dt><a href="marc$i.html"><i>$title</i></a></dt>\n};
print XNF qq{<dd>\n$author<br>\nISBN $i</dd>\n};
close(XNF);
