proce=./tmp/proce
ihdm proce h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" d00"^" d01"^" d02"^" d03 > $proce

# |  1 H00 fileno 05 15   0 | 2 H01 barcod 03 00   5 | 3 H02 descri 60 00   8 |
# |  4 H03 unidco 02 51  68 | 5 H04 observ 256 11  70 | 6 H05 status 02 55 326 |

#  7 D00 marcas  15  00 |  8 D01 colors  15  00 |  9 D02 costos  10  01 | 
# 10 D03 obserc  50  00 | 
bases=./tmp/bases
ihdm bases h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" d00"^" d01"^" d02"^" d03"^" d04 > $bases

# |  1 H00 fileno 05 15   0 | 2 H01 barcod 03 00   5 | 3 H02 descri 40 00   8 |
# |  4 H03 unidco 02 51  48 | 5 H04 observ 256 11  50 | 6 H05 status 02 55 306 |

#  7 D00 espess  3   01 |  8 D01 marcas  15  00 |  9 D02 colors  15  00 | 
# 10 D03 costos  10  01 | 11 D04 obserc  50  00 | 
grafi=./tmp/grafi
ihdm grafi h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" d00"^" d01"^" d02"^" d03 > $grafi

# |  1 H00 fileno 05 15   0 | 2 H01 barcod 03 00   5 | 3 H02 descri 60 00   8 |
# |  4 H03 unidco 02 51  68 | 5 H04 observ 256 11  70 | 6 H05 status 02 55 326 |

#  7 D00 marcas  15  00 |  8 D01 colors  15  00 |  9 D02 costos  10  01 | 
# 10 D03 obserc  50  00 | 

mater=./tmp/mater
ihdm mater h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06 > $mater

# |  1 H00 codigo 05 01   0 | 2 H01 generi 03 00   5 | 3 H02 descri 50 00   8 |
# |  4 H03 unidad 02 51  58 | 5 H04 costos 10 01  60 | 6 H05 status 02 52  70 |

terce=./tmp/terce
ihdm terce h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06 > $terce

# |  1 H00 codigo 05 01   0 | 2 H01 generi 03 00   5 | 3 H02 descri 50 00   8 |
# |  4 H03 unidad 02 51  58 | 5 H04 costos 10 01  60 | 6 H05 status 02 52  70 |

rm MAT/*
>./data/bpygr/H
>./data/bpygr/HEAD
echo -n "    0     0">./data/bpygr/INFO

awk -F"^" '
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
$1!=ant { x=0; ant=$1 }
FILENAME== ARGV[1] && $6=="00" && $9+0 {
file="./MAT/"sprintf("G%s%.2d",$1,x)
printf "H00G%s%.2d\n",$1,x++ >file
printf "H01%s\n",trail($2) >file
printf "H02%s\n",trail($3)" "trail($7)" "trail($8) >file
printf "H03%s\n",trail($4) >file
printf "H04%s\n\n",trail($9) >file
}
FILENAME== ARGV[2] && $6=="00" && $9+0 {
file="./MAT/"sprintf("P%s%.2d",$1,x)
printf "H00P%s%.2d\n",$1,x++ >file
printf "H01%s\n",trail($2) >file
printf "H02%s\n",trail($3)" "trail($7)" "trail($8) >file
printf "H03%s\n",trail($4) >file
printf "H04%s\n\n",trail($9) >file
}
FILENAME== ARGV[3] && $6=="00" && $10+0 {
file="./MAT/"sprintf("B%s%.2d",$1,x)
printf "H00B%s%.2d\n",$1,x++ >file
printf "H01%s\n",trail($2) >file
printf "H02%s\n",trail($3)" "trail($7)"mm "trail($8)" "trail($9) >file
printf "H03%s\n",trail($4) >file
printf "H04%s\n\n",trail($10) >file
}
FILENAME== ARGV[4] && $7=="00" {
file="./MAT/"sprintf("M%s%.2d",$1,x)
printf "H00M%s%.2d\n",$1,x++ >file
printf "H01%s\n",trail($2) >file
printf "H02%s\n",trail($3) >file
printf "H03%s\n",trail($4) >file
printf "H04%s\n\n",trail($5) >file
}
FILENAME== ARGV[5] && $7=="00" {
file="./MAT/"sprintf("T%s%.2d",$1,x)
printf "H00T%s%.2d\n",$1,x++ >file
printf "H01%s\n",trail($2) >file
printf "H02%s\n",trail($3) >file
printf "H03%s\n",trail($4) >file
printf "H04%s\n\n",trail($5) >file
}
' $grafi $proce $bases $mater $terce

for i in MAT/*
do
./ihd/bin/ihmu bpygr ../../$i
done

bpygr=./tmp/bpygr
ihdm bpygr h00"^" h01"^" h02"^" h03"^" h04 > $bpygr

# |  1 H00 codigo 08 01   0 | 2 H01 generi 03 00   8 | 3 H02 descri 50 00  11 |
# |  4 H03 unidad 02 51  61 | 5 H04 costos 10 01  63 |
unids=./tmp/unids
ihdm unids h00"^" h01 > $unids

# |  1 H00 codigo 02 00   0 | 2 H01 descri 10 00   2 |

awk -F"^" ' BEGIN { printf "var unid=new Array()\nvar cost=new Array()\n" }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
FILENAME ==ARGV[1] { un[$1]=trail($2); next }
{
printf "unid[\"%s\"]=\"%s\"\n",$1,un[$4]
printf "cost[\"%s\"]=%s\n",$1,$5
}
' $unids $bpygr >../js/bpg.js
./prods.cg
