#!/usr/bin/perl -w
# count questions and points for mc and open questions.
# The script makes specific assumptions about the
# directory layout of a project, fitting the fontys venlo way of working.
# %% <CourseDir>
# %% ├── builds
# %% │   ├── <ExamInstance>, e.g. 20120628
# %% │
# %% └── questions, and resources e.g.
# %%     ├── figures
# %%     ├── q1h01

my ($quest,$opencount,$openpoints,$mccount,$mcpoints);

sub countCat($$$);
my $defpoints=2;
countCat("questions.tex",'mc',$defpoints);
my $defpoints=5;
countCat("openquestions.tex",'mc',$defpoints);
exit 0;

sub countCat($$$) {
  my ($qfile,$cat) = @_;
  if (open(QUESTIONS,"<$qfile")) {
    while (<QUESTIONS>) {
      chomp;
      s/\\element{general}{//;
      s/}}//;
      s/\\inputQ{/..\/..\/questions\//;
      $quest=$_;
      if (open(QUEST,"<$quest")) {
	$delta = $defpoints;
	while (<QUEST>) {
	  if (m/\%\%maxpoints=(\d+)/) {
	    $delta =$1;
	  }
	}
	$mccount++;
	$mcpoints +=$delta;
	close(QUEST);
      } else {
	print STDERR "WARNING: cannot open question file $quest\n";
      }
    }
    close(QUESTIONS);
    print 
      qq(\\def\\${cat}count{$mccount}\n).
      qq(\\def\\${cat}count{$mcpoints}\n);
  } else {
    print STDERR "WARNING: could not open file $qfile\n";
  }
} # end of countCat.
