#! /usr/bin/perl

use strict;
use FileHandle;

&main();

exit 0;

sub main {
    my @array = ();
    while (<STDIN>) {
	chomp;
	push(@array, $_);
    }

    my $last_title = '';
    my $len = 0;
    my $fh = undef;
    my $s = '';
    my $count = 1;
    my @file_list = ();
    my %anchor_list = ();
    my %anchor_order = ();
    my $anchor_count = 1;
    my $file = '';
    foreach my $data (sort byrecord @array) {
	my ($title, $num, $subtitle, $date, $tape, $comment)
	    = split /,/, $data;
	$title = "$title [$comment]" if $comment eq '再';
	if ($title ne $last_title) {
	    if ($last_title ne '') {
		$s = "</TABLE>\n";
		$fh->print($s);
		$len += length $s;
		if ($len > 20*1024) {
		    &print_footer($fh);
		    $fh->close;
		    $fh = undef;
		}
	    }
	    unless ($fh) {
		$file = sprintf "list-%d.html", $count;
		$fh = new FileHandle "> $file";
		$len = &print_header($fh, $count);
		$count++;
		push @file_list, $file;
	    }
	    my $atitle = &title_decode($title);
	    $atitle =~ s/<BR>/ /gi;
	    $atitle =~ s/\s+/ /g;
	    $anchor_list{$atitle} = sprintf "%s#%d", $file, $anchor_count;
	    $anchor_order{$atitle} = $anchor_count;
	    $s = sprintf("<H2><A NAME=\"%d\">%s</A></H2>\n",
			 $anchor_count,
			 &title_decode($title));
	    $anchor_count++;
	    $fh->print($s);
	    $len += length $s;

	    $s = "<TABLE WIDTH=\"90%\" BORDER=\"1\">\n";
	    $fh->print($s);
	    $len += length $s;

	}
	$subtitle = &subtitle_decode($subtitle);

	$s = "<TR><TH WIDTH=\"5%\">$num</TH><TD WIDTH=\"70%\">$subtitle</TD><TD WIDTH=\"15%\">$date</TD><TD WIDTH=\"10%\">$tape</TD></TR>\n";
	$fh->print($s);
	$len += length $s;

	$last_title = $title;
    }

    $fh->close;

    $fh = new FileHandle ">list.html";
    &print_html_header($fh, "Tape List");
    $fh->print(<<PROLOG);
<UL>
PROLOG
    foreach my $title (sort {$anchor_order{$a} <=> $anchor_order{$b}}
		       keys %anchor_list) {
	$fh->printf("<LI><A HREF=\"%s\">%s</A></LI>\n",
		    $anchor_list{$title}, $title);
    }
    $fh->print(<<CLOSING);
</UL>
<P><A HREF="./">戻る</A></P>
CLOSING
    $fh->close;
}

sub subtitle_decode {
    my $s = shift;
    $s =~ s/<BR>/ /gi;
    $s =~ s/<P>/ /gi;
    $s =~ s/<SPAN[^>]*>//gi;
    $s =~ s/<\/SPAN>//gi;
    $s = &decode_ruby($s) if $s =~ /<RUBY>/i;
    $s =~ s/\//<BR>/gi;
    $s =~ s/&comm;/,/g;
    $s =~ s/&heart;/&#9825;/g;
    $s;
}

sub title_decode {
    my $s = shift;
    $s = &decode_ruby($s) if $s =~ /<RUBY>/i;
    $s =~ s/<P>/<BR>/g;
    $s =~ s/&comm;/,/g;
    $s =~ s/&heart;/&#9825;/g;
    $s;
}

sub decode_ruby {
    my $s = shift;
    $s =~ m,^(.*)<RUBY>(.*)</RUBY>(.*)$,i;
    my ($s1, $s2, $s3) = ($1, $2, $3);
    my ($rt, $rb);
    if ($s2 =~ m,^<RT>(.*)</RT><RB>(.*)</RB>$,i) {
	($rt, $rb) = ($1, $2);
    } elsif ($s2 =~ m,^<RB>(.*)</RB><RT>(.*)</RT>$,i) {
	($rb, $rt) = ($1, $2);
    } else {
	die "Error: Invalid substitle: $s\n";
    }
    return "$s1$rb($rt)$s3";
}

sub byrecord {
    my @x = split(/,/, $a, 6);
    my @y = split(/,/, $b, 6);
    return ("$x[0]$x[5]" cmp "$y[0]$y[5]") if ("$x[0]$x[5]" ne "$y[0]$y[5]");
    return ($x[1] <=> $y[1]) if ($x[1] != $y[1]);
    return ($x[3] cmp $y[3]) if ($x[3] ne $y[3]);
    return ($x[2] cmp $y[2]) if ($x[2] ne $y[2]);
    return 0;
}

sub print_header {
    my $fh = shift;
    my $count = shift;

    my $l = &print_html_header($fh, "Tape List -- #$count");

    my $s = <<HTMLHEAD;
<TABLE WIDTH="90%" BORDER="1">
<TR><TH WIDTH="5%">話</TH><TD WIDTH="70%">サブタイトル</TD><TD WIDTH="15%">録画日(放映日)</TD><TD WIDTH="10%"><FONT SIZE="-2">8mm/VHS<BR>- テープ番号<BR>- 位置</FONT></TD></TR>
</TABLE>
HTMLHEAD
    $fh->print($s);
    return ($l + length $s);
}

sub print_html_header {
    my ($fh, $title) = @_;

    my $s = <<HEAD;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"> 
<HEAD>
  <LINK REV=MADE HREF="mailto:AE5T-KSN\@asahi-net.or.jp">
  <META HTTP-EQUIV=CONTENT-TYPE CONTENT="text/html; charset=EUC-JP">
  <TITLE>$title</TITLE>
  <meta http-equiv="Content-Style-Type" content="text/css">
  <link rel="stylesheet" href="list.css">
</HEAD>
<BODY>
<H1>$title</H1>
HEAD
    $fh->print($s);
    return length $s;
}

sub print_footer {
    my $fh = shift;

    my $s = <<HTMLFOOT;
</TABLE>
<HR>
<P><A HREF="list.html">戻る</A></P>
</BODY>
</HTML>
HTMLFOOT
    $fh->print($s);
    return length $s;
}

# end of sort-prout.pl
