--- /dev/null
+#!/usr/bin/perl
+
+$tex = 0;
+$theory = 3;
+$praxis = 2;
+$usage = "Usage: mo-score2004 [--tex] task1 task2 ...";
+while (($arg = $ARGV[0]) =~ /^--([a-z]+)$/) {
+ shift @ARGV;
+ $var = "\$$1";
+ if (!eval "defined $var") { die $usage; }
+ eval "$var = 1;";
+}
+@ARGV >= $praxis || die $usage;
+
+print STDERR "Scanning contestants... ";
+open (CT, "bin/mo-get-users --full |") || die "Cannot get list of contestants";
+while (<CT>) {
+ chomp;
+ ($u,$f) = split /\t/;
+ ($u eq "somebody") && next;
+ $users{$u}=$f;
+}
+close CT;
+print STDERR 0+keys %users, "\n";
+
+print STDERR "Scanning teoretical results... ";
+if (open (EX, "teorie.txt")) {
+ while (<EX>) {
+ chomp;
+ (/^$/ || /^#/) && next;
+ @a = split /\ *\t\ */;
+ $u = shift @a;
+ defined $users{$u} || die "Unknown user $u";
+ $names{$u} = shift @a;
+ $forms{$u} = shift @a;
+ $addresses{$u} = "{". (shift @a) ."}";
+ $i=0;
+ while (@a) { $tasks{$u}{$i} = shift @a;$i++; }
+ }
+ close EX;
+ print STDERR "OK\n";
+} else {die "none\n";}
+
+print STDERR "Scanning task results... ";
+$need_tasks = join("|", @ARGV);
+foreach $u (keys %users) {
+ opendir (D, "testing/$u") or next;
+ foreach $t (readdir(D)) {
+ $t =~ /^\./ && next;
+ $t =~ /$need_tasks/ || next;
+
+ $t_num=$praxis;
+ for (my $t_num2=0;$t_num2<@ARGV;$t_num2++) {if ($t eq $ARGV[$t_num2]) {$t_num=$t_num2;}}
+ $t_num+=$theory;
+
+ $tt = "testing/$u/$t/points";
+ -f $tt || next;
+ print STDERR "$u/$t ";
+ open (X, $tt) || die "Unable to open $tt";
+ while (<X>) {
+ chomp;
+ /^\S+ (-?\d+)/ || die "Parse error: $_";
+ $tasks{$u}{$t_num} += $1;
+ }
+ close X;
+ }
+ closedir D;
+}
+print STDERR "OK\n";
+
+print STDERR "Creating table template... ";
+@body = ('','$names{$u}','$forms{$u}','$addresses{$u}');
+for ($a=0;$a<$theory+$praxis;$a++) {push @body,"\$tasks{\$u}{$a}";}
+print STDERR "OK\n";
+
+print STDERR "Filling in results... ";
+@table = ();
+foreach $u (keys %users) {
+ $row = [];
+ $row_index=0;
+ $row_sum=0;
+ foreach my $c (@body) {
+ $c =~ s/\$(\d+)/\$\$row[$1]/g;
+ $x = eval $c;
+ push @$row, (defined $x ? $x : '-');
+ if ($row_index>3) {
+ if ((defined $x) && ($x>0)) {$row_sum+=$x;}
+ }
+ $row_index++;
+ }
+ push @$row, $row_sum;
+ push @table, $row;
+}
+print STDERR "OK\n";
+
+print STDERR "Sorting... ";
+$sortcol = @{$table[0]} - 1;
+$namecol = 1;
+@table = sort {
+ my $p, $an, $bn;
+ $p = $$b[$sortcol] <=> $$a[$sortcol];
+ ($an = $$a[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
+ ($bn = $$b[$namecol]) =~ s/(\S+)\s+(\S+)/$2 $1/;
+ $p ? $p : ($an cmp $bn);
+} @table;
+$i=0;
+while ($i < @table) {
+ $j = $i;
+ while ($i < @table && ${$table[$i]}[$sortcol] == ${$table[$j]}[$sortcol]) {
+ $i++;
+ }
+ if ($i == $j+1) {
+ ${table[$j]}[0] = "$i.";
+ } else {
+ ${table[$j]}[0] = $j+1 . ".-" . $i . ".";
+ $j_old=$j;
+ $j++;
+ while ($j < $i) { ${table[$j++]}[0] = $j_old+1 . ".-" . $i . "."; };
+ }
+}
+print STDERR "OK\n";
+
+if ($tex) {
+ print
+'\language=\czech
+\font\head=csr12 scaled \magstephalf
+\font\hexx=csti12
+\font\xxit=csti10
+\def\xit{\xxit\kern-0.1em\relax}
+\let\hb=\relax
+\parindent=0pt
+\nopagenumbers
+\advance\hsize by 0.2cm
+\advance\hoffset by 1cm
+
+\centerline{\head Výsledková listina celostátního kola 53. roèníku MO kategorie P}
+\bigskip
+\centerline{\hexx 31. bøezna -- 3. dubna 2004 v Pøerovì}
+\bigskip
+\bigskip
+
+\hrule
+\bigskip
+
+\centerline{\vbox{\halign{%
+#\hfil \enspace&#\hfil\enspace & #\hfil & \quad #\hfil&
+\hfil # &
+\hfil # &
+\hfil # & \kern0.4em
+\hfil # &
+\hfil # & \kern1em
+\hb\quad\hfil # \cr
+\noalign{\bigskip\bigskip\hbox{\xit Vítìzové}\bigskip}
+%\noalign{\bigskip\bigskip\hbox{\xit Úspì¹ní øe¹itelé}\bigskip}
+%\noalign{\bigskip\bigskip\hbox{\xit Ostatní úèastníci}\bigskip}
+';
+ foreach $r (@table) { print join('&',@$r), "\\cr\n";}
+
+print '&&&
+\hphantom{10} &
+\hphantom{10} &
+\hphantom{10} &
+\hphantom{10} &
+\hphantom{10} &&
+\hphantom{10} \cr
+}}}
+
+\bye
+';
+} else {
+ foreach $r (@table) { print join("\t",@$r), "\n"; }
+}