BEGIN {
    coded  = 1;
    built  = 2;
    tested = 3;

    fError = 0;     # no error
}

/^[ \t]*$/ {	# skip blank lines
    next;
}

/^#/ {		# skip comment lines
    next;
}

/^COMPONENT/ {	# start of component table
    iTable = "COMP";
    next;
}

/^WEEK/ {	# start of a weekly table
    iTable = "WEEK";
    iw = $2;	     # remember week index
    week[iw] = $3;   # remember date
    iwLast = iwNow;  # track last two weeks
    iwNow = iw;
    next;
}

{		# process a data line
    if (iTable == "COMP") {
	sortACComp[$1 $2] = $2; # sort by author,component -> comp
	compAuthor[$2] = $1;	# save component author
	compDesc[$2] = substr($0,index($0,$3)); # save component description
    } else if (iTable == "WEEK") {
	if ($1 in compAuthor) {
	    data[iw][$1][coded]  = $2+0;
	    data[iw][$1][built]  = $3+0;
	    data[iw][$1][tested] = $4+0;
	}
	else {
	    print "unknown component in WEEK " iw ": " $1;
	    fError=1;
	    exit;
	}
    }
    else {
	print "unexpected data record:" $0;
	fError=1;
	exit;
    }
}

END {
    if (fError)
	exit;

    print "MVDM Progress from " week[iwLast] " to " week[iwNow];
    print "";

	print	    "                     D E L T A";
	print	    "#Component  %written %compiled %tested";
	print	    "#---------- -------- --------- -------";
	for (iac in sortACComp) {
	    ic = sortACComp[iac];
	    printf ("%-12s %8d %8d %7d\n",
		ic,
		data[iwNow][ic][coded]	- data[iwLast][ic][coded],
		data[iwNow][ic][built]	- data[iwLast][ic][built],
		data[iwNow][ic][tested] - data[iwLast][ic][tested]);
	}

    print "";
    print "";
    print "MVDM Distance from Completion";
    print "";

	print	    "                  A B S O L U T E";
	print	    "#Component  %written %compiled %tested";
	print	    "#---------- -------- --------- -------";
	for (iac in sortACComp) {
	    ic = sortACComp[iac];
	    printf ("%-12s %8d %8d %7d\n",
		ic,
		100 - data[iwNow][ic][coded],
		100 - data[iwNow][ic][built],
		100 - data[iwNow][ic][tested]);
	}

    print "";
    print "";
    print "*************";
    print "Detailed Data";
    print "*************";
    print "";
    print "COMPONENT";
    print	"#author    component description";
    print	"#------    --------- -----------";
    for (iac in sortACComp) {
	ic = sortACComp[iac];
	printf ("%-9s %-12s %s\n",compAuthor[ic],ic,compDesc[ic]);
    }

    for (iw in week) {
	print	    "";
	print	    "WEEK " iw " " week[iw];
	print	    "#Component  %written %compiled %tested";
	print	    "#---------- -------- --------- -------";
	for (iac in sortACComp) {
	    ic = sortACComp[iac];
	    printf ("%-12s %8d %8d %7d\n",
		ic,
		data[iw][ic][coded],
		data[iw][ic][built],
		data[iw][ic][tested]);
	}
    }
}
