#!/bin/sh

# disable filename globbing

 echo Content-type: text/html
 echo
set basetit tmp/vtalocal
#set pedidos tmp/horneo
awk -F^ ' BEGIN {alg["L"]="left"; alg["C"]="center"; alg["R"]="right" }
FILENAME==ARGV[1] {
	T[$1+0]++
	TI[$1+0""T[$1+0]]=$2
	ST[$1+0""T[$1+0]]=$3
	CSP[$1+0""T[$1+0]]=$4
	RSP[$1+0""T[$1+0]]=$5
	if(row=="")  row=$1+0
	next
}
function labelf(value,style,colspan,rowspan)
{
alg["L"]="left"
alg["C"]="center"
alg["R"]="right"
align="align=\""alg[substr(style,1,1)]"\""
printf "<th colspan=\"%s\" rowspan=\"%s\" bgcolor=\"%s\">",colspan,rowspan,"#003399"
if(type=="N") printf "<align=\"%s\">&nbsp;%s&nbsp;</font>\n",alg[alig],punto(value,substr(tip,2))
else printf "%s\n",value
print "</th>"
}
function boton(value)
{
        print "<td>"
        printf "<input id=\"submit\" type=\"submit\" center value=\"%s\" name=\"T02\">\n",value
        print "</td>"
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
function label(value,style)
{
align="align=\""alg[substr(style,1,1)]"\""
type=substr(style,2,1)
if($2=="ZZ") printf "<td id=\"tt\" %s>\n",align
else printf "<td %s>",align,sty
if(type=="T") printf "&nbsp;%s\n",trail(value)
else printf "&nbsp;%s&nbsp;\n",punto(value,0)
print "</td>"
}
function punto (dat,dec)
{
        if (dat+0==0) return ""
        sig=""
        if(dat<0)
        {
                dat *= -1
                sig="-"
        }
        pun=sprintf("%%.%df",dec)
        da=sprintf (pun,dat)
        if(dec) l=length(da)-dec-1
        else l=length(da)
        dat=substr(da,l+2)
        dato=substr(da,1,l)
        if (l>6) pun=substr(dato,1,l-6)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if(l >3) pun=substr(dato,1,l-3)"."substr(dato,l-2)
        else pun=dato
        if (dec) pun=pun","dat
        return sig""pun
}
FNR==1{
print "<!DOCTYPE html>"
print "<html lang=\"en-US\">"
print "<head>"
print "<meta charset=\"UTF-8\">"
print "<meta http-equiv=\"Content-Type\" content=\"text/html\">"
print "<meta name=\"GENERATOR\" content=\"Inhedo 4gt 1.0\">"
print "<meta name=\"ProgId\" content=\"Inhedo Form Generator\">"
print "<link rel=\"stylesheet\" href=\"../css/repo.css\">"
print "<center>"

	print "<table id=\"customers\">"
	for (j=1;j<3;j++)
	{
		print "<tr>"
		for(i=1;i<T[j]+1;i++) { labelf(TI[j""i],ST[j""i],CSP[j""i],RSP[j""i])}
		print "</tr>"
	}
next
}
{
	print "<tr>"
	for(i=1;i<NF;i++)
	{
		rg=$i
		if(i<3) styl[i]="LT"
		else styl[i]="RN"
		if($i=="Z0") rg=""
		if($i=="ZZ") rg="TOTAL"
		label(rg,styl[i])
	}
	print "</tr>"
}
	
' RPHEADER/$1 $2
