#!/bin/sh

# disable filename globbing

 echo Content-type: text/html
 echo
 BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
DESD=`date "+%d/%m/%Y"`
[ "$1" = "" ] || DESD=$1
opera=/tmp/opera
rm tst1
ihdm opera h00"^" h01"^" h02"^" h03"^" h04"^" h05 > $opera
desba=/tmp/desb
ihdm desba h00"^" h01"^" h02"^" h04 | awk -F"^" ' FILENAME == ARGV[1] {y[$1]=$2; next }
index($2,"'"$DESD"'") > 0 {
	x[$3]++
	x1[$3""x[$3]] +=$4
}
END {
	for (i in x1) if (max+0 < substr(i,5)+0) max=substr(i,5)+0
	printf "CORR^" >"'"$desba"'"
	for (j in x) printf "%s^",y[j] >"'"$desba"'"
	print "TOTAL" >"'"$desba"'"
	for (i=1;i<=max;i++)
	{
		printf "%.4d^",i
		tr=0
		for (j in x)
		{
			ind=sprintf ("%s%s",j,i)
			printf "%8.2f^",x1[ind]
			tr +=x1[ind]
		}
		printf "%10.2f\n",tr
	}
}

 ' $opera - | sort  >> $desba

awk -F"^" ' NR==1 { 
tx=NF
	for (i=0;i<tx-1;i++)
	{
		printf "%s^",$(i+1)
		if(i==0) printf "CT^"
		else printf "RN2^"
	}
	print ""
next
}
{
	for (i=1;i<=tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}

print ""
} END {
	printf "TOTAL^"
	for (i=2;i<tx;i++)
	printf "%.2f^",tt[i]
	printf "^"
} ' $desba | tee /tmp/TST |
awk -F"^" ' BEGIN {ttop("Pizarra de Operarios");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF-1; i+=2)
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelf($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" { for (i=1; i<NF-1; i++) if(i==10) 
	if($(i-1)+0 !=0) labelf($(i+1)/$(i-1),al[i],tp[i])
	else labelf("**.**",al[i],tp[i])
	else labelf($i,al[i],tp[i])
}
$1!="TOTAL" {
if ($(NF-1)=="mi") bgcolor="#00FF00"
if ($(NF-1)=="ma") bgcolor="#FF0000"
 for (i=1; i<NF-1 ; i++) label($i,al[i],tp[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END { foot() } 
function boton(value,alig)
{
	print "<td bgcolor=\"#E1E1E1\">"
printf "<p><input type=\"submit\" style=\"width: 120; text-align:%s\" value=\"&nbsp;%s&nbsp;\" name=\"T02\"></p>\n",alg[alig],value
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
}
function label(value,alig,tip)
{
printf "<td bgcolor=\"%s\">",bgcolor
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font face=\"Arial\" size=\"4\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font face=\"Arial\" size=\"4\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") value="&nbsp"
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"4\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"4\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labels(value,alig)
{
print "<td bordercolor=\"#C0C0C0\" bgcolor=\"#003399\" align=\"center\">"
printf "<p align=\"%s\"><b><font color=\"#FFFFFF\">&nbsp;%s&nbsp;</font></b></p>\n",alg[alig],value
print "</td>"
}
function ttop(value)
{
print "<html>"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
print "<meta name=\"GENERATOR\" content=\"Microsoft FrontPage 4.0\">"
print "<meta name=\"ProgId\" content=\"FrontPage.Editor.Document\">"
print "<meta http-equiv=\"refresh\" content=\"30\">"

printf "<title>%s</title>\n",value
print "</head>"
print "<center>"
print "<body bgcolor=\"#D0D0DF\">"
##print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
printf "<H1>PIZARRA: %s</H1>\n","'"$DESD"'","'"$HAST"'"
print "<form method=\"post\" action=\"../../cgi-bin/boletbsc.cgi\" name=billform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<input type=\"hidden\" value=\"Query\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
print "</table>"
print "</center>"
print "</form>"
print "</body>"
print "</html>"
} ' -