#!/usr/bin/perl
#
# Copyright 2014-2016 - 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/>.

=encoding utf8

=head1 NAME

pft init - Initialize a PFT Site

=head1 SYNOPSIS

B<pft init> [I<options>]

=head1 DESCRIPTION

This command initializes a PFT site in the current directory. It generates a
configuration file named C<pft.yaml> and it creates the following filesystem
structure (see L<pft(1)>).

A generic configuration skeleton is provided by default, and can be modified
by editing the C<pft.yaml> configuration file.  Each option explained in
this manual page corresponds to an equally named setting in the C<pft.yaml>
file.

=cut
# NOTE: Documentation continues with auto-generated options from PFT::Conf
# at the end of this file (from "=head1 OPTIONS" to the subsequent
# "# END # AUTOGEN" comment).
#
# Generate again if needed with:
#
#   perl -CO -MPFT::Conf=pod_autogen -wE 'say pod_autogen'
#

use strict;
use warnings;
use utf8;
use v5.16;
use feature 'say';

use PFT::Conf;
use PFT::Tree;
use PFT::Header;

use Encode;
use Encode::Locale;

use File::Spec;
use File::Copy;
use File::ShareDir;
use File::Basename qw/basename/;

use Cwd;

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

use Pod::Usage;
use App::PFT;

my %opts = (home => 1);
GetOptions(
    PFT::Conf::wire_getopt(\my %conf_opts),

    'help|h!' => sub {
        pod2usage
            -exitval => 0,
            -verbose => 2,
            -input => App::PFT::help_of 'init',
    },
) or exit 1;

if (my $root = PFT::Conf::locate) {
    say STDERR 'Configuration exists: ', $root;
    exit 1;
}

my $conf = PFT::Conf->new_getopt(\%conf_opts);
$conf->save_to(my $root = Cwd::cwd);
my $tree = PFT::Tree->new($root, {create => 1});

my $home = $tree->content->new_entry(PFT::Header->new(
    title => $conf->{site}{home},
    author => $conf->{site}{author},
));
$home->open('a') unless $home->exists;

my $glob = File::Spec->catdir(
    File::ShareDir::module_dir('App::PFT'),
    'templates',
    '*',
);

foreach (glob encode(locale_fs => $glob)) {
    my $outfn = File::Spec->catfile(
        encode(locale_fs => $tree->dir_templates),
        basename($_)
    );

    open my $in, '< :encoding(UTF-8)', $_ or do {
        say STDERR 'Cannot open ', decode(locale_fs => $_), ": $!";
        exit 1;
    };

    open my $out, '> :encoding(locale_fs)', $outfn or do {
        say STDERR 'Cannot open ', decode(locale_fs => $outfn), ": $!";
        exit 1;
    };

    print $out (<$in>);

    close $in;
    close $out;
};

=head1 CONFIGURATION OPTIONS

=over

=item B<--publish-host>=I<HOST>

Remote host where to publish (see L<pft-pub(1)>).
Defaults to C<example.org>.

=item B<--publish-method>=I<NAME>

Method used for publishing (see L<pft-pub(1)>).
Defaults to C<rsync+ssh>.

=item B<--publish-path>=I<PATH>

Remote path on publishing host (see L<pft-pub(1)>).
Defaults to C</home/$USER/public_html>, as by tradition.

=item B<--publish-port>=I<PORT>

Port for connection on publishing host (see L<pft-pub(1)>).
Defaults to C<22>.

=item B<--publish-user>=I<USER>

User login on publishing host (see L<pft-pub(1)>).
Defaults to $USER (environment variable).

=item B<--site-author>=I<USER>

Global Author, can be overriden by individual entries.
Defaults to C<$USER> (environment variable).

=item B<--site-encoding>=I<ENC>

Charset of the generated web pages.
Defaults to what is defined by L<locale(1)>.

=item B<--site-feed-description>=I<DESC>

Description of the channel (C<E<lt>descriptionE<gt>> in the XML).
Defaults to C<News from a PFT website>.

=item B<--site-feed-length>=I<N>

Number of most recent blog entries to list in the RSS feed.
Defaults to C<10>.

=item B<--site-feed-path>=I<PATH>

File name of the RSS XML to be published by L<pft-gen-rss(1)>.
Defaults to C<feed.rss>.

=item B<--site-home>=I<PAGE_NAME>

First page, where C<index.html> will redirect the browsers.
Defaults to C<Welcome>.

=item B<--site-template>=I<TEMPLATE>

Global HTML template, can be overriden by individual entires.
Defaults to C<default.html>.

=item B<--site-theme>=I<THEME>

Global theme (e.g. C<light> or C<dark>) optionally honored by templates. Specific accepted values depend on the template implementation.
Defaults to C<light>.

=item B<--site-title>=I<TITLE>

Title of the website.
Defaults to C<My PFT website>.

=item B<--site-url>=I<URL>

Base url for the website.
Defaults to C<http://example.org>.

=item B<--system-browser>=I<BROWSER>

Browser to be invoked by B<pft-show(1)>. You may specify an executable, or a L<sh(1)> command where "%s" gets replaced with the file name (e.g. "firefox -profile x '%s'").
Defaults to C<$BROWSER> (environment variable), or C<firefox> if not defined.

=item B<--system-editor>=I<EDITOR>

Editor to be invoked by L<pft-edit(1)>. You may specify an executable, or a L<sh(1)> command where "%s" gets replaced with the file name (e.g. "vim +'set filetype=markdown spell' %s").
Defaults to C<$EDITOR> (environment variable), or C<vi> if not defined.

=back

# END AUTOGEN

=head1 ENVIRONMENT

The following environment variables are honored as sensible defaults unless
some different setting is supplied by command line arguments.

=over

=item C<$USER>

=item C<$BROWSER>

=item C<$EDITOR>

=back

=head1 SEE ALSO

L<pft>, L<pft-pub(1)>
