#!/bin/sh

# disable filename globbing

{
BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
export APPLDIR DATADIR PATH


#set newform /tmp/T1010047
DIR=appl/$1
[ -d $DIR ] || mkdir $DIR
FIL=$DIR/$1
FI0=$DIR/$1.t0
FI1=$DIR/$1.t1
FIK=$DIR/$1.l

awk -F= '
 (/nformH/ || /nformD/){ 
frm[$1]=$2
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\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<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<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"'"
} ' $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]
	
 } ' $FIK >$FIK""1

awk -F= ' FILENAME==ARGV[1] {link[$1]=$2;next } 
 (/nformH/ || /nformD/){ 
frm[$1]=$2
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,nl*np,nd,0 >"'"$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 (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
