#!/usr/bin/perl

use strict;
use warnings;

use List::Util qw/sum/;
use YAML qw/LoadFile DumpFile/;
use IO::All;
use List::Util qw/sum/;

use Getopt::Long;
use Pod::Usage;

my $man = 0;
my $help = 0;
my ($league );

GetOptions (
	"league=s" => \$league,
	'help|?' => \$help, man => \$man) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

die "League: ?" unless $league;
my $class = LoadFile "/home/$ENV{USER}/$league/league.yaml"
					or die "No league with this name: $!";
my @sessions = @{$class->{series}};
my @members = @{$class->{member}};
my %ids = map { $_->{name} => $_->{id} } @members;
my %names = map { $_->{id} => $_->{name} } @members;
my @ids = values %ids;
my @names = values %names;
my $names = join '|', @names;
my $nameqr = qr/$names/;

my $hwdir = $class->{hw} || "/home/$ENV{USER}/$league/homework";
my @hwfiles = glob "$hwdir/*.yaml";
my @rounds = map { m/^$hwdir\/(\d+)\.yaml$/ } @hwfiles;
@rounds = sort {$a<=>$b} @rounds;
my %hw = map { $_ => LoadFile "$hwdir/$_.yaml" } @rounds;

my %grades = map { 	my $id = $_;
			$id => [ map { $hw{$_}{$id} } @rounds ]
			} @ids;

my $oldtotal = LoadFile "$hwdir/cumulative.yaml";
my $newtotal;

#format STDOUT_TOP = 
#                            Homework
#Name        ID          Weeks                 Total HomeWork Grade
#-------------------------------------------------------------------------
#.

open REP, '>-' or die 'STDOUT? $!'; 
# my @romans = qw/'' I II III IV V VI VII VIII IX X XI XII XIII XIV XV XVI XVII XVIII/;
my @romans = qw/'' 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18/;

print REP
"             $class->{league} Homework
                    Weeks: Grade(0, 0,5, 1)
Name        ID  @romans[@rounds]       Total Homework
-------------------------------------------------------------------------
";
foreach my $id ( sort values %ids ) 
{
	my $name = $names{$id};
	my @indgrades = @{$grades{$id}};
	@indgrades = map { $_ == 0.5? substr $_, 1: $_ } @indgrades;
	die "No homework grade for $name $id" if grep {not defined} @indgrades;
	my $grade = sum @indgrades;
	$newtotal->{$id} = $grade;
	no warnings;
	format STDOUT =
@<<<<<< @<<<<<<< @<<@<<@<<@<<@<<@<<@<<@<<@<<@<<@<<@<<@<<   @<<<<<
$name,   $id,	@indgrades, $grade
.
	write;
	use warnings;
	# $grades->{$partner} = $newGrade;
} 


my $log = io "$hwdir/hwtotal.log";
localtime() . ": $0 run to calculate homework totals at round $rounds[-1].\n"
									>> $log;
DumpFile ("$hwdir/cumulative.yaml.bak", $oldtotal);
DumpFile ("$hwdir/cumulative.yaml", $newtotal);

__END__

=head1 NAME

cumulative - Add player results in individual rounds to get a cumulative total and show present standing

=head1 SYNOPSIS

hwtotal [options] 

Options:

--help            This help message

--man            A man page

--league m/j	The league whose results these are

=head1 OPTIONS

=over 8

=item B<-league>

The league to which the redeemer belongs

=back

=head1 DESCRIPTION

B<hwtotal> tallies individuals' scores in the files in the hw directory recorded in league.yaml. It stores the total in cumulative.yaml, in the same directory.

=cut

