#!/usr/bin/perl

use Error qw/:try/;
use MARC::Batch;
use MARC::File::XML (BinaryEncoding => 'UTF-8');
use XML::LibXSLT;
use XML::LibXML;
use Unicode::Normalize;
use Getopt::Long;
use FileHandle;

my ($split,$enc,$marc,$out,$bad) = (100);
GetOptions(
	'split=i' => \$split,
	'marc=s'  => \$marc,
	'encoding=s'  => \$enc,
	'out_dir=s'  => \$out,
	'bad=s'  => \$bad,
);

if ($enc) {
	MARC::Charset->ignore_errors(1);
	MARC::Charset->assume_encoding($enc);
}

die "gimme some marc!\n" unless $marc;
die "gimme somewhere to put it!\n" unless $out;

my $xsl = join('',(<DATA>));

my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();

$stylesheet = $xslt->parse_stylesheet( $parser->parse_string($xsl) );

$bad = new FileHandle( $bad => '>:raw' ) if ($bad);

my $xml = '';
my $current = 1;
my $prev = 0;
my $next = 2;

my $marc = MARC::Batch->new( USMARC => $marc );
$marc->strict_off;
$marc->warnings_off;

while (my $r = $marc->next) {
	my $rxml = entityize(MARC::File::XML::record($r));
	$rxml =~ s/[\x00-\x1f]//go;

	try { $doc = $parser->parse_string($rxml); }
	catch Error with {
		my $e = shift;
		warn "arg ... bad record $current, skipping: $e\n";
		$current++;
		print $bad $r->as_usmarc if ($bad);
		$r = undef;
	};
	next unless ($r);

	$xml .= $rxml;

	unless ($current % $split) {
		$xml = <<"		XML";
			<collection xmlns="http://www.loc.gov/MARC21/slim">
				$xml
			</collection>
		XML

		my $doc;
		try { $doc = $parser->parse_string($xml); }
		catch Error with { my $e = shift; warn "ARG! Doc failed to parse:\n$e\n-------------------------------------------\n$xml\n"; };
		die unless $doc;

		$xml = '';

		my $results = $stylesheet->transform($doc, prev => "'$prev'", next => "'$next'");
		$prev++;
		$next++;

		open OUT, ">$out/$prev.html";
		print OUT $results->toString;
		close OUT;
	}
	$current++;
}

my $doc = $parser->parse_string(<<XML);
<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim">
	$xml
</marc:collection>
XML

my $results = $stylesheet->transform($doc, prev => "'$prev'", next => "'0'");
$prev++;

$stylesheet->output_file($results, "$out/$prev.html");


sub entityize {
        my $stuff = shift;
        my $form = shift; 
        
        if ($form eq 'D') {
                $stuff = NFD($stuff);
        } else {
                $stuff = NFC($stuff);
        }
        
        $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
        $stuff =~ s/([\x00-\x19])//sgoe;
        return $stuff;
}



__DATA__
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html"/>
	
	<xsl:template match="/">
		<html>
			<head>

				<style>

					.marc_table {}
					.marc_tag_row {}
					.marc_tag_data {}
					.marc_tag_col {}
					.marc_tag_ind {}
					.marc_subfields {}
					.marc_subfield_code { 
						color: var(--primary); 
						padding-left: 5px;
						padding-right: 5px; 
					}

				</style>

				<link href='/css/opac_marc.css' rel='stylesheet' type='text/css'></link>
			</head>
			<body>
				<xsl:if test="$prev &gt; 0">
					<a>
						<xsl:attribute name="href">
							<xsl:value-of select="concat( $prev, '.html')"/>
						</xsl:attribute>
						<xsl:text>Previous page</xsl:text>
					</a>
				</xsl:if>
				<span> | </span>
				<xsl:if test="$next &gt; 0">
					<a>
						<xsl:attribute name="href">
							<xsl:value-of select="concat( $next, '.html')"/>
						</xsl:attribute>
						<xsl:text>Next page</xsl:text>
					</a>
				</xsl:if>
				<hr/>
				<xsl:apply-templates select="//marc:record"/>
				<xsl:if test="$prev &gt; 0">
					<a>
						<xsl:attribute name="href">
							<xsl:value-of select="concat( $prev, '.html')"/>
						</xsl:attribute>
						<xsl:text>Previous page</xsl:text>
					</a>
				</xsl:if>
				<span> | </span>
				<xsl:if test="$next &gt; 0">
					<a>
						<xsl:attribute name="href">
							<xsl:value-of select="concat( $next, '.html')"/>
						</xsl:attribute>
						<xsl:text>Next page</xsl:text>
					</a>
				</xsl:if>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template match="marc:record">
		<table class='marc_table'>
			<tr class='marc_tag_row'>
				<th class='marc_tag_col' NOWRAP="TRUE" ALIGN="RIGHT" VALIGN="middle">
					LDR
				</th>
				<td class='marc_tag_data' COLSPAN='3'>
					<xsl:value-of select="marc:leader"/>
				</td>
			</tr>
			<xsl:apply-templates select="marc:datafield|marc:controlfield"/>
		</table>
		<hr/>
	</xsl:template>
	
	<xsl:template match="marc:controlfield">
		<tr class='marc_tag_row'>
			<th class='marc_tag_col' NOWRAP="TRUE" ALIGN="RIGHT" VALIGN="middle">
				<xsl:value-of select="@tag"/>
			</th>
			<td class='marc_tag_data' COLSPAN='3'>
				<xsl:value-of select="."/>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="marc:datafield">
		<tr class='marc_tag_row'>
			<th class='marc_tag_col' NOWRAP="TRUE" ALIGN="RIGHT" VALIGN="middle">
				<xsl:value-of select="@tag"/>
			</th>
			<td class='marc_tag_ind'>
				<xsl:value-of select="@ind1"/>
			</td>

			<td class='marc_tag_ind' style='border-left: 1px solid #A0A0A0; padding-left: 3px;'>
				<xsl:value-of select="@ind2"/>
				<span style='color:#FFF'>.</span> 
			</td>

			<td class='marc_subfields'>
				<xsl:apply-templates select="marc:subfield"/>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="marc:subfield">
		<span class='marc_subfield_code' > 
			&#8225;<xsl:value-of select="@code"/>
		</span><xsl:value-of select="."/>	
	</xsl:template>

</xsl:stylesheet>

