GE=tmp/GE
if [ "$1" = "" ] 
then
set newform
else
awk -F= ' { printf "%s^%s",$1,$2; for (i=3;i<=NF;i++) printf "=%s",$i;print "" } ' $2 >$GE
fi
NH=`awk -F^ ' /nformD/ && $2=="H" { nh++ } END { print nh } ' $GE` # Nro Campos de HEAD
NF=`awk -F^ ' /nformD/ && $2=="F" { nf++ } END { print nf } ' $GE` # Nro Campos de HEAD
ND=`awk -F^ ' /nformD/ && $2=="D" { nd++ } END { print nd } ' $GE` # Nro Campos de DOC
RG=`awk -F^ ' /nformH0[45]/ { if(r=="") r=$2; else p=$2  } END { print p*r } ' $GE` # Nro Filas de DOC
RW=`awk -F^ ' /nformH04/ { print $2 } ' $GE` # Nro Lineas de DOC
PG=`awk -F^ ' /nformH05/ { print $2 } ' $GE` # Nro Paginas de DOC
awk -F^ '
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
/nformD..05/ && $2+0 >50 { ind=$2+0 } 
/nformD..08/ && $2!="" { form[ind]=trail($2) }
/nformD..09/ && $2!="" { field[ind]=trail($2) }
/nformD..10/ && $2!="" { len[ind]=trail($2) }
END {
	for (i in form)
	{
		ff= "appl/"form[i]"/"form[i]".t0"
		j=-1
		while ((getline lin < ff) >0)
		{
			if(index(lin,field[i])) rr[i]=j
			j++
		}
		close(ff)
	}
	for (i in form) printf "./ihd/bin/ihmq %s h00^ h%.2d^ | awk -F^ %c { printf \"%%s^%%%.2d.%.2ds\\n\",$1,$2 } %c | sort -u -t^ -k2 > ./tmp/%s%s\n",form[i],rr[i],39,len[i],len[i],39,form[i],i
}
 ' $GE > linksh
sh linksh
rm linksh
awk -F^ ' FILENAME==ARGV[1] { x[$1]=$2;next }
NF>1 { printf $NF"\n",x[$1],x[$2] ; next }
{ print }

' $GE nform/nform.head >../$1.html

awk -F^ ' BEGIN {
form="'"$1"'"
d["16"]="button"
d["15"]="hidden"
d["00"]="text"
d["01"]="text"
d["08"]="date"
d["09"]="date"
d["51"]="select"
d["52"]="select"
d["53"]="select"
d["54"]="select"
d["55"]="select"
d["56"]="select"
d["57"]="select"
d["58"]="select"
d["59"]="select"
d["10"]="textarea"
d["11"]="textarea"
d["12"]="textarea"
d["13"]="textarea"
d["14"]="textarea"
d["17"]="checkbox"
}
/nformF/ { foot[substr($1,1,8)]=foot[substr($1,1,8)]trail($2) }
/nformH/ { head[substr($1,1,8)]=head[substr($1,1,8)]trail($2) }
/nformD/ { doc[substr($1,1,8)]=doc[substr($1,1,8)]trail($2)trail($3)trail($4)trail($5)"^" }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
function regh(rgh,par)
{
	x=split(par,xrg)
	if(xrg[6]==15) printf "    <input type=\"hidden\" id=\"%s\" name=\"%sH%.2d\">\n",xrg[2],form,rgh
        if(xrg[6]==8)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgh,xrg[4],rgh
                print "</td>"
        }
        if(xrg[6]==9)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgh,xrg[4],rgh
                print "</td>"
        }
        if(xrg[6]==0)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgh,xrg[4],rgh
                print "</td>"
        }
        if(xrg[6]+0 >50)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<%s id=\"%s\" name=\"%sH%.2d\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[2],form,rgh,rgh
                sel="./tmp/"xrg[9]xrg[6]
                print "<option value=\"\">"
                print "<option value=\" \">MOSTRAR"
                while ((getline lin <sel) >0)
                {
                        split (lin,ss)
                        printf "<option value=\"%s\">%s\n",ss[1],ss[2]
                }
                printf "</%s>\n",d[xrg[6]]
                print "</td>"
                close(sel)
        }
        if(xrg[6]==16)
        {
                #printf "<td id=\"lab\">\n"
                ##printf "    <label for=\"%s\">%s</label>\n",xrg[2],xrg[3]
                #print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" id=\"%s\" value=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[2],xrg[3],form,rgh,xrg[4],rgh
                print "</td>"
        }
        if(xrg[6]==17)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
 
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgh,rgh
                print "</td>"
        }
        if(xrg[6]==1)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgh,xrg[4],rgh
                print "</td>"
        }
        if(xrg[6]>9 && xrg[6]<15)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<%s rows=\"%d\" cols=\"%d\" id=\"%s\" name=\"%sH%.2d\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[6]-8,xrg[12],xrg[2],form,rgh,rgh
                printf "</%s>\n",d[xrg[6]]
                print "</td>"
        }
        if(xrg[8]!="") print "</tr>\n<tr>"
        return
}
function regf(rgf,par)
{
        x=split(par,xrg)
        if(xrg[6]==15) printf "    <input type=\"hidden\" id=\"%s\" name=\"%sH%.2d\">\n",xrg[2],form,rgf
        if(xrg[6]==0)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgf,xrg[4],i+nh+rw*(nd+pg*nd+1)+1
                print "</td>"
        }
        if(xrg[6]==8)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgf,xrg[4],i+nh+rw*(nd+pg*nd+1)+1
                print "</td>"
        }
        if(xrg[6]==9)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgf,xrg[4],i+nh+rw*(nd+pg*nd+1)+1
                print "</td>"
        }
        if(xrg[6]+0 >50)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<%s id=\"%s\" name=\"%sH%.2d\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[2],form,rgf,i+nh+rw*(nd+pg*nd+1)+1
                sel="./tmp/"xrg[9]xrg[6]
                print "<option value=\"\">"
                print "<option value=\" \">MOSTRAR"
                while ((getline lin <sel) >0)
                {
                        split (lin,ss)
                        printf "<option value=\"%s\">%s\n",ss[1],ss[2]
                }
                printf "</%s>\n",d[xrg[6]]
                print "</td>"
                close(sel)
        }
        if(xrg[6]==16)
        {
                #printf "<td id=\"lab\">\n"
                ##printf "    <label for=\"%s\">%s</label>\n",xrg[2],xrg[3]
                #print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" id=\"%s\" value=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[2],xrg[3],form,rgf,xrg[4],i+nh+rw*(nd+pg*nd+1)+1
                print "</td>"
        }
        if(xrg[6]==17)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgf,i+nh+rw*(nd+pg*nd+1)+1
                print "</td>"
        }
        if(xrg[6]==1)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<input type=\"%s\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"%sH%.2d\" placeholder=\"%s\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[12],xrg[7],xrg[2],form,rgf,xrg[4],i+nh+rw*(nd+pg*nd+1)+1
                print "</td>"
        }
	if(xrg[6]>9 && xrg[6]<15)
        {
                printf "<td id=\"regh\">%s",xrg[3]
                print "</td>"
                if(xrg[5]) printf "<td colspan=\"%d\">\n",xrg[5]
                else print "<td>"
                printf "<%s rows=\"%d\" cols=\"%d\" id=\"%s\" name=\"%sH%.2d\" OnChange=\"change(%d)\">\n",d[xrg[6]],xrg[6]-8,xrg[12],xrg[2],form,rgf,i+nh+rw*(nd+pg*nd+1)+1
		printf "</%s>\n",d[xrg[6]]
                print "</td>"
        }
        if(xrg[8]!="") print "</tr>\n<tr>"
        return
}
function regd(rgd,par)
{
	if(rgd==0) printf "<td><input type=\"text\" size=\"1\" id=\"T1\" name=\"T1\" value=\"%.2d\" style=\"text-align: center;\" readonly OnDblClick=\"insert(%d)\"></td>\n",++z,z-1
        x=split(par,xrg)
        if(xrg[6]==15) printf "    <input type=\"hidden\" id=\"%s\" name=\"DOC%s%.2d%.2d\">\n",xrg[2],form,z-1,z-1,rgd
        if(xrg[6]==0)
        {
                print "<td>"
                printf "<input type=\"text\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"DOC%s%.2d%.2d\" placeholder=\"%s\">\n",xrg[12],xrg[7],xrg[2],form,z-1,rgd,xrg[4]
                print "</td>"
        }
        if(xrg[6]+0 >50)
        {
                print "<td>"
                printf "<select id=\"%s\" name=\"DOC%s%.2d%.2d\">\n",xrg[2],form,z-1,rgd
                sel="./tmp/"xrg[9]xrg[6]
                print "<option value=\"\">"
                print "<option value=\" \">MOSTRAR"
                while ((getline lin <sel) >0)
                {
                        split(lin,ss)
                        printf "<option value=\"%s\">%s\n",ss[1],ss[2]
                }
                print "</select>"
                print "</td>"
                close(sel)
        }
        if(xrg[6]==1)
        {
                print "<td>"
                printf "<input type=\"text\" size=\"%d\" maxlength=\"%d\" id=\"%s\" name=\"DOC%s%.2d%.2d\" placeholder=\"%s\">\n",xrg[12],xrg[7],xrg[2],form,z-1,rgd,xrg[4]
                print "</td>"
        }
        if(xrg[6]>9 && xrg[6]<15)
        {
                print "<td>"
                printf "<textarea rows=\"%d\" cols=\"%d\" id=\"%s\" name=\"DOC%s%.2d%.2d\">\n",xrg[6]-8,xrg[12],xrg[2],form,z-1,rgd
                print "</textarea>"
                print "</td>"
        }
        return
}

function titd(td,par)
{
	if(td==0) printf "<th id=\"th\">&nbsp;##&nbsp;</th>\n"
	x=split(par,xrg)
	printf "<th id=\"th\">&nbsp;%s&nbsp;</th>\n",xrg[3]
}
END {
nh="'"$NH"'"+0 # Nro Campos de HEAD
nf="'"$NF"'"+0 # Nro Campos de HEAD
nd="'"$ND"'"+0 # Nro Campos de DOC
rw="'"$RW"'"+0 # Nro Lineas  de DOC
pg="'"$PG"'"+0 # Nro Paginas de DOC

#print nh,nd,rw,pg, i+nh+rw*(nd+pg*nd+1) >"/dev/tty"
print "<table style=\"border-top: solid 1px; border-color: #aaa;\">"
print "<tr>"
for (i=0; i<nh; i++) regh(i,doc[sprintf("nformD%.2d",i)])
print "</tr>"
print "</table>"
for(j=0;j<rw*pg;j++)
{
	for(i=0;i<nd;i++) printf "<input type=\"hidden\" name=\"%sD%.2d%.2d\">",form,j,i
	print ""
}
print "<table style=\"border: solid 1px; border-collapse: collapse; padding: 0px; border-color: #aaa;\">"
print "<tr>"
for (i=0;i<nd;i++) titd(i,doc[sprintf("nformD%.2d",i+nh)])
print "</tr>"
for (j=0;j<rw;j++)
{
	print "<tr>"
	for (i=0; i<nd; i++) regd(i,doc[sprintf("nformD%.2d",i+nh)])
	print "</tr>"
}
print "</table>"
if(pg>1) printf "<input type=\"range\" id=\"scroll\" min=\"-1\" max=\"%d\" name=\"scroll\" value=\"0\" class=\"slider\"><br>\n",rw*pg-rw+1
print "<table style=\"border-top: solid 1px; border-color: #aaa;\">"
print "<tr>"
for (i=0; i<nf; i++) regf(i+nh,doc[sprintf("nformD%.2d",i+nh+nd)])
print "</table>"
print "<table>\n<tr>"

printf "<td><br><input type=\"submit\" id=\"consultar\" value=\"Consultar\" name=\"B1\" style=\"font-family: Arial; font-size: 8pt;\" Onclick=\"cons(this)\"><td>\n"

esb=split(head["nformH10"],es,"
")
g[1]="<td align=\"center\"><font size=\"1\" face=\"Tahoma\" color=\"#003399\"><b>%s<br></b>"
g[2]="<input type=\"%s\" "
g[3]="id=\"%s\" "
g[4]="value=\"%s\" "
g[5]="placehorder=\"%s\" "
g[6]="size=\"%s\" "
g[7]="maxlen=\"%s\" "
g[8]="%s"
for (i=1;i<=esb;i++) if(es[i]!="")
{
	a=split(es[i],b,",")
	if(b[2]!="hidden") printf g[1],b[1]
	for(j=2;j<=a;j++)  if(b[j]!="") printf g[j],b[j]
	print "></td>"
}
printf "<td><br><input type=\"submit\" id=\"actualizar\" value=\"Actualizar\" disabled name=\"B1\" style=\"font-family: Arial; font-size: 8pt;\" Onclick=\"cons(this)\"><td>\n"
print "</tr>\n</table>"
printf "<script language=\"Javascript\" src=\"../js/%s.js\"></script>\n",form
j=split(head["nformH08"],js,",")
for (i=1;i<=j;i++) if(js[i]!="") printf "<script language=\"Javascript\" src=\"../js/%s.js\"></script>\n",js[i]

fil="./nform/nform.js"
fio="../js/"head["nformH01"]".js"
print "nhd="nh > fio
print "nrd="pg*rw > fio
print "ncd="nd > fio
print "lin="rw > fio
while ((getline lin <fil) >0) printf lin"\n",head["nformH01"],head["nformH01"] > fio
close(fil)
fil="./nform/nform"
fio="../"head["nformH01"]
while ((getline lin <fil) >0) printf lin"\n",head["nformH01"],head["nformH01"] > fio
close(fil)
fil="./nform/nformbsc.cgi"
fio="./"head["nformH01"]"bsc.cgi"
while ((getline lin <fil) >0) printf lin"\n",head["nformH01"] > fio
close(fil)
fil="./nform/nform.cgi"
fio="./"head["nformH01"]".cgi"
while ((getline lin <fil) >0) printf lin"\n",head["nformH01"],head["nformH01"],head["nformH01"] > fio
close(fil)
fil="./nform/nformget.cgi"
fio="./"head["nformH01"]"get.cgi"
while ((getline lin <fil) >0) 
printf lin"\n",head["nformH01"],head["nformH01"] > fio
fio="./"head["nformH01"]"list.cgi"
fil="appl/"form"/"form".tf"
printf "awk -F= %cBEGIN {\n",39 > fio
j=0
while ((getline lin <fil) >0)
 printf "T[\"%.2d\"]=\"%s\"\n",j++,toupper(lin) > fio
        printf "printf \"%%s^\",T[\"00\"]\n" > fio
print "}" >fio
close(fil)
fil="./nform/nformlist.cgi"
while ((getline lin <fil) >0) printf lin"\n",head["nformH01"],head["nformH01"] > fio
close(fil)
}
' $GE  >> ../$1.html
#cat ../$1
