#!/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
CLIE=`awk -F= ' $1=="T3" { print $2 } ' $t `
set `cat ./AGS`
week=tmp/week
{
agend=./tmp/agend
./appl/sh/agend > $agend
awk -F"^" ' BEGIN { inic=fec("'"$1"'");fina=fec("'"$2"'")
}

fec($6) >= inic  && fec($6) <= fina && $7=="V" {
	aa=substr($2,1,5)"^"substr($4,1,2)
	print aa"^"$6"^"trail(substr($5,14))"^"trail($10)"^"trail($11)
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
function fec(x) { return substr(x,7)""substr(x,4,2)""substr(x,1,2) }
' $agend | sort -u > $agend.A

pacie=./tmp/pacie
./appl/sh/pacie1 > $pacie
awk -F"^" ' BEGIN { clie="'"$CLIE"'" }
FILENAME==ARGV[1] { x[$1]=1 ; next } 
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
clie!="" && clie!=substr($64,1,2) { next }
x[$1] || substr($21,1,2)=="00" {
if(ant!=$1) {
	printf "%s^",$1
	printf "%s^",trail($2)
	printf "%s ",trail($5)
	printf "%s ",trail($3)
	printf "%s^",trail($4)
	printf "%s^",substr(trail($19),4)
	printf "%s^",substr(trail($7),7)
	printf "%s^",trail($15)
	printf "%s^",substr(trail($21),4)
	print ""
	li=0
	fil="'"$pacie.C"'"
	rgs=index($18,"
")
	if (rgs) print $1"^"substr($18,1,rgs-1) > fil
	else print $1"^"$18 > fil
}
ant=$1
printf "%s^%.2d^%s^%s\n",$1,li++,$22,$23 >"'"$pacie.B"'"
} ' $agend.A $pacie | sort -t"^" -k3 > $pacie.A
./age $1 >$week.p
./age $1 7 >$week.f

awk -F"^" '
FILENAME==ARGV[1] { prs[$1$2]=$3+0"^"$4+0 ; next }
FILENAME==ARGV[2] { wkp[$1]=FNR ; next }
wkp[$3]!="" {
	print $1"^"wkp[$3]"^"prs[$1$2]"^"$4"^"$5"^"$6
} ' $pacie.B $week.p $agend.A | sort >$agend.B
awk -F"^" ' BEGIN {  split("31:28:31:30:31:30:31:31:30:31:30:31",dm,":")}
FILENAME==ARGV[1] { pa[$1]=1; next }
FILENAME==ARGV[2] { wkf[$1]=FNR ; next }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
function fec(x) { return substr(x,7)""substr(x,4,2)""substr(x,1,2) }
function tomorrow(dt)
{
        split(dt,dd,"/")
        dd[1]++
        if( dd[2]+0==2 && !(dd[3]%4)) ab=1
        else ab=0
        if (dd[1]+0 >dm[dd[2]+0]+ab) { dd[2]++; dd[1]=1 }
        if (dd[2]+0 >12) { dd[2]=1; dd[3]++ }
        return sprintf("%.2d/%.2d/%.4d",dd[1],dd[2],dd[3])
}
substr($22,1,2)=="  " { next }
{
        tmrx=$16
        for (i=0;i<37;i++)
        {
                dat=tmrx
                if ((substr($(25+i),1,1)=="P" || substr($(25+i),1,1)=="R") && wkf[dat]) printf "%s^%d^%d^%d\n",$1,wkf[dat],$22,$23
                tmrx=tomorrow(dat)
        }
        reg++
}
' $agend.A $week.f $pacie >$agend.C

awk -F"^" '
FILENAME==ARGV[1] { reg[$1$3$2]=$4"|"$5"|"$6"|"$7 ;if($3<3) { ec[$1]=$6; ev[$1]=$7 } ; next }
FILENAME==ARGV[2] { fut[$1$3$2]=$4; next }
FILENAME==ARGV[3] { obs[$1]=$2; next }
{
	printf "%s%s^%s^",$0,ec[$1],ev[$1]
	for (i=1;i<8;i++)
		for (j=1;j<8;j++) printf "%s^",reg[$1""i""j]
		printf "%s^^",obs[$1]
	for (i=1;i<8;i++)
		for (j=1;j<8;j++) printf "%s^",fut[$1""i""j]
	print ""
} ' $agend.B $agend.C $pacie.C $pacie.A >../../xls/tm/weekp.data

echo "^"$1 >>../../xls/tm/weekp.data
} >error/rpage.err
cat ../rpage
#sleep 5
#cat ../index.html



# |  1 H00 indice 05 15   0 | 2 H01 codigo 13 07   5 | 3 H02 patern 40 00  18 |
# |  4 H03 matern 40 00  58 | 5 H04 nombre 40 00  98 | 6 H05 direcc 50 00 138 |
# |  7 H06 comuna 05 55 188 | 8 H07 ciudad 20 00 193 | 9 H08 sexoss 09 50 213 |
# | 10 H09 nacimt 10 00 222 |11 H10 contac 40 00 232 |12 H11 telefo 40 00 272 |
# | 13 H12 tcelul 40 00 312 |14 H13 correo 50 00 352 |15 H14 actges 10 00 402 |
# | 16 H15 renova 10 00 412 |17 H16 diagno 80 00 422 |18 H17 observ 512 13 502 |
# | 19 H18 isapre 02 51 1014 |20 H19 fechat 10 00 1016 |21 H20 status 02 53 1026 |

# 22 D00 presta  2   54 | 23 D01 modali  2   52 | 24 D02 topes   2   01 |
# 25 D03 di      2   00 | 26 D04 di      2   00 | 27 D05 di      2   00 |
# 28 D06 di      2   00 | 29 D07 di      2   00 | 30 D08 di      2   00 |
# 31 D09 di      2   00 | 32 D10 di      2   00 | 33 D11 di      2   00 |
# 34 D12 di      2   00 | 35 D13 di      2   00 | 36 D14 di      2   00 |
# 37 D15 di      2   00 | 38 D16 di      2   00 | 39 D17 di      2   00 |
# 40 D18 di      2   00 | 41 D19 di      2   00 | 42 D20 di      2   00 |
# 43 D21 di      2   00 | 44 D22 di      2   00 | 45 D23 di      2   00 |
# 46 D24 di      2   00 | 47 D25 di      2   00 | 48 D26 di      2   00 |
# 49 D27 di      2   00 | 50 D28 di      2   00 | 51 D29 di      2   00 |
# 52 D30 di      2   00 | 53 D31 di      2   00 | 54 D32 di      2   00 |
# 55 D33 di      2   00 | 56 D34 di      2   00 | 57 D35 di      2   00 |
# 58 D36 di      2   00 | 59 D37 di      2   00 | 60 D38 di      2   00 |
# 61 D39 di      2   00 | 62 D40 total   1   01 |
