[ "$1" = "" ] && exit
BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin
export APPLDIR DATADIR PATH
#echo APPLDIR=$APPLDIR
#echo DATADIR=$DATADIR
#echo PATH=$PATH
#echo "--------------------------"
t=/tmp/T
if [ -d $APPLDIR/$1.x ]
then
	echo "NO HA REGENERADO TABLAS"
	#exit
fi
frdir=$APPLDIR/$1
mv $frdir $frdir.x
dtdir=$DATADIR/$1
[ -d $frdir ] || mkdir $frdir
[ -d $dtdir ] || {
mkdir $dtdir $dtdir/user
>$dtdir/HEAD
>$dtdir/DOC
echo -n "    0     0" >$dtdir/INFO
chmod 777 $dtdir $dtdir/*
}
file=$frdir/$1
file0=$file.0
file1=$file.1
filet0=$file.t0
filet1=$file.t1
filec0=$file.c0
>$file0
>$file1
>$filet0
>$filet1
>$filec0
ihmq forms h00"^*" h03"^" h04"^" d00"^" d01"^" d04"^" d03"^" d06"^" d07"^" d08"^"  > $t
awk -F"^" ' $1=="'"$1"'" { print }  ' $t > $t.1
awk -F"^" '
rk=="" { print $2*$3 > "'"$filet0"'"; rk=1}
toupper($4)=="H" { print $5":"$6":"$7 > "'"$filet0"'" }
toupper($4)=="D" { print $5":"$6":"$7 > "'"$filet1"'" }
' $t.1
cat $filet1
echo FIN
#mv $APPLDIR/$1 $APPLDIR/$1.x
#mkdir $APPLDIR/$1
#cp $APPLDIR/$1.x/* $APPLDIR/$1
awk -F"^" ' BEGIN {
while (getline < "'"$t.1"'" >0)
{
	nr=$2*$3
	if(toupper($4)=="H") hh++
	if(toupper($4)=="D") dd++
}
close("'"$t.1"'")
printf "%.2d %.2d %.3d 00\n",hh,nr,dd
}
toupper($4)=="H" { printf "%.2d %.2d %.3d %.2d",0,0,$6,$7 }
toupper($4)=="D" { d++;rr[d]=sprintf("%.2d %.2d %.3d %.2d",0,0,$6,$7)
printf "%s",rr[d]
} 
$7+0 >49 && $7 <60 {
	rp[$7]=FNR
	le=0
	tb=$8
	rf=$9
	ds=$10
	file="'"$APPLDIR"'/"$8"/"$8".t0"
	FS=":"
	while (getline <file >0)
	{
		if(NF==1) re=$1*6
		if(rf==$1) tk=le
		le +=$2
	}
	printf " ../%s/HEAD %d %d %d",tb,le+1+re,tk,ds
	close(file)
	FS="^"
}
{ print "" }
END {
for (i=1;i<nr;i++) for (j=1;j<=dd;j++) printf "%s\n",rr[j]
}
' $t.1 > $file
