#!/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 tag - Edit a tag page

=head1 SYNOPSYS

pft tag [options]

=head1 DESCRIPTION

Edit a tag page.

=head1 OPTIONS

=over

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

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

=item --help | -h

=back


=cut

use strict;
use warnings;

use feature qw/say/;

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] },
    'editor=s'      => \$opts{editor},
    'help|h!'       => sub {
        pod2usage
            -exitval => 1,
            -verbose => 2,
            -input => App::PFT::help_of 'tag',
    },
);

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

unless (@ARGV) {
    say STDERR "Usage: $App::PFT::Name tag [options] <title>";
    exit 1;
}

my $tree = App::PFT::Struct::Tree->new(basepath => $ROOT);
my $page = $tree->tag(
    name => join(' ', @ARGV),
    -create => 1,

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

    # override
    %opts,
);

$page->edit($opts{editor});
