#!/bin/sh

# disable filename globbing

 #echo Content-type: text/html
 #echo
 BASE=`pwd`
cd NWRK
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR


T1=$1
awk -F"^" ' BEGIN { ret=sprintf("%c",13) } 
FILENAME==ARGV[2] { print ; next }
{
split($0,a,"=")
	if(index(a[2],ret))
	{
		r=split(a[2],b,ret)
		a[2]=b[1]
		for(i=2;i<=r;i++) a[2]=a[2]"<br>"b[i]
	}
	if(index(a[2],"|"))
	{
		r=split(a[2],b,"|")
		a[2]="&nbsp;"b[1]
		for(i=2;i<=r;i++) a[2]=a[2]"<br>"b[i]
	}
	print a[1]"^"a[2]
 }
' $T1 TEXT > Enc
x=`egrep wrkorH09 Enc | awk '{ print substr($0,10) } '`
vend=`(cd ..;$APPLDIR/sh/assoc $x | awk -F"^" ' { print $2 } ' ) `
echo "wrkorH09^"$vend >>Enc
x=`egrep wrkorH10 Enc | awk '{ print substr($0,10) } '`
vend=`(cd ..;$APPLDIR/sh/resot $x | awk -F"^" ' { print $2 } ' ) `
[ $x != "" ] && echo "wrkorH10^"$vend >>Enc
awk -F"^" ' BEGIN {rg=0
file="WRK_Files/head"
while ((getline < file) > 0) print
}
/wrkor[HT]/ { enc[$1]=$2; next } 
/wrkorD..01/ { lr += sprintf("%d++",(length-10)/85)+1;if(index(toupper($0),"INSTALAC"))indx=1 }
/wrkorD..03/ { enc["wrkorD"substr($1,7,2)"01"]=enc["wrkorD"substr($1,7,2)"01"]"<br>"$2; lr += sprintf("%d",(length-10)/85)+1;lr1=length -10 } 
/wrkorD/ {
if (r!=substr($1,6,3) ) { r=substr($1,6,3);enc[$1]=sprintf("%.2d",rg+1)
if(lr>35) { sl[rg-1] = lr1;sp[rg-1]=lr;lr=0 } 
rg++
 }
 else enc[$1]=$2

 next }
function header()
{
		file="WRK_Files/header"
		while ((getline < file) > 0)
		if(NF>1) printf $2"\n",enc[$1]
		else print
		close(file)
}
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 footer()
{
		if(!indx) enc["wrkorT3"]=""
		file="WRK_Files/footer"
		while ((getline < file) > 0)
		{
	
			if(NF>1) if(index($1,"H06")) printf $2"\n",enc[$1]
			else printf $2"\n",punto(enc[$1])"&nbsp;"
			else print
		}
		close(file)
}
END {
#  <tr style="border-bottom:1.0pt solid #A6A6A6;">

header()
	if (!indx) if(rg <6 && lr+0<10 ) rg=16
	if (indx) if(rg <6 && lr+0<10 ) rg =16-lr
	for (i=0;i<rg;i++)
	{
		li="itemlin"
		if(i%2) li="itemlinc"
	
		file="WRK_Files/lines0"
		while ((getline < file) > 0)
		if (NF>1)
		{
			r=sprintf($1,i)
			cc=substr(r,9)+0
			if(cc >1) printf $2"\n",li,punto(enc[r])"&nbsp;"
			else printf $2"\n",li,enc[r]
		}
		else if((i==rg-1 || sp[i]) && $0=="<tr>") print " <tr style=\"border-bottom:1.0pt solid #A6A6A6;\">"
		else print
		close(file)
		if(sp[i]+0)
		{
			print"</table>";
			#print i"-"sp[i]"--"sl[i]
#print "<p style=\"page-break-after: always;\">"
			print "<div class=\"page-break\"></div>"
			print "&nbsp;"
			header()
		}
	}
footer()
}
' Enc
