#!/usr/bin/perl

#input-file
$file = shift @ARGV;

open(PM, $file)|| die "ERROR: Can't open file: $file\n";
binmode PM;

#reads the whole file into @innh
while(read(PM, $buf, 1))
{
    push(@innh, $buf);
}
close(PM);

$kol=0; @line = (); $linjenr=0; $end=0; $bold=0; $center=0; $right=0;
for($i=0; $i<@innh; $i++)
{
    #counts number of columns
    if(($innh[$i] eq "\x08") && ($innh[$i+1] eq "\x6E")) { $kol++; }

    #if start of field
    if($innh[$i] eq "\xA0") { $read = 1; }
	
    #if end of field (or eof)
    elsif(($innh[$i] eq "\x78") && (($innh[$i+1] eq "\x00") || ($innh[$i] eq "\xC8"))) { $read = 0; $linjenr++;}

    #if eof
    elsif($innh[$i] eq "\xC8"){ $end = 1; }

    if(($read == 1) && ($end == 0))
    {
	#use only the data in the fields, ignore other PM-codes
	$ok = &validate_char($innh[$i]);
	if($ok == 1)
	{
		$line[$linjenr] .= $innh[$i];
	}
    }
}
#make rows
@tmp = (); $lnr=0;
for($i=0; $i<@line; $i++)
{
	if($line[$i] eq "")
	{
		$line[$i] = "&nbsp;";
	}
	if($i % $kol == 0)
	{
		$tmp[$lnr] .= $line[$i].":";
		$lnr++;
	}
	else
	{
		$tmp[$lnr] .= $line[$i].":";
	}
}

#open file for output
local($file_name, $ext) = split(/\.tbl/, $file);
open(OUT, ">"."$file_name".".html") || die "ERROR: Can't write to: $file_name.txt\n"; print OUT "<table border=1>\n";

$first = 0;
for($i=1; $i<@tmp; $i++)
{
	local($d1,$d2,$d3,$d4,$d5,$d6,$d7,$d8,$d9,$d10,$d11,$d12) = split(/:/, $tmp[$i]);
	if(($first == 0) && ($d1 eq "&nbsp;") && ($d3 eq "&nbsp;") && ($d4 eq "&nbsp;"))
	{
		$jump = $kol-1;
		print OUT "<tr><td></td><td align=center colspan=$jump>$d2</td></tr>";
		$first = 1;
	}
	else 
	{
		print OUT "<tr><td>$d1</td><td align=center>$d2</td>";
		if($d3 ne "") { print OUT "<td align=center>$d3</td>";}
		if($d4 ne "") { print OUT "<td align=center>$d4</td>";}
		if($d5 ne "") { print OUT "<td align=center>$d5</td>";}
		if($d6 ne "") { print OUT "<td align=center>$d6</td>";}
		if($d7 ne "") { print OUT "<td align=center>$d7</td>";}
		if($d8 ne "") { print OUT "<td align=center>$d8</td>";}
		if($d9 ne "") { print OUT "<td align=center>$d9</td>";}
		if($d10 ne "") { print OUT "<td align=center>$d10</td>";}
		if($d11 ne "") { print OUT "<td align=center>$d11</td>";}
		if($d12 ne "") { print OUT "<td align=center>$d12</td>";} 
	}
	print OUT "</tr>\n"; 
}
print OUT "</table>\n";
close(OUT);

#only data in fields are used
sub validate_char
{
	local($char) = $_[0];
	local($tmp) = 0;
	if($char =~ /\w/) {$tmp = 1;}
	elsif($char =~ /\d/) {$tmp = 1;}
	elsif($char =~ /\x20/) {$tmp = 1;}
	elsif($char =~ /\x21/) {$tmp = 1;}
	elsif($char =~ /\x22/) {$tmp = 1;}
	elsif($char =~ /\x23/) {$tmp = 1;}
	elsif($char =~ /\x24/) {$tmp = 1;}
	elsif($char =~ /\x25/) {$tmp = 1;}
	elsif($char =~ /\x26/) {$tmp = 1;}
	elsif($char =~ /\x28/) {$tmp = 1;}
	elsif($char =~ /\x29/) {$tmp = 1;}
	elsif($char =~ /\x2A/) {$tmp = 1;}
	elsif($char =~ /\x2B/) {$tmp = 1;}
	elsif($char =~ /\x2C/) {$tmp = 1;}
	elsif($char =~ /\x2D/) {$tmp = 1;}
	elsif($char =~ /\x2F/) {$tmp = 1;}
	elsif($char =~ /\x3C/) {$tmp = 1;}
	elsif($char =~ /\x3D/) {$tmp = 1;}
	elsif($char =~ /\x3E/) {$tmp = 1;}
	elsif($char =~ /\x3F/) {$tmp = 1;}
	elsif($char =~ /\x7B/) {$tmp = 1;}
	elsif($char =~ /\x7D/) {$tmp = 1;}

	return $tmp;
}


