#!/usr/bin/perl

# all the nice modules
use strict;
use warnings;
use Log::Log4perl qw( :easy );
use Getopt::Long qw( :config no_ignore_case bundling auto_version);
use Pod::Usage;
use Games::Tournament::TableTennis qw( :all );

our $VERSION = '0.02';

# set up all the nice options
pod2usage(2) unless @ARGV;  # no options are no good
my %Options;
GetOptions(
        's'      => \$Options{s},
        'a'      => \$Options{a},
        'p'      => \$Options{p},
        'h|help' => \$Options{h},
        'r:s'    => \$Options{r},
        'g:s'    => \$Options{g},
        'n:s'    => \$Options{n},
        'd:s'    => \$Options{d},
        'P:s'    => \$Options{P},
) or pod2usage(2);

# initialize the logger or not
if (defined $Options{d}) {
    my %loglevels = (
            1 => 5000,
            2 => 10000,
            3 => 20000,
            4 => 30000,
            5 => 40000,
            6 => 50000,
            );

    Log::Log4perl->easy_init( {
        level  => $loglevels{$Options{d} || 1},
        layout => '%d: [%L] %m %n',
        } );

    # little debbugging help
    for my $option (keys %Options) {
        DEBUG "Set option $option to $Options{$option}." # my favourite
            if defined $Options{$option};                # part
    }
}

# invoke the subroutines corresponding to the options
{
my @arguments = (\%Options, \@ARGV);
if (defined $Options{n}) {
    INFO "Invoking the subroutine creating a new tournament.";
    create_new_tournament(@arguments);
} elsif (defined $Options{p}) {
    INFO "Invoking the subroutine processing the group files.";
    process_group_files(@arguments);
} elsif (defined $Options{a}) {
    INFO "Invoking the subroutine preprocessing the game files.";
    preprocess_game_files(@arguments);
} elsif (defined $Options{h}) {
    pod2usage( { -exitval => 1,
                 -verbose => 2,
                 -output  => \*STDOUT,
               } );
}
}


__END__

=encoding utf8

=head1 NAME

myrtscht - My Round robin Tournament SCHeduling for Table tennis



=head1 SYNOPSIS

myrtscht B<-n> [I<name>] [B<-g> I<number>] [B<-d> [I<file>]]

myrtscht B<-p> I<files> [B<-r> I<rounds>] [B<-s>] [B<-P> I<printer>] [B<-d> [I<file>]]

myrtscht B<-a> I<files>/I<directory> [B<-d> [I<file>]]

myrtscht B<--help>

myrtscht B<-h>

myrtscht B<--version>



=head1 DESCRIPTION

B<myrtscht> is a program which helps you managing table tennis
tournaments using the round robin system.  But I think, you can also
use it for other games which do not allow standoffs.  It is completely
text and command based, so there is neither a nice command line nor a
GUI, my apologies for this (if this needs to be apologised).
Therefore it works, as you can see in the synopsis, with running
commands and editing text files.



=head1 OPTIONS AND ARGUMENTS

=over

=item B<-n> [I<name>]

Creates a new directory used for the tournament with the group files
in it.  You can also give the directory a I<name>, which has no
effects on the group files, the default is F<< Tournament-<date> >>.

=item B<-g> I<number>

Defines the I<number> of group files created when running with B<-n>,
the default is 10.

=item B<-p> I<files>

Creates a group directory with the game files in it from each of the
group I<files> given and prints out some nice formatted tables on
sheets of paper where you can write down the results of the games.

=item B<-r> I<sets>

Defines the number of I<sets> to win for winning the whole game, the
default is 2.  This is senseful when running with B<-p>.

=item B<-s>

Disables printing when running with B<-p>.

=item B<-P> I<printer>

Defines the I<printer> used when running with B<-p>, the default is the
standard printer.

=item B<-a> I<files>/I<directory>

Computes a ranking file from the given group I<files> or I<directory>.

=item B<-d> [I<file>]

Switches to debugging mode.  The log is created by using the
L<Log::Log4perl(3)|Log::Log4perl> module.  You can specify a config 
I<file> to modify the output.

=item B<-h> or B<--help>

Print this help.

=item B<--version>

Print the version and exit.

=back



=head1 FILES

B<myrtscht> creates different files in its process of working.  They all
have either a special extension or a special name.  These may show up:

=head2 The Tournament Directory

The tournament directory is the directory all information for on
tournament are stored in.  It is recommended to stay in this or
subordinated directories when working with B<myrtscht>.


=head2 The Group Files F<*.g>

These are the files you are going to see first when initialising a
tournament.  Their format (before editing) is:

    Name:
    Tables:
    Members:

Z<> Behind S<"Name: "> you have to write down the name of the group.
Behind S<"Tables: "> the names (numbers are recommended) of the tables
the group plays at stand in a comma separated list. (Yes there are
also other separators possible.  Look in the source code if
you want them implicitly!)  I<Under> "Members:" you have to
write the names of the members of the group.

=head3 Example:

    Name: 50 before Christ
    Tables: 1, 2, 3
    Members:
    Asterix
    Obelix
    Gaius Iulius Caesar
    Gaius Bonus

=head3 Note:

Names with more than 20 letters are unbearable.  (Maybe also the
parents of the persons with this names.)


=head2 The Group Directories F<*.d>

Group directories are created when running B<myrtscht> with the option
B<-p>.  They are named like the group files they are belonging to
(apart from the extension).  All things belonging to one group should
be done within the particular directory, thus the group file is moved
into the directory when running with B<-p>.


=head2 The Game Files F<*.s>

These are the files being in a group directory after running with B<-p>.
They look like this (before editing):

    Asterix | Gaius Iulius Caesar

There you can write down the results of a match.

=head3 Example:

    Asterix | Gaius Iulius Caesar
    11  8
    14  16
    11  9

=head3 Note:

It is recommended to use just whitespace between the numbers.


=head2 The Printout/F<gamesheets>

When running B<myrtscht> with the option B<-p> it creates the file
F<gamesheets>, sends it to the printer and then removes it.  If B<-s> is
defined, there will be no printing and no removing.  In both cases
the parts of the printout will have this format (here just an example):
 
    Group: 50 before Christ
    Round: 2     Table: 3              ID: 0303
    ----------------------+---------------------
           Asterix        | Gaius Iulius Caesar
    ----------------------+---------------------
                          |
                          |
                          |
    ----------------------+---------------------

Each of this parts represents one game.  Behind S<"Group: "> is the
name of the group situated.  S<"Round: "> is the round they play in,
S<"Table: "> the name of the table they play at.  The "ID" is
formed out of the names of the group directory (first two digits)
and the game file (last two or three digits).  Hence you can
easily identify the game file belonging to the particular game
sheet.  (This is also demonstrated in L<.)  Then the names of
the opponents come and in the space below the referee can write
down the result of the match.  The height of this space varies
with the number of rounds to play to win a match.


=head2 The Ranking File F<ranking>

Here the results of a group are written down.  You can see an example
down below, but in order to work also with high scores it is a bit wide.

    Rank | Name                  |  Games  |   Sets    |  SD  |    Balls    |  BD
    -----+-----------------------+---------+-----------+------+-------------+------
      1. | Asterix               |  3 : 0  |   6 : 1   |  5   |   81 : 65   |  16
      2. | Gaius Iulius Caesar   |  2 : 1  |   5 : 2   |  3   |   77 : 52   |  25
      3. | Obelix                |  1 : 2  |   2 : 4   |  -2  |   54 : 56   |  -2
      4. | Gaius Bonus           |  0 : 3  |   0 : 6   |  -6  |   28 : 67   |  -39

"Rank" and "Name" are unambiguous.  "Games", "Sets" and "Balls"
represent the played games, sets and balls respectively in the format
"won : lost".  SD and BD represent the difference between won and lost
sets balls respectively.



=head1 NOTES

=over

=item *

B<myrtscht> will not check if the input data are valid and go along with
the rules of the game.

=item *

B<myrtscht> will not check if the given command line options are
senseful.

=item *

It is required to have a printout with 67 lines per page.  In later
version, when B<myrtscht> makes use of configuration files, there will
be easy support for other numbers.  Now you can change the code, too,
though.---That should not be such difficult.

=item *

If there are some mistakes in my grammar or spelling---please excuse and
write me!

=item *

If anybody does not my like my way to document---please write me.

=item *

My name is written with "o umlaut", but perldoc(1) doesn't like the
correct spelling.  If you want to see the correct spelling use
S<C<pod2man --utf8 myrtscht | nroff -Tlatin1 -man | less>> instead of
running with B<-h> or B<--help>!

=back



=head1 BUGS

When printing letters with umlaut in formats the layout gets a bit
crazy, because the umlauts are counted like two but printed like one
char.---I think, this is a common problem, but haven't found a
solution.  Maybe anyone reading this will give me a hint.


=head1 SEE ALSO

=over 4

=item L<Games::Tournament::TableTennis>

The module on which this program is based.

=item L<Games::Tournament::MyrtschtTutEn>

A tutorial to this program.

=item L<www.myrtscht.de|http://www.myrtscht.de>

A website concerning this program. Not really mature yet.

=item L<git://git.tuxfamily.org/gitroot/myrtscht/programrel.git>

The git repository with the current state of development. Use it by
typing

    git clone git://git.tuxfamily.org/gitroot/myrtscht/programrel.git

But beware: It is NOT stable.


=head1 AUTHOR

Richard Möhn E<lt>myrscht@cpan.orgE<gt>



=head1 COPYRIGHT AND LICENSE

Copyright 2008 by Richard Möhn E<lt>myrscht@cpan.orgE<gt>

This program is free software.  You may copy or redistribute it under
the same terms as Perl itself.

=cut
