#!/bin/sh

# disable filename globbing
BASE=`pwd`
HOME=$BASE
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin
export APPLDIR DATADIR PATH HOME
param=./tmp/param
wrkor=./tmp/wrkor
repo1=../../xls/es/repo1
DESD=`awk -F= ' $1=="repo1H01" { print $2 } ' $1`
HAST=`awk -F= ' $1=="repo1H02" { print $2 } ' $1`
CLIE=`awk -F= ' $1=="repo1H03" { print $2 } ' $1`
VEND=`awk -F= ' $1=="repo1H04" { print $2 } ' $1`
STAT=`awk -F= ' $1=="repo1H05" { print $2 } ' $1`
cotiz=./tmp/cotiz
awk -F"^" '
function fec(ff) {return substr(ff,7)substr(ff,4,2)substr(ff,1,2)}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
BEGIN {
desd=fec("'"$DESD"'")
hast=fec("'"$HAST"'")
clie="'"$CLIE"'"
vend="'"$VEND"'"
stat="'"$STAT"'"
ret=sprintf("%c",13)
x1="1,2,3,6,11,14,15,"
z=split(x1,x,",")
}
FILENAME==ARGV[1] && substr($12,1,2)=="99" { next } # OT NULA
FILENAME==ARGV[1] && ant!=$1 { vot[$3]=vot[$3]$1"|";ot[$3]=ot[$3]$1"!"t; fot[$3]=fot[$3]$2"!"
#print "vot="$3,vot[$3] >"/dev/tty"
#print "ot="$3,ot[$3] >"/dev/tty"
#print "fot="$3,fot[$3] >"/dev/tty"
ant=$1 }
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
}
FILENAME==ARGV[1] { val[$1] +=$20*$21;next }
ott=="" {
for (i in vot)
{
	print i,vot[i] > "tmp/vot"
	x1=split(vot[i],d,"|")
	for (j=1;j<x1;j++) vl[i]=vl[i]val[d[j]]"!"
	for (j=1;j<x1;j++) print d[j],val[d[j]]"!" >"tmp/vot"
}
for (i in vl) print i,vl[i] > "tmp/vl"
ott=1 }
fec($2) < desd { next }
fec($2) > hast { next }
clie+0!=0 && clie!=substr($3,1,5) { next }
vend+0!=0 && vend!=substr($14,1,3) { next }
stat+0!=0 && stat!=substr($15,1,2) { next }

clie+0!=0 && cl=="" {cl=substr($3,7)}
vend+0!=0 && vd=="" {vd=substr($14,5)}
stat+0!=0 && st=="" {st=substr($15,4)}
ant!=$1 { 
for (i=1;i<z;i++)  if(i==4) {
#print >"/dev/tty"
#print "cli0="$x[i] >"/dev/tty"
	split(trail($x[i]),c,ret)
	printf "%s",trail(c[1])
	if(c[4]!="" && c[4]!=" -") printf "%s%s","!",c[4]
	printf "^"
}
else
{
#print "cli"i"="x[i],$x[i]>"/dev/tty"
	zz=$x[i]
	if(index(zz,"|")) { split(zz,c,"|"); zz=c[2] }
	printf "%s^",trail(zz)
}
	
print ot[$1]"^"fot[$1]"^"vl[$1]
ant=$1

}
END {
print "'"$DESD"'" >"'"$param"'"
print "'"$HAST"'" >"'"$param"'"
print cl >"'"$param"'"
print vd >"'"$param"'"
print st >"'"$param"'"
}
 ' $wrkor $cotiz >$cotiz.f
awk -F"^" '
{
x1=split($4,ob,"!")
split(ob[2],ob1,"|")
x2=split($8,ot,"!")-1
split($9,fo,"!")
split($10,vl,"!")
if (x2 > x1) x1=x2+1
if(x1 < 1) print
else
{
	i=1
	printf "%s^",$1
	printf "%s^",$2
	printf "%s^",$3
	printf "%s^",ob[i]" - "ob1[i]
	printf "%s^",$5
	printf "%s^",$6
	printf "%s^",$7
	printf "%s^",ot[i]
	printf "%s^",fo[i]
	printf "%s\n",vl[i]
	for (i=2;i<x1;i++)
	{
		printf "^",$1
		printf "^",$2
		printf "^",$3
		printf "^",ob[i]
		printf "^",$5
		printf "^",$6
		printf "^",$7
		printf "%s^",ot[i]
		printf "%s^",fo[i]
		printf "%s\n",vl[i]
	}
}
} ' $cotiz.f > $repo1.dat
cp $param ../../xls/es
