#!/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

perso=./tmp/perso.e
./appl/sh/perso | awk -F^ ' substr($16,1,2)=="00" { print trail($2)"^"trail($1)"^"trail($12)"^"trail($10)"^"trail($9) } 
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) } ' | sort  >$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 |

pacie=./tmp/pacie.e
./appl/sh/pacie | awk -F^ ' substr($21,1,2)!="00" { next }
x[$1]!=1{ print $1"^"trail($2)"^"trail($5)"^"trail($3)" "trail($4)"^"trail(substr($19,4))"^"trail(sep($7))"^"trail($15);x[$1]=1 }
{ der[$1$24]=$26 }
END {
for (i in der)  print i"^"der[i] >"'"$pacie.d"'"
}

function sep(s) { split(s,a,"|"); return a[2]}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) } ' | sort >$pacie



# |  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 999 14 502 |
# | 19 H18 isapre 02 51 1501 |20 H19 fechat 10 00 1503 |21 H20 status 02 53 1513 |
# | 22 H21 fecref 10 00 1515 |23 H22 client 02 56 1525 |

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


agend=./tmp/agend.e
./appl/sh/agend | awk -F^ ' {  x[substr($2,1,5)"^"substr($4,1,2)]=sep($5) }
function sep(s) { split(s,a,"|"); return a[1]}
END {
for (i in x) print  i"^"x[i]
} ' - | sort > $agend
# |  1 H00 regist 06 00   0 | 2 H01 pacien 05 51   6 | 3 H02 fechai 10 00  11 |
# |  4 H03 valida 04 00  21 | 5 H04 profes 12 52  25 | 6 H05 fechae 10 00  37 |
# |  7 H06 transs 01 00  47 | 8 H07 transc 20 00  48 | 9 H08 usuari 20 00  68 |
# | 10 H09 ecog00 02 01  88 |11 H10 eva000 02 01  90 |12 H11 filler 296 15  92 |

awk -F^ ' BEGIN { deriv="SIN DERIVACION^^^^"; asign="POR ASIGNAR^^^^"}
FILENAME == ARGV[1] {pac[$1]=$0}
FILENAME == ARGV[2] {prf[$2]=$0}
FILENAME == ARGV[3] {eqp[$1$2]=$3}
FILENAME == ARGV[4] {der[$1]=$2}
END {
# print "PACIENTES"
#for (i in pac) print i, pac[i]
#print "PROFESIOLES------------------------------------------------------"
#for (i in prf) print i, prf[i]
#

#print "EQUIPOP------------------------------------------------------"
#for (z in eqp) print "EQr=",z, eqp[z]

for (i in pac)
{
	printf "%s^",pac[i]
        for(j=0;j<5;j++)
	{
		#printf "i=%s j=%.2d\n",i,j
		#printf "indice=%s\n",sprintf("%.5d%.2d",i,j)
		#print "EQP=",sprintf("%.5d%.2d",i,j),"|"eqp[sprintf("%.5d%.2d",i,j)]"|",prf[eqp[sprintf("%.5d%.2d",i,j)]]
		de=sprintf("%.5d%.2d",i,j+1)
		if(der[de]+0!=0) vacio=asign
		else vacio=deriv
		if(prf[eqp[sprintf("%.5d%.2d",i,j)]]=="") printf "%s^",vacio
		else printf "%s^",prf[eqp[sprintf("%.5d%.2d",i,j)]]
	}
	print ""
}
}
' $pacie $perso $agend $pacie.d > ../../xls/tm/paeq.data


cat ../equip
