#!/bin/sh

# disable filename globbing
 echo Content-type: text/html
 echo
 BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
t=./tmp/tt
splash >$t
FEC=`awk -F= ' $1=="T1" { print $2 } ' $t `
CLI=`awk -F= ' $1=="T2" { print $2 } ' $t `
x=`./agm $1`
./pac3 $x $1


awk -F^ ' BEGIN { fi="'"$FEC"'" ; split("0:31:59:90:120:151:181:212:242:273:304:334",da,":");di=conv(fi) }
function sacap(txt) { tx="";for (i=1;i<=length(txt);i++) if(substr(txt,i,1)!=".")tx=tx""substr(txt,i,1) ; return (tx) }
function conv(ff)
{
	dd=substr(ff,1,2)
	mm=substr(ff,4,2)
	yy=substr(ff,7,4)-2021
	return (dd+da[mm+0]+yy*365)
}
FILENAME==ARGV[1] && "'"$CLI"'"!="" && "'"$CLI"'"!=substr($64,1,2) { next }
FILENAME==ARGV[1]  { if(r[$1]=="")
{
	r[$1]=sacap($2)
	if(substr($21,1,2)=="00") pa[$1]=1
}
 next }
pa[$1]!="" {
	pa[$1]=1
	x[sprintf("%s^%2.2s^%d",$1,$8,conv($10)-di)]=1

}
END {
for (i in pa) if(pa[i]!="")
{
	printf "%10.10s^",r[i]
	for(j=0;j<5;j++)
	for(k=0;k<14;k++) {
				rr=sprintf("%s^%.2d^%d",i,j,k)
				printf "%d^",x[rr]
	}
	print ""
}
	printf "^%2.2s-%2.2s-%s\n",substr(fi,4),fi,substr(fi,7)
} ' tmp/paci1 tmp/D1 | sort >../../xls/tm/newage.dat
perso=./tmp/perso.q
./appl/sh/perso | awk -F^ ' substr($16,1,2)=="00" { print $1"^"$2 } ' >$perso

# |  1 H00 codigo 12 07   0 | 2 H01 identi 40 00  12 | 3 H02 nombre 40 00  52 |
# |  4 H03 patern 40 00  92 | 5 H04 matern 40 00 132 | 6 H05 direcc 40 00 172 |
# |  7 H06 comuna 25 00 212 | 8 H07 ciudad 25 00 237 | 9 H08 correo 40 00 262 |
# | 10 H09 telefo 50 00 302 |11 H10 especi 02 51 352 |12 H11 regist 06 01 354 |
# | 13 H12 fechai 10 00 360 |14 H13 fechae 10 00 370 |15 H14 zonas  03 53 380 |
# | 16 H15 status 02 52 383 |17 H16 conara 06 00 385 |
>../../xls/tm/nage.dat
awk -F^ ' BEGIN { fi="'"$FEC"'" ; split("0:31:59:90:120:151:181:212:242:273:304:334",da,":");di=conv(fi) }
function sacap(txt) { tx="";for (i=1;i<=length(txt);i++) if(substr(txt,i,1)!=".")tx=tx""substr(txt,i,1) ; return (tx) }
function conv(ff)
{
	dd=substr(ff,1,2)
	mm=substr(ff,4,2)
	yy=substr(ff,7,4)-2021
	return (dd+da[mm+0]+yy*365)
}
FILENAME==ARGV[1] && "'"$CLI"'"!="" && "'"$CLI"'"!=substr($64,1,2) { next }
FILENAME==ARGV[1]  { if(r[$1]=="") r[$1]=$2
 next }
FILENAME==ARGV[2]  { pr[$1]=$2; next } 
FILENAME==ARGV[3]  { if(pr[$2]!="") ds[$1]=$2; next } 
{ dis=conv($10) - di }
r[$1]!="" && dis >=0 && dis <14 {
	printf "%12.12s^%s^%s^%s^%s\n",r[$1],$10,$4,ds[sprintf("%s%.2d",$1,$5-1)],pr[ds[sprintf("%s%.2d",$1,$5-1)]]
}
' tmp/paci1 $perso tmp/prtopa tmp/D1 | sort >../../xls/tm/nage.dat 
cat ../newage
