#!/usr/bin/perl -w
# programming: KUBO kubo@ees.hokudai.ac.jp
# DATE: Sun Oct 27 00:10:11 JST 2002

use strict;

my ($file_table, $template) = @ARGV;
my $table_start_key = 'TABLE_START';
my $number_data_item = 7; # including dummy line
my $next_key = 'next ';

open TEMPLATE, $template or die "ERROR: can't open $template";
my @template_lines = <TEMPLATE>;
close TEMPLATE;

my $table_head = <<EOH;
<table>
        <tr>
                <th class="date"> 月/日 </th>
                <th class="tutor"> 解説担当 </th>
                <th class="chnumber"> # </th>
                <th class="chapter"> &nbsp;章 </th>
        </tr>
EOH
my @table_output = ($table_head);
open TABLE, $file_table or die "ERROR: can't open $file_table";
my @array_table = <TABLE>;
sub strong {
        my ($string) = @_;
        return "<strong class=\"red\">$string</strong>";
}
sub gray {
        my ($string) = @_;
        return "<span class=\"gray\">$string</span>";
}
while (@array_table > 0) {
        my @array_tmp;
        foreach (1 .. $number_data_item) {
                my $item = shift @array_table;
                chomp $item;
                push @array_tmp, $item;
        }
        my ($date, $tutor, $ch_number, $chapter, $memo, $info_file)
                = @array_tmp;
        if ($date =~ m/$next_key/) {
                $date =~ s/$next_key//g;
                $date = strong $date;
                $ch_number = strong $ch_number;
                $chapter = strong $chapter;
        }
        $date = gray($date) if $date =~ m/\?/o;
        if (not defined $memo or $memo eq '-') {
                $memo = '-';
        }
        else {
                $info_file = '' if not defined $info_file;
                $memo = "<a href=\"$memo\">解説</a> $info_file";
        }
        my $line = <<EOL;
        <tr>
                <td class="date">$date</td>
                <td class="tutor">$tutor</td>
                <td class="chnumber">$ch_number</td>
                <td class="chapter">$chapter ($memo)</td>
        </tr>
EOL
        push @table_output, $line;
}
close TABLE;
push @table_output, "</table>\n";

foreach (@template_lines) {
        print;
        if (m/$table_start_key/o) {
                foreach my $line (@table_output) {
                        print $line;
                }
        }
}

exit 0;