BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
frm=`echo $* |./get | awk ' { print $1 }'`
FIL=`echo $* |./get | awk ' { print $2 }'`
deb=`echo $* |./get | awk ' { print $3 }'`

SRC=tmp/$frm"list.cgi"
RES="appl/$frm/$frm.t0 appl/$frm/$frm.t1"
echo $FIL $SRC,$frm,$RES,
{

awk -F: ' BEGIN { h[0]=1 ; z=0 }
{ split($0,x,"=") }
FILENAME==ARGV[1] && /H/ { h[substr(x[1],length(x[1])-1,2)+0]=1 }
FILENAME==ARGV[1] && /D/ { d[substr(x[1],length(x[1])-3,2)+0]=1 }
FILENAME==ARGV[1] { next }
FILENAME==ARGV[2] && FNR==1 { next }
FILENAME==ARGV[2] && h[FNR-2] { fr[z++]=$3 }
FILENAME==ARGV[3] && d[FNR-1] { fr[z++]=$3 }
END { for (i=0;i<z;i++) printf "%d^",fr[i]
print "" }
' $FIL $RES >XLS/FRM

awk -F^ ' { nf=NF}
END {
getline < "XLS/FRM"
alg=0
ffc=0
for (i=1;i<NF;i++) if($i==15 || $i==8 || $i==9) { alig[i]=1; if(i>1 && i<NF-1) alg++ }
for (i=1;i<NF;i++) if($i==8 || $i==9) { fec[i]=1;if(i>1 && i<NF-1) ffc++ }
print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
"
print "<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" mc:Ignorable=\"x14ac\">"
if(ffc|| fec[1] || fec[NF-1]) {
print "  <numFmts count=\"1\">"
print "    <numFmt numFmtId=\"165\" formatCode=\"yyyy/mm/dd;@\"/>"
print "  </numFmts>"
} 
print "  <fonts count=\"2\" x14ac:knownFonts=\"1\">"
print "    <font>"
print "      <sz val=\"11\"/>"
print "      <color theme=\"1\"/>"
print "      <name val=\"Calibri\"/>"
print "      <family val=\"2\"/>"
print "      <scheme val=\"minor\"/>"
print "    </font>"
print "    <font>"
print "      <b/>"
print "      <sz val=\"11\"/>"
print "      <color rgb=\"FFFFFF00\"/>"
print "      <name val=\"Calibri\"/>"
print "      <family val=\"2\"/>"
print "      <scheme val=\"minor\"/>"
print "    </font>"
print "    <font>"
print "      <b/>"
print "      <sz val=\"14\"/>"
print "      <color theme=\"1\"/>"
print "      <name val=\"Calibri\"/>"
print "      <family val=\"2\"/>"
print "      <scheme val=\"minor\"/>"
print "    </font>"
print "  </fonts>"
print "  <fills count=\"3\">"
print "    <fill>"
print "      <patternFill patternType=\"none\"/>"
print "    </fill>"
print "    <fill>"
print "      <patternFill patternType=\"gray125\"/>"
print "    </fill>"
print "    <fill>"
print "      <patternFill patternType=\"solid\">"
print "        <fgColor rgb=\"FF0070C0\"/>"
print "        <bgColor indexed=\"64\"/>"
print "      </patternFill>"
print "    </fill>"
print "  </fills>"
nf=4
nr=4
f=nr*(nf-1)
print "   <borders count=\"10\">"
print "    <border>"
print "      <left/>"
print "      <right/>"
print "      <top/>"
print "      <bottom/>"
print "      <diagonal/>"
print "    </border>"
ffl[1]="medium"
ffr[1]="thin"
fft[1]="medium"
ffb[1]="medium"

ffl[2]="thin"
ffr[2]="thin"
fft[2]="medium"
ffb[2]="medium"

ffl[3]="thin"
ffr[3]="medium"
fft[3]="medium"
ffb[3]="medium"

ffl[4]="medium"
ffr[4]="thin"
fft[4]=""
ffb[4]="thin"

ffl[5]="thin"
ffr[5]="thin"
fft[5]=""
ffb[5]="thin"

ffl[6]="thin"
ffr[6]="medium"
fft[6]=""
ffb[6]="thin"

ffl[7]="medium"
ffr[7]="thin"
fft[7]=""
ffb[7]="medium"

ffl[8]="thin"
ffr[8]="thin"
fft[8]=""
ffb[8]="medium"

ffl[9]="thin"
ffr[9]="medium"
fft[9]=""
ffb[9]="medium"
for (z=1;z<10; z++)
{
#z=2
#if(i==1) z=1
#if(i==nf-1) z=3
#printf "    <border %d>\n",z
printf "    <border>\n"
printf "      <left style=\"%s\">\n",ffl[z]
printf "        <color auto=\"1\"/>\n"
printf "      </left>\n"
printf "      <right style=\"%s\">\n",ffr[z]
printf "        <color auto=\"1\"/>\n"
printf "      </right>\n"
if(fft[z]=="") printf "      <top/>\n"
else {
printf "      <top style=\"%s\">\n",fft[z]
printf "        <color auto=\"1\"/>\n"
printf "      </top>\n"
	}
printf "      <bottom style=\"%s\">\n",ffb[z]
printf "        <color auto=\"1\"/>\n"
printf "      </bottom>\n"
printf "      <diagonal/>\n"
printf "    </border>\n"
}
#for (i=1;i<nf; i++)
#{
#z=5
#if(i==1) z=4
#if(i==nf-1) z=6
#printf "    <border>\n"
#printf "      <left style=\"%s\">\n",ffl[z]
#printf "        <color auto=\"1\"/>\n"
#printf "      </left>\n"
#printf "      <right style=\"%s\">\n",ffr[z]
#printf "        <color auto=\"1\"/>\n"
#printf "      </right>\n"
#printf "      <top/>\n"
#printf "      <bottom style=\"%s\">\n",ffb[z]
#printf "        <color auto=\"1\"/>\n"
#printf "      </bottom>\n"
#printf "      <diagonal/>\n"
#printf "    </border>\n"
#}
#for (j=1;j<nf;j++)
#{
#z=8
#if(i==1) z=7
#if(i==nf-1) z=9
#printf "    <border>\n"
#printf "      <left style=\"%s\">\n",ffl[z]
#printf "        <color auto=\"1\"/>\n"
#printf "      </left>\n"
#printf "      <right style=\"%s\">\n",ffr[z]
#printf "        <color auto=\"1\"/>\n"
#printf "      </right>\n"
#printf "      <top/>\n"
#printf "      <bottom style=\"%s\">\n",ffb[z]
#printf "        <color auto=\"1\"/>\n"
#printf "      </bottom>\n"
#printf "      <diagonal/>\n"
#printf "    </border>\n"
#}
print "   </borders>"
print "  <cellStyleXfs count=\"1\">"
print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"0\"/>"
print "  </cellStyleXfs>"
if(alg && NF-3 !=alg ) cellxfs=13
else  cellxfs=10
if (cellxfs==13 && ffc) cellxfs=14
printf "  <cellXfs count=\"%d\">\n",cellxfs
#print NF,alg
print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"0\" xfId=\"0\"/>"
if (alig[1])
{
if("'"$deb"'"!="")	print "IZQ CEN"
	 print "    <xf numFmtId=\"0\" fontId=\"1\" fillId=\"2\" borderId=\"1\" xfId=\"0\" applyFont=\"1\" applyFill=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
	 print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"4\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
	 print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"7\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
}
else
{
if("'"$deb"'"!="")	print "IZQ NO CEN"
	print "    <xf numFmtId=\"0\" fontId=\"1\" fillId=\"2\" borderId=\"1\" xfId=\"0\" applyFont=\"1\" applyFill=\"1\" applyBorder=\"1\"/>"
	print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"4\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\"/>"
	print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"7\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\"/>"
}
if (alig[NF-1])
{
if("'"$deb"'"!="")	print "DER CEN"
	 print "    <xf numFmtId=\"0\" fontId=\"1\" fillId=\"2\" borderId=\"3\" xfId=\"0\" applyFont=\"1\" applyFill=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
	 print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"6\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
	 print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"9\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
}
else
{
if("'"$deb"'"!="")	print "DER NO CEN"
	print "    <xf numFmtId=\"0\" fontId=\"1\" fillId=\"2\" borderId=\"3\" xfId=\"0\" applyFont=\"1\" applyFill=\"1\" applyBorder=\"1\"/>"
	print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"6\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\"/>"
	print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"9\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\"/>"
}
if (!alg)
{
if("'"$deb"'"!="")	print "NINGUN CEN"
        print "    <xf numFmtId=\"0\" fontId=\"1\" fillId=\"2\" borderId=\"2\" xfId=\"0\" applyFont=\"1\" applyFill=\"1\" applyBorder=\"1\"/>"
        print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"5\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\"/>"
        print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"8\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\"/>"
}
if(NF-1-alig[1]-alig[NF-1]==alg)
{
if("'"$deb"'"!="")	print "TODOS CEN"
         print "    <xf numFmtId=\"0\" fontId=\"1\" fillId=\"2\" borderId=\"2\" xfId=\"0\" applyFont=\"1\" applyFill=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
         print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"5\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
         print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"8\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
}
if (NF >3 && alg)
{
if("'"$deb"'"!="")	print "CEN  NO CEN"
        print "    <xf numFmtId=\"0\" fontId=\"1\" fillId=\"2\" borderId=\"2\" xfId=\"0\" applyFont=\"1\" applyFill=\"1\" applyBorder=\"1\"/>"
        print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"5\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\"/>"
        print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"8\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\"/>"
        print "    <xf numFmtId=\"0\" fontId=\"1\" fillId=\"2\" borderId=\"2\" xfId=\"0\" applyFont=\"1\" applyFill=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
        print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"5\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\" applyAlignment=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
        print "    <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"8\" xfId=\"0\" applyFont=\"1\" applyBorder=\"1\">\n      <alignment horizontal=\"center\"/>\n    </xf>"
}


print "  </cellXfs>"
print "  <cellStyles count=\"1\">"
print "    <cellStyle name=\"Normal\" xfId=\"0\" builtinId=\"0\"/>"
print "  </cellStyles>"
print "  <dxfs count=\"0\"/>"
print "  <tableStyles count=\"0\" defaultTableStyle=\"TableStyleMedium2\" defaultPivotStyle=\"PivotStyleLight16\"/>"
print "  <extLst>"
print "    <ext xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\" uri=\"{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}\">"
print "      <x14:slicerStyles defaultSlicerStyle=\"SlicerStyleLight1\"/>"
print "    </ext>"
print "    <ext xmlns:x15=\"http://schemas.microsoft.com/office/spreadsheetml/2010/11/main\" uri=\"{9260A510-F301-46a8-8635-F512D64BE5F5}\">"
print "      <x15:timelineStyles defaultTimelineStyle=\"TimeSlicerStyleLight1\"/>"
print "    </ext>"
print "  </extLst>"
print "</styleSheet>"

} ' $SRC >XLS/expo/xl/styles.xml # |  awk ' { printf "%s",$1;for (i=2;i<=NF;i++) printf " %s",$i  } ' > XLS/expo/xl/styles.xml


awk -F^ ' BEGIN {
print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
"
z=0
}
{
	nf=NF
	for (i=1;i<nf;i++) if($i+0==0 && $i!="0" || $i+0 > 0 && substr($i,1,1)=="0") reg[z++]=$i
	for (i=1;i<nf;i++) if(length($i)> llg[i]) llg[i]=length($i)
}
END {
	for (i=1;i<nf;i++) printf "%d^",llg[i] >"XLS/LGC"
	print "" >>"XLS/LGC"
printf "<sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"%d\" uniqueCount=\"%d\">\n",z,z
	for (i=0;i<z;i++) printf "  <si>\n    <t>%s</t>\n  </si>\n",reg[i]
print "</sst>"
} ' $SRC  | awk ' { printf "%s",$1;for (i=2;i<=NF;i++) printf " %s",$i  } '  > XLS/expo/xl/sharedStrings.xml
awk -F^ ' BEGIN {
z=0
rv=0
}
{
nf=NF
for (i=1;i<nf;i++)
{
	if(substr($i,5,1)=="-" && substr($i,8,1)=="-")
	{
		if(a==$i) reg[z]=b
		else
		{
			shel=sprintf("date -d %s +%%s",$i)
			shel | getline reg[z]
			reg[z] /=86400; reg[z]+=25569; reg[z]=sprintf("%d",reg[z])
		}
		a=$i
		b=reg[z]
        }
	else reg[z]=$i
	z++
}
}
END {
print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
"
print "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" mc:Ignorable=\"x14ac\">"
printf "  <dimension ref=\"A3:%c%d\"/>\n",nf+63,NR+3
print "  <sheetViews>"
print "    <sheetView tabSelected=\"1\" workbookViewId=\"0\"/>"
print "  </sheetViews>"
print "  <sheetFormatPr baseColWidth=\"10\" defaultRowHeight=\"15\" x14ac:dyDescent=\"0.25\"/>"
print "  <cols>"
getline < "XLS/LGC"
for (i=1;i<NF;i++) 
printf "    <col min=\"%d\" max=\"%d\" width=\"%.3f\" bestFit=\"1\" customWidth=\"1\"/>\n",i,i,$i+3+i*.1
print "  </cols>"

print "  <sheetData>"
print "    <row r=\"3\" spans=\"1:"nf-1"\" ht=\"15.75\" thickBot=\"1\" x14ac:dyDescent=\"0.3\"/>"
rw=4
nr=NR
print "    <row r=\""rw"\" spans=\"1:"nf-1"\" ht=\"15.75\" thickBot=\"1\" x14ac:dyDescent=\"0.3\">"
rs=0
rg=0
vv=0
nf=NF
getline < "XLS/FRM"
alg=0
for (i=1;i<NF;i++) if($i==15 || $i==8 || $i==9) alig[i]=++alg
for (i=1;i<nf;i++)
{
	if(alig[i]) z=10
	else z=7
	if(i==1) z=1
	if(i==nf-1) z=4
	if(reg[rg]+0==0 && reg[rg]!="0" || reg[rg]+0 > 0 && substr(reg[rg],1,1)=="0")
	{

		printf "      <c r=\"%c%d\" s=\"%d\" t=\"s\">\n",i+64,rw,z
		printf "        <v>%d</v>\n",rs++
	}
	else
	{
		printf "      <c r=\"%c%d\" s=\"%d\">\n",i+64,rw,z
		printf "        <v>%s</v>\n",reg[rg]
	}
	print "      </c>"
	rg++
}
	print "    </row>"
for (j=2;j<nr;j++)
{

	print "    <row r=\""++rw"\" spans=\"1:"nf-1"\" x14ac:dyDescent=\"0.25\">"
	for (i=1;i<nf; i++)
	{
		if(alig[i]) z=11
		else z=8
		if(i==1) z=2
		if(i==nf-1) z=5
		if(reg[rg]+0==0 && reg[rg]!="0"  || reg[rg]+0 > 0 && substr(reg[rg],1,1)=="0")
		{
			printf "      <c r=\"%c%d\" s=\"%d\" t=\"s\">\n",i+64,rw,z
			printf "        <v>%d</v>\n",rs++
		}
		else
		{
			printf "      <c r=\"%c%d\" s=\"%d\">\n",i+64,rw,z
			printf "        <v>%s</v>\n",reg[rg]
		}
		print "      </c>"
		rg++
	}
	print "    </row>"
}
	print "    <row r=\""++rw"\" spans=\"1:"nf-1"\" x14ac:dyDescent=\"0.25\">"
for (i=1;i<nf;i++)
{
	if(alig[i]) z=12
	else z=9
        if(i==1) z=3
        if(i==nf-1) z=6
	if(reg[rg]+0==0 && reg[rg]!="0" ||  reg[rg]+0 > 0 && substr(reg[rg],1,1)=="0")
        {
                printf "      <c r=\"%c%d\" s=\"%d\" t=\"s\">\n",i+64,rw,z
                printf "        <v>%d</v>\n",rs++
        }
        else
        {
                printf "      <c r=\"%c%d\" s=\"%d\">\n",i+64,rw,z
                printf "        <v>%s</v>\n",reg[rg]
        }
	print "      </c>"
	rg++
}
	print "    </row>"

print "  </sheetData>"
print "  <pageMargins left=\"0.7\" right=\"0.7\" top=\"0.75\" bottom=\"0.75\" header=\"0.3\" footer=\"0.3\"/>"
print "</worksheet>"

} ' $SRC  | awk ' { printf "%s",$1;for (i=2;i<=NF;i++) printf " %s",$i  } '  > XLS/expo/xl/worksheets/sheet1.xml
ex=Xls`date +%s`.xlsx

echo $ex
cd XLS/expo
zip -rq ../../../../xls/$ex *
echo $ex |awk ' {
print "<html>"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html\">"
print "<meta name=\"GENERATOR\" content=\"Inhedo 4gt 1.0\">"
print "<meta name=\"ProgId\" content=\"Inhedo Form Generator\">"
printf "<META HTTP-EQUIV=\"refresh\" content=\"0;URL=https://xls.inhedo.com/%s\">\n",$1
print "</head>"
print "</body>"
print "</head>"
} ' 
} 2>tmp/err
cd
cat tmp/err
