#!perl

use 5.014;
use strict;
use warnings;

use Getopt::Long ();
use Game::Fanorona;
use Game::Fanorona::Terminal;

my %given;
my $parsed = Getopt::Long::GetOptionsFromArray(
    \@ARGV, \%given,
    'level=i', 'side=s', 'variant=s', 'load=s',
    'colour|color!', 'pick!', 'unicode!', 'version', 'help',
);

if (!$parsed || @ARGV) {
    print STDERR "fanorona: I do not know '@ARGV'.\n" if $parsed && @ARGV;
    print STDERR usage();
    exit 2;
}
if ($given{help}) {
    print usage();
    exit 0;
}
if ($given{version}) {
    print "fanorona $Game::Fanorona::VERSION\n";
    exit 0;
}

my %with;
$with{level}   = $given{level}   if defined $given{level};
$with{human}   = $given{side}    if defined $given{side};
$with{variant} = $given{variant} if defined $given{variant};
$with{colour}  = $given{colour}  if defined $given{colour};
$with{picking} = $given{pick}    if defined $given{pick};
$with{unicode} = $given{unicode} if defined $given{unicode};
$with{menu}    = 1 unless grep { defined $given{$_} } qw(side level variant load);

my $terminal = eval { Game::Fanorona::Terminal->new(%with) };
if (!$terminal) {
    (my $why = $@) =~ s/ at \S+ line \d+\.?\s*\z//;
    $why =~ s/\AGame::Fanorona(?:::\w+)*: //;
    print STDERR "fanorona: $why.\n";
    exit 2;
}

if (defined $given{load}) {
    exit 2 unless $terminal->load($given{load});
}

exit $terminal->start;

sub usage {
    return <<'USAGE';
fanorona - the board game of Madagascar, in a terminal

  fanorona [options]

  --side WHO       white (the default, and White moves first), black, both, or neither
  --level N        how hard the program plays: 1 to 5, 3 by default
  --variant NAME   the board: sivy (nine by five, the default), dimy or telo
  --load FILE      carry on with a saved game
  --no-colour      letters and lines instead of a painted board
  --no-pick        type moves instead of choosing them with the arrow keys
  --no-unicode     letters for the stones, with colour kept
  --version        the version, and stop
  --help           this, and stop

In a terminal, moves are chosen on the board with the arrow keys; press ? for
the keys. Anywhere else, type a move as the points the stone stands on, like
d2-e3A, or help.
USAGE
}

__END__

=head1 NAME

fanorona - the board game of Madagascar, in a terminal

=head1 SYNOPSIS

    fanorona
    fanorona --side black --level 5
    fanorona --side both --no-colour
    fanorona --variant telo
    fanorona --load saved.txt

=head1 DESCRIPTION

Plays fanorona against the program, between two people at one keyboard, or the
program against itself. See L<Game::Fanorona::Terminal> for how it is played
and L<Game::Fanorona> for the rules.

Given none of C<--side>, C<--level>, C<--variant> and C<--load>, and a terminal
whose keys it can read, it begins by asking for the three.

=head1 OPTIONS

=over 4

=item B<--side> I<who>

Which side is yours: C<white> (the default; White moves first), C<black>,
C<both> for two people at one keyboard, or C<neither> to watch.

=item B<--level> I<n>

1 to 5. 3 by default. Level 1 takes the most stones it can and looks no
further; each level above it looks at more positions before it moves.

=item B<--variant> I<name>

The board: C<sivy>, nine points by five and the game most people mean;
C<dimy>, five by five; or C<telo>, three by three. Anything
L<Game::Fanorona::Variant/from_string> reads is accepted.

=item B<--load> I<file>

Carry on with a game written by the C<save> command.

=item B<--no-colour>

Draw the board in letters and lines. C<--no-color> is the same, and so is
setting C<NO_COLOR> in the environment.

=item B<--no-pick>

Type moves instead of choosing them with the arrow keys.

=item B<--no-unicode>

Letters for the stones, keeping the colour.

=item B<--version>, B<--help>

Say so and stop.

=back

There is no C<--seed>: the game has no chance in it.

=head1 EXIT STATUS

0 when the game was left in the ordinary way, 2 when the options were wrong or
a file could not be loaded.

=head1 AUTHOR

LNATION <email@lnation.org>

=head1 LICENSE AND COPYRIGHT

This software is Copyright (c) 2026 by LNATION <email@lnation.org>.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

=cut
