# gb1.pl   07.07.21,22
use strict;
use HTML::Template;

my(%t,$n,$n1,@fld);

# Read names
open(FH,"biaozhun_name.txt") or die "Can't open the file biaozhun_name.txt.\n";
while(<FH>) {
    @fld = split;
    if (/^NAME/ ) {
        $t{th1} = $fld[1];
        $t{th2} = $fld[2]; 
        $t{th3} = $fld[3];
    } elsif ( /^SHIP/ ) {
        $t{ship}{$fld[2]} = $fld[1];
        $t{f1} = lc($fld[2]);
        $t{f2} = $t{f1} . '.txt';
        $t{f3} = $t{f1} . '.htm';
        $t{name1} = $fld[1] . '(' . $fld[2] . ')';
        push(@{ $t{txtfiles} }, $t{f2});
        push(@{ $t{htmfiles} }, $t{f3});
        push(@{ $t{names} }, $t{name1});
    }
}
close(FH);

# Read raw data and input html files
for $n ( 0 .. $#{ $t{txtfiles} } ) {
    my $template = HTML::Template->new(filename => 'std.htm');
    my @loop = ();

    $t{txtf1} = $t{txtfiles}[$n];
    $t{htmf1} = $t{htmfiles}[$n];
    $t{name1} = $t{names}[$n];
    $template->param(CNAME => $t{name1});

    open(FH,"./doc/$t{txtf1}") or die "Can't open the file $t{txtf1}.\n";
    while(<FH>){
        @fld = split;
        $t{NO1} = $fld[0] . ' ' . $fld[1];
        $t{n1} = substr($fld[2],-1);
        if ( $t{n1} =~ /[0-9]/ or $t{n1} =~ /[A-Z]/) {
            $t{NO1} = $t{NO1} . ' ' . $fld[2];
            $t{n2} = substr($fld[3],-1);
            if ( $t{n2} =~ /[0-9]/ or $t{n2} =~ /[A-Z]/) {
                $t{NO1} = $t{NO1} . ' ' . $fld[3];
                $t{c1} = $fld[4];
                $t{e1} = join(' ',@fld[5..$#fld]);
            } else {            
                $t{c1} = $fld[3];
                $t{e1} = join(' ',@fld[4..$#fld]);
            }
        } else {
            $t{NO1} = $t{NO1};
            $t{c1} = $fld[2];
            $t{e1} = join(' ',@fld[3..$#fld]);
        }
        my %row = (
                    N1 => $t{NO1},
                    C1 => $t{c1},
                    E1 => $t{e1}
                );
#       print $t{NO1},'===',$t{c1},"\n";
#       print '===',$t{e1},"\n";
        push(@loop, \%row);
    }
    close(FH);

    $template->param(std_loop => \@loop);
    open(OUT,">$t{htmf1}");
    print OUT $template->output;
    close(OUT);
#   exit;
}


__END__