#!/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=01/01/2010
HAST=31/01/2010
CCST=3
rm -f tmp/*
t=tmp/tt
splash> $t
DESD=`awk -F= ' $1=="T1" { print $2 } ' $t `
HAST=`awk -F= ' $1=="T2" { print $2 } ' $t `
ORIG=`awk -F= ' $1=="T3" { print $2 } ' $t `
GALN=`awk -F= ' $1=="T4" { print $2 } ' $t `
REGS=`awk -F= ' $1=="T5" { print $2 } ' $t `
awk ' END {
print "var a=new Array()"
printf "a[0]=\"%s\"\n","'"$DESD"'"
printf "a[1]=\"%s\"\n","'"$HAST"'"
printf "a[2]=\"%s\"\n","'"$ORIG"'"
printf "a[3]=\"%s\"\n","'"$GALN"'"
printf "a[4]=\"%s\"\n","'"$REGS"'"
} ' > ../js/regas.js
fdav2=./tmp/fdav2
ihdm fdav2 h00"^" h01"^" h02 > $fdav2

servi=./tmp/servi
ihdm servi h00"^" h01 >$servi

grupf=./tmp/grupf
ihdm grupf h00"^" h01 >$grupf
awk -F"^" ' BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'");galn="'"$GALN"'";orig="'"$ORIG"'"
t["M"]="Membresia"
t["G"]="Grupo GAL"
t["C"]="Consolidacion"
regs="'"$REGS"'"
if (regs!="") regs +=0
}
function rdat(rec) { return substr(rec,7,4)""substr(rec,1,2)""substr(rec,4,2) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
 FILENAME==ARGV[1] { memb[$1]=trail($2)" "trail($3); next }
 FILENAME==ARGV[2] { serv[$1]=trail($2); next }
 FILENAME==ARGV[3] { ggal[$1]=trail($2)" "trail($5); next }
rdat($7) < desd || rdat($7)> hast { next }
orig!="" && $6!=orig { next }
galn!="" && $5!=galn { next }
regs!="" && $3+0 != regs { next }
{
	printf "%s^",$3 
	printf "%s^",memb[$3]
	printf "%s^",ggal[$5]
	printf "%s^",$7
	printf "%s^",$8
	printf "%s^",t[$6]
	printf "%s^",serv[$4]
	printf "%s^",$1
	printf "%s^",$2
print "<br>"
}
' $fdav2 $servi $grupf ASIST >ASIST.0

awk -F"^" ' BEGIN { 
T["00"]="ID^CT"
T["01"]="IDENTIFICACION^LT"
T["02"]="GRUPO GAL^LT"
T["03"]="FECHA^CT"
T["04"]="HORA^RT"
T["05"]="CHK-IN&nbsp;<br>&nbsp;DESDE^LT"
T["06"]="SERVIDOR^LT"
T["07"]="USUARIO&nbsp;<br>ACREDITADO^LT"
T["08"]="IP&nbsp;<br>REMOTA^CT"
tx=10
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<=tx-1;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
		tt0[i] +=$i
	}
printf "\n"
}
END {
} ' ASIST.0 |
awk -F"^" ' BEGIN {ttop("Reporte de Asistencia General");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF-2; 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
}
index($0,"TOTAL") >0  { for (i=1; i<NF-1; i++) labelf($i,al[i],tp[i])
print "</tr>"
}
index($0,"TOTAL")==0 {
if ($(NF-1)=="mi") bgcolor="#00FF00"
if ($(NF-1)=="ma") bgcolor="#FF0000"
for (i=1; i<NF ; i++) if( i==1) boton($i,al[i]) 
else label($i,al[i],tp[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 for (i=1; i<NF ; i++) labelf("",al[i],tp[i])
 foot() } 
function boton(value,alig)
{
	print "<td bgcolor=\"#E1E1E1\">"
#printf "<p><input type=\"button\" style=\"width: 120; text-align:%s\" value=\"%s\" name=\"T02\" OnClick=\"produ(this.value,%c%s al %s%c)\"></p>\n",alg[alig],value,39,"'"$DESD"'","'"$HAST"'",39
printf "<p><input type=\"submit\" style=\"width: 120; text-align:%s\" value=\"%s\" name=\"T02\"></p>\n",alg[alig],value,39,"'"$DESD"'","'"$HAST"'",39
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>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else 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") if(value <0) printf "<p align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
else printf "<p align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") if(value=="0.00") value="&nbsp"
if(index($0,"GENERAL") || index($0,"DOCUM")) printf "<td bgcolor=\"%s\">","#003399"
else printf "<td bgcolor=\"%s\">","#004499"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&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\">"
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 "<H2>Reporte de Asistencia<br>%s al %s</H1>\n","'"$DESD"'","'"$HAST"'"
print "<form method=\"post\" action=\"../../cgi-bin/fdav2bsc.cgi\" name=billform>"
print "</H2><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 "<b>"
print NR-1" Registros informados<br>Emitido: "
dd < system("date")
print dd
print "</center>"

print "</form>"
print "</body>"
print "</html>"
printf "<script language=\"Javascript\" src=\"../js/mayor.js\"> </script>\n"
} ' -

} 2>error/mayor.err
#cat error/regas.err
