#!/usr/bin/perl
#
# Copyright 2014 - Giovanni Simoni
#
# This file is part of PFT.
#
# PFT is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# PFT is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with PFT.  If not, see <http://www.gnu.org/licenses/>.
#
=head1 NAME

pft month - Edit month pages

=head1 SYNOPSYS

pft month [options]

=head1 DESCRIPTION

Edit month pages.

=head1 OPTIONS

=over

=item --year=<y> | -y <y>

=item --month=<m> | -m <m>

=item --author=<name> | -a <name>

=item --tag=<tag> | -t <tag>

=item --help | -h

=back


=cut

use strict;
use warnings;

use Pod::Usage;
use Pod::Find qw/pod_where/;

use App::PFT;
use App::PFT::Struct::Tree;
use App::PFT::Data::Date;
use App::PFT::Struct::Conf qw/
    cfg_load
    $ROOT
    $AUTHOR
/;

use Getopt::Long qw/GetOptionsFromArray/;
Getopt::Long::Configure qw/bundling/;

my %opts;
my %datespec;
GetOptions(
    'author|a=s'    => sub { $opts{author} = $_[1] },
    'tag|t=s@'      => sub { push @{$opts{tags}}, $_[1] },
    'year|y=i'      => \$datespec{year},
    'month|m=s'     => \$datespec{month},
    'help|h!'       => sub {
        pod2usage
            -exitval => 1,
            -verbose => 2,
            -input => App::PFT::help_of 'month',
    },
) or exit 1;

cfg_load App::PFT::findroot -die => 1;

my $tree = App::PFT::Struct::Tree->new(basepath => $ROOT);
my $page = $tree->month(
    date => App::PFT::Data::Date->new(
        %datespec,
        -fill => 'now',
    ),
    -create => 1,

    # defaults
    author => $AUTHOR,
    tags => [],

    # override
    %opts,
);

$page->edit;

1;
