#!/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
paci1=./tmp/paci1
D=./tmp/D
D1=./tmp/D1
P=./tmp/P
DP=./tmp/DP
V=./tmp/V
V1=./tmp/V1
inic=$1
fina=$2
perso=./tmp/perso
./appl/sh/perso  | awk -F"^" '
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
substr($16,1,2)=="00" { print trail($1)"^"trail($2) >"./tmp/"substr($11,1,2) } ' - > $perso

# |  1 H00 codigo 13 07   0 | 2 H01 identi 40 00  13 | 3 H02 nombre 40 00  53 |
# |  4 H03 patern 40 00  93 | 5 H04 matern 40 00 133 | 6 H05 direcc 40 00 173 |
# |  7 H06 comuna 25 00 213 | 8 H07 ciudad 25 00 238 | 9 H08 correo 40 00 263 |
# | 10 H09 telefo 50 00 303 |11 H10 especi 02 51 353 |12 H11 status 02 52 355 |

profs=./tmp/profs
./appl/sh/profs > $profs

# |  1 H00 codigo 02 00   0 | 2 H01 identi 20 00   2 |

sh ./appl/sh/pacie1 > $paci1

# |  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 20 00 188 | 8 H07 ciudad 20 00 208 | 9 H08 sexoss 09 50 228 |
# | 10 H09 nacimt 10 00 237 |11 H10 contac 40 00 247 |12 H11 telefo 40 00 287 |
# | 13 H12 tcelul 40 00 327 |14 H13 correo 50 00 367 |15 H14 actges 10 00 417 |
# | 16 H15 renova 10 00 427 |17 H16 diagno 80 00 437 |18 H17 observ 512 13 517 |
# | 19 H18 isapre 02 51 1029 |20 H19 fechat 10 00 1031 |21 H20 status 02 53 1041 |

# 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 total   1   01 | 

awk -F"^" ' BEGIN {  split("31:28:31:30:31:30:31:31:30:31:30:31",dm,":"); modal["01"]="Domicilio";modal["02"]="Ambulatoria" ;modal["03"]="Telemedicina"}
FILENAME==ARGV[1] { pr[$1]=trail($2); next }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
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 }
x[$1]=="" { x[$1]=trail($2)"^"trail($5)" "trail($3)"^"trail($6)"^"trail($7)"^"trail($11)"^"trail($12)"^"trail($15)"^"trail($16)"^"trail($17)"^"trail($19)"^"trail($20)"^"trail($21);reg=0 }
{
	tmrx=$16
	if(substr($21,1,2)=="00") for (i=0;i<37;i++)
	{
		dat=tmrx
		if ($(25+i)!="  ") printf "%s^%s^%s^%s^%s^%s^%.2d%.2d^%s^%s\n",$1,trail($5)" "trail($3),trail($7),pr[$22]"^"$22,modal[$23],$24,reg,i+3,$16,dat
		tmrx=tomorrow(dat)
	}
	dp[$1""sprintf("%.2d",reg)] = pr[$22]"^"modal[$23]"^"$24
	v[$1]=dat
	reg++
}
END {
for (i in v) printf "%s^%s\n",i,v[i] >"'"$V"'"
for (i in v) printf "%s^%s\n",i,x[i] >"'"$P"'"
for (i in dp) printf "%s^%s\n",i,dp[i] >"'"$DP"'"
}
' $profs $paci1 >$D
[ "$3" = "1" ] && exit

awk -F"^" ' BEGIN { inic="'"$inic"'"; fina="'"$fina"'" ;ini=dn(inic);fin=dn(fina)}
FILENAME==ARGV[1] { v[$1]=$2; next }
function dn(x) { split(x,d,"/");return d[3]""d[2]""d[1]}
{ zz=dn($10) }
zz < ini { next }
zz > fin { next }
{
	print zz"^"$0"^"v[$1]
} ' $V $D  | sort |
awk -F"^" ' { for (i=1;i<NF-1;i++)
printf "%s^",$i
if (i==3) printf "%s^%s^",$9,$11
print ""
print substr($0,10) } ' > $D1
awk -F"^" ' BEGIN { inic="'"$inic"'"; fina="'"$fina"'" ;ini=dn(inic);fin=dn(fina)}
FILENAME==ARGV[1] { v[$1]=$2; next }
function dn(x) { split(x,d,"/");return d[3]""d[2]""d[1]}
{ zz=dn($10) }
zz < ini { next }
zz > fin { next }
{
	print zz"^"$0"^"v[$1]
} ' $V $D  | sort |
awk -F"^" ' { print substr($0,10) } ' > $D1
awk -F"^" ' BEGIN { inic="'"$inic"'"; fina="'"$fina"'" ;ini=dn(inic);fin=dn(fina)}
function dn(x) { split(x,d,"/");return d[3]""d[2]""d[1]}
x[$1]!="" { next } 
{ zz=dn($11) ; x[$1]=1 }
zz < ini { next }
zz > fin { next }
{
	print zz"^"$1"^"$2"^"$3"^"$9"^"$11
} ' $D1  | sort |
awk -F"^" ' { print substr($0,10) } ' > $V1


awk -F"^" ' BEGIN { print "var pac = new Array()" }
{
 printf "pac[\"%s\"]=\"%s\"\n",$1,"&nbsp;"$2"&nbsp;<br>&nbsp;"$3"&nbsp;<br>&nbsp;"$4"&nbsp;<br>&nbsp;"$5"&nbsp;<br>&nbsp;"$6"&nbsp;<br>&nbsp;"$7"&nbsp;<br>&nbsp;"$10"&nbsp;<br>&nbsp;"substr($11,4)"&nbsp;<br>."
}
' $P >../js/pac.js
./agejs $inic $fina 
./age1 $inic $fina
