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
NHEAD=$DATADIR/$1/NHEAD
NDOC=$DATADIR/$1/NDOC
HEAD=$DATADIR/$1/HEAD
DOC=$DATADIR/$1/DOC
file=$APPLDIR/$1/$1
filet0=$file.t0
filet1=$file.t1
file=$APPLDIR/$1.x/$1
filen0=$file.t0
filen1=$file.t1
awk -F: '
FILENAME== ARGV[1] && NF==1 { row=$1 ;next}
FILENAME== ARGV[3] && NF==1 { nrow=$1 ;next}
FILENAME == ARGV[1] {
++z
h0[z] =$1
lh0[z] =$2
lh1[z] =lh
lh +=$2
next
}
FILENAME == ARGV[2] {
++w
d0[w] =$1
ld0[w] =$2
ld1[w] =ld
ld +=$2
next
}
FILENAME == ARGV[3] {
++x
nh0[x] =$1
nlh0[x] =sprintf("%%-%d.%ds",$2,$2)
print x,$1,nlh0[x]
next
}
FILENAME == ARGV[4] {
++y
nd0[y] =$1
nld0[y] =sprintf("%%-%d.%ds",$2,$2)
nld0[y] =sprintf("%%-%d.%ds",$2,$2)
print y,$1,nld0[y]
next
}
FILENAME == ARGV[5] {
for (i=1;i<=z;i++) head[h0[i]]=substr($0,lh1[i]+1,lh0[i])
rsth=substr($0,lh+1)
for (i=1;i<=y;i++) printf nlh0[i],head[nh0[i]] >"'"$NHEAD"'"
if (nrow+0 > row+0)
{
	printf "%s",rsth >"'"$NHEAD"'"
	for (j=row;j<nrow;i++) printf "-00001" >"'"$NHEAD"'"
}
else if (nrow+0 < row+0) printf substr(rsth,1,nrow*6) >"'"$NHEAD"'"
else printf "%s",rsth >"'"$NHEAD"'"
print "" >"'"$NHEAD"'"
}
FILENAME == ARGV[6] {
for (i=1;i<=w;i++) head[d0[i]]=substr($0,ld1[i]+1,ld0[i])
rstd=substr($0,ld+1)
for (i=1;i<=y;i++) printf nld0[i],head[nd0[i]] >"'"$NDOC"'"
printf "%s",rstd >"'"$NDOC"'"
print "" >"'"$NDOC"'"
}


 ' $filen0 $filen1 $filet0 $filet1 $HEAD $DOC

cat $NHEAD > $HEAD
cat $NDOC > $DOC
