#!/bin/sh

# disable filename globbing

 #echo Content-type: text/html
 #echo
 BASE=`pwd`
cd NCOT
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 }
{
xx=split($0,a,"=")
	printf "%s^",a[1]
	for(j=2;j<=xx;j++)
{
	if(index(a[j],ret))
	{
		r=split(a[j],b,ret)
		a[j]=b[1]
		for(i=2;i<=r;i++) a[j]=a[j]"<br>"b[i]
	}
	if(index(a[j],"|"))
	{
		r=split(a[j],b,"|")
		a[j]="&nbsp;"b[1]
		for(i=2;i<=r;i++) a[j]=a[j]"<br>"b[i]
	}
	printf "%s",a[j]
	if(j < xx ) printf "="
}
print ""
 }
' $T1 TEXT > Enc
x=`egrep cotizH13 Enc | awk '{ print substr($0,10) } '`
vend=`(cd ..;$APPLDIR/sh/assoc $x | awk -F"^" ' { print $2 } ' ) `
echo "cotizH13^"$vend >>Enc
awk -F"^" ' BEGIN {rg=0
file="COT_Files/head"
while ((getline < file) > 0) print
}
/cotiz[HT]/ { enc[$1]=$2; next } 
/cotizD..01/ { lr += sprintf("%d++",(length-10)/72)+1;if(index(toupper($0),"INSTALAC"))indx=1 }
/cotizD..03/ { enc["cotizD"substr($1,7,2)"01"]=enc["cotizD"substr($1,7,2)"01"]"<br>"$2; lr += sprintf("%d",(length-10)/72)+1;lr1=length -10 } 
/cotizD/ {
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="COT_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["cotizT3"]=""
		file="COT_Files/footer"
		while ((getline < file) > 0)
		{
	
			if(NF>1) if(index($1,"T")) 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="COT_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
