#!/bin/sh
if [ "$1" = "" ]
then
set newform /tmp/T
fi
# disable filename globbing
{
BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
export APPLDIR DATADIR PATH
SH=$APPLDIR/sh/$1
DIR=$APPLDIR/$1
rm -r $DIR
[ -d $DIR ] || mkdir $DIR
FIL=$DIR/$1
FI0=$DIR/$1.t0
FI1=$DIR/$1.t1
FI2=$DIR/$1.t2
FIK=$DIR/$1.l
FIT=$DIR/$1.tf
>$FIK
DAT=$DATADIR/$1
>$DIR/$1.1
>$DIR/$1.0
>$DIR/$1.c0
if [ ! -d $DAT ]
then
mkdir $DAT
[ -d $DAT/user ] || mkdir $DAT/user
>$DAT/HEAD
>$DAT/DOC
>$DAT/H
echo -n "    0      0">$DAT/INFO
fi
awk -F= ' BEGIN { nf=0;nh=0;nd=0 }
/nformH/ || /nformD/ || /nformF/ { frm[$1]=trail($2) }
/nformD..00/ && $2=="F" { nf++ }
/nformD..00/ && $2=="H" { nh++ }
/nformD..00/ && $2=="D" { nd++ }
/nformD..02/ && $2!="" { D[nd]=trail($2) }
/nformD..02/ && $2!="" { F[nf]=trail($2) }
/nformD..02/ && $2!="" { H[nh]=trail($2) }

#$2!="" {print $0 "<br>"}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
END {
nl=frm["nformH04"]
np=frm["nformH05"]
printf "%.2d\n",nl*np >"'"$FI0"'"
for (i=0;i<nh;i++) printf "%s:%.3d:%.2d:%.2d:%.2d:%s:%s\n",trail(frm[sprintf("nformD%.2d%.2d",i,1)]),frm[sprintf("nformD%.2d%.2d",i,6)],frm[sprintf("nformD%.2d%.2d",i,5)],frm[sprintf("nformD%.2d%.2d",i,11)],frm[sprintf("nformD%.2d%.2d",i,10)],trail(frm[sprintf("nformD%.2d%.2d",i,8)]),trail(frm[sprintf("nformD%.2d%.2d",i,9)]) >"'"$FI0"'"
for (i=0;i<nd;i++) printf "%s:%.3d:%.2d:%.2d:%.2d:%s:%s\n",trail(frm[sprintf("nformD%.2d%.2d",nh+i,1)]),frm[sprintf("nformD%.2d%.2d",nh+i,6)],frm[sprintf("nformD%.2d%.2d",nh+i,5)],frm[sprintf("nformD%.2d%.2d",nh+i,11)],frm[sprintf("nformD%.2d%.2d",nh+i,10)],trail(frm[sprintf("nformD%.2d%.2d",nh+i,8)]),trail(frm[sprintf("nformD%.2d%.2d",nh+i,09)]) >"'"$FI1"'"
for (i=0;i<nf;i++) printf "%s:%.3d:%.2d:%.2d:%.2d:%s:%s\n",trail(frm[sprintf("nformD%.2d%.2d",nh+nd+i,1)]),frm[sprintf("nformD%.2d%.2d",nh+nd+i,6)],frm[sprintf("nformD%.2d%.2d",nh+nd+i,5)],frm[sprintf("nformD%.2d%.2d",nh+nd+i,11)],frm[sprintf("nformD%.2d%.2d",nh+nd+i,10)],trail(frm[sprintf("nformD%.2d%.2d",nh+nd+i,8)]),trail(frm[sprintf("nformD%.2d%.2d",nh+nd+i,09)]) >"'"$FI2"'"
for (i=0;i<nh;i++) if(frm[sprintf("nformD%.2d%.2d",i,5)]+0>50) printf "%s:%.3d:%.2d:%.2d:%.2d:%s:%s\n",trail(frm[sprintf("nformD%.2d%.2d",i,1)]),frm[sprintf("nformD%.2d%.2d",i,6)],frm[sprintf("nformD%.2d%.2d",i,5)],frm[sprintf("nformD%.2d%.2d",i,11)],frm[sprintf("nformD%.2d%.2d",i,10)],trail(frm[sprintf("nformD%.2d%.2d",i,8)]),trail(frm[sprintf("nformD%.2d%.2d",i,9)]) >"'"$FIK"'"
for (i=0;i<nh;i++) printf "%s\n",trail(frm[sprintf("nformD%.2d%.2d",i,2)]) >"'"$FIT"'"
for (i=0;i<nf;i++) printf "%s\n",trail(frm[sprintf("nformD%.2d%.2d",i+nh+nd,2)]) >"'"$FIT"'"
for (i=0;i<nd;i++) printf "%s\n",trail(frm[sprintf("nformD%.2d%.2d",i+nh,2)]) >"'"$FIT"'"
for (i=0;i<nf;i++) if(frm[sprintf("nformD%.2d%.2d",nh+nd+i,5)]+0 >50) printf "%s:%.3d:%.2d:%.2d:%.2d:%s:%s\n",trail(frm[sprintf("nformD%.2d%.2d",nh+nd+i,1)]),frm[sprintf("nformD%.2d%.2d",nh+nd+i,6)],frm[sprintf("nformD%.2d%.2d",nh+nd+i,5)],frm[sprintf("nformD%.2d%.2d",nh+nd+i,11)],frm[sprintf("nformD%.2d%.2d",nh+nd+i,10)],trail(frm[sprintf("nformD%.2d%.2d",nh+nd+i,8)]),trail(frm[sprintf("nformD%.2d%.2d",nh+nd+i,9)]) >"'"$FIK"'"
for (i=0;i<nd;i++) if(frm[sprintf("nformD%.2d%.2d",nh+i,5)]+0 >50) printf "%s:%.3d:%.2d:%.2d:%.2d:%s:%s\n",trail(frm[sprintf("nformD%.2d%.2d",nh+i,1)]),frm[sprintf("nformD%.2d%.2d",nh+i,6)],frm[sprintf("nformD%.2d%.2d",nh+i,5)],frm[sprintf("nformD%.2d%.2d",nh+i,11)],frm[sprintf("nformD%.2d%.2d",nh+i,10)],trail(frm[sprintf("nformD%.2d%.2d",nh+i,8)]),trail(frm[sprintf("nformD%.2d%.2d",nh+i,9)]) >"'"$FIK"'"
printf "./ihd/bin/ihmq %s h00^$1 ","'"$1"'" >"'"$SH"'"
for (i=1;i<nh;i++) printf "h%.2d^ ",i >"'"$SH"'"
for (i=0;i<nf;i++) printf "h%.2d^ ",i+nh >"'"$SH"'"
for (i=0;i<nd;i++) printf "d%.2d^ ",i >"'"$SH"'"
} ' $2



awk -F: ' {
link=$3
lnk[link]=sprintf("../%s/HEAD",$6)
fil[link]=sprintf("appl/%s/%s.t0",$6,$6)
fielt[link]=$5
field[link]=$7
j=0
while (( getline <fil[link] ) >0)
{
	if(j++==0) l[link] +=$1*6
	else
	{
		if($1==field[link]) lf[link]=ld[link]
		ld[link] +=$2
	}
}
close (fil[link])
}
END {

for (i in lnk) print i"="lnk[i]" "l[i]+ld[i]+1" "lf[i]" "fielt[i]
	
 } ' $FIK >$FIK""1

awk -F= ' FILENAME==ARGV[1] {link[$1]=$2;next } 
 (/nformH/ || /nformD/ ){ 
frm[$1]=$2
if($2=="F") nf++
if($2=="H") nh++
if($2=="D") nd++
#if($2!="") print $0 "<br>"
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
END {
nl=frm["nformH04"]
np=frm["nformH05"]

printf "%.2d %.2d %.3d %.2d\n",nh+nf,nl*np,nd,nf >"'"$FIL"'"

for (i=0;i<nh;i++)
{
	printf "%.2d %.2d %.3d %.2d",i,0,frm[sprintf("nformD%.2d%.2d",i,6)],frm[sprintf("nformD%.2d%.2d",i,5)] >"'"$FIL"'"
	if( frm[sprintf("nformD%.2d%.2d",i,5)] >50) printf " %s\n",link[frm[sprintf("nformD%.2d%.2d",i,5)]] >"'"$FIL"'"
	else printf "\n" >"'"$FIL"'"
}
for (i=0;i<nf;i++)
{
	printf "%.2d %.2d %.3d %.2d",i,0,frm[sprintf("nformD%.2d%.2d",i+nh+nd,6)],frm[sprintf("nformD%.2d%.2d",i+nh+nd,5)] >"'"$FIL"'"
	if( frm[sprintf("nformD%.2d%.2d",i+nh+nd,5)] >50) printf " %s\n",link[frm[sprintf("nformD%.2d%.2d",i+nh+nd,5)]] >"'"$FIL"'"
	else printf "\n" >"'"$FIL"'"
}
for (j=0;j<nl*np;j++)
for (i=0;i<nd;i++)
{
	printf "%.2d %.2d %.3d %.2d",j,i,frm[sprintf("nformD%.2d%.2d",nh+i,6)],frm[sprintf("nformD%.2d%.2d",nh+i,5)] >"'"$FIL"'"
	if( j==0 && frm[sprintf("nformD%.2d%.2d",nh+i,5)] >50) printf " %s\n",link[frm[sprintf("nformD%.2d%.2d",nh+i,5)]] >"'"$FIL"'"
	else printf "\n" >"'"$FIL"'"
}

} ' $FIK""1 $2


./nform.web $1 $2
} 2>./tmp/$1.err
cat ./tmp/$1.err
