###########################
#  make_std.pl
#     07.09.04

use strict;
use HTML::Template;

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

my $template = HTML::Template->new(filename => 'index.html');
my @loop = ();

print "Please input the filename(*.txt)=";
chop($t{file}=<STDIN>);
$t{name} = uc($t{file});
$t{name1} = ' ' . $t{name};
if ( $t{name} eq 'ASTM' ) {
    $t{name2} = 'ASME';
    $t{name3} = 'ANSI';
    $t{name4} = 'UL';
} else {
    $t{name2}=$t{name3}=$t{name4}='XXXXXXXX';
}
if ( $t{name} eq 'EN' ) {
    $t{name4} = 'prEN';
}

$t{htmfile} = $t{file} . '.htm';
$t{file} = $t{file} . '.txt';

open(IN,"titles.txt") or die "Can't open the file titles.txt.\n";
while(<IN>){
    @fld = split;
    if ( /^TITLE/ ) {
        if ( $fld[1] eq $t{name} ) {
            $t{title} = $fld[2];
        }
    }
}
close(IN);

$template->param(title => $t{title});

open(IN,"$t{file}") or die "Can't open the file $t{file}";
while(<IN>){
    next if $. == 1;
    chomp;
    @fld = split;
    if ( /^$t{name}/ or /^$t{name1}/ or /^$t{name2}/ or /^$t{name3}/  or /^$t{name4}/ ) {
        $t{N1} = $_;
        push(@{ $t{N1s} },$t{N1});
    } elsif ( /^\t/ ) {
    } elsif ( length($_) == 0 ) {
        print "Please input one tab in $.!\n";
        exit;
    } elsif ( /^\w/ && !/^PSM/ or /^\(/) {
        $t{elist}{$t{N1}} = $_;
    } elsif ( /^PSM/ ) {
        $t{clist}{$t{N1}} = $_;
    } else {
        $t{clist}{$t{N1}} = $_;
    }
}
close(IN);

for $n ( 0 .. $#{ $t{N1s} } ) {
    $t{N0} = $n+1;
    $t{N1} = $t{N1s}[$n];
    $t{e1} = $t{elist}{$t{N1}};
    $t{c1} = $t{clist}{$t{N1}};
    my %row = (
            N0 => $t{N0},
            N1 => $t{N1},
            C1 => $t{c1},
            E1 => $t{e1}
    );
    push(@loop, \%row);
}

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

exit;