 BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
$APPLDIR/sh/tpdoc $1 |
awk -F"^" ' BEGIN {
TH["02"]=1
TD["04"]=5
TD["05"]=5
NH=4
ND=3
}

function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
FILENAME==ARGV[1] && NR==1 { for (i=1;i<=NH;i++) reg[sprintf("%.2d",i-1)]=$i }
FILENAME==ARGV[1] { for (i=NH+1;i<=NF;i++) doc[sprintf("%.2d",NR-1)sprintf("%.2d",i-1-NH)]=$i
next }
/tpdocD/ {
m0=$0
m1=""
for (i=0;i<ND;i++)
{
        x=index(m0,"tpdocD")
        y=index(substr(m0,2),">")
        re=substr(m0,x+6,4)
        m1=m1""sprintf("%s value=\"%s\"%s",substr(m0,1,y),trail(doc[re]),substr(m0,y+1,1))
        m0=substr(m0,y+2)
}
print m1
next
}
/DOCtpdoc/ { print ; next }
/textarea / {
print 
x=index($0,"tpdocH")
re=substr($0,x+6,2)
printf trail(reg[re])
next
}
/input type=\"hidden/ {
x=index($0,"tpdocH")
y=index($0,">")
re=substr($0,x+6,2)
if (re=="00") rdonly="readonly"
else rdonly=""
printf "%s value=\"%s\" %s %s\n",substr($0,1,y-1),trail(reg[re]),rdonly,substr($0,y)
next
}
/input type=\"checkbox/ {
x=index($0,"tpdocH")
y=index($0,">")
re=substr($0,x+6,2)
rdonly=""
if (re=="00") rdonly="readonly"
chkd=""
if (reg[re]+0==1) chkd="checked"
printf "%s value=\"%s\" %s %s %s\n",substr($0,1,y-1),trail(reg[re]),rdonly,chkd,substr($0,y)
next
}
/input type=\"text/ {
x=index($0,"tpdocH")
y=index($0,">")
re=substr($0,x+6,2)
if (re=="00") rdonly="readonly"
else rdonly=""
printf "%s value=\"%s\" %s %s\n",substr($0,1,y-1),trail(reg[re]),rdonly,substr($0,y)
next
}
/select name=\"tpdocH/ { print
x=index($0,"tpdocH")
indx=substr($0,x+6,2)
re=trail(substr(reg[indx],1,TH[indx]))
while (index($0,"/select") ==0 ) 
{
	getline
	y=index($0,">")
	x=index($0,"=")
	if(re==substr($0,x+2,TH[indx])) printf "%s selected %s\n",substr($0,1,y-1),substr($0,y)
	else
	print
}
next } 
/Consultar/ {
	y=index($0,">")
	printf "%s disabled %s\n",substr($0,1,y-1),substr($0,y)
	next
}
{print}
' - ../tpdoc.htm
