#!/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 pub - Publish content

=head1 SYNOPSYS

pft pub

=head1 DESCRIPTION

Publish content

=cut

use strict;
use warnings;

use feature qw/say state/;

use App::PFT;
use App::PFT::Struct::Tree;
use App::PFT::Struct::Conf qw/
    cfg_load
    $ROOT
    %REMOTE
/;

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

use File::Spec::Functions qw/catfile catdir splitdir/;
use File::Path qw/make_path remove_tree/;
use File::Copy::Recursive;

use Carp;

use Getopt::Long;
Getopt::Long::Configure 'bundling';

sub check {
    foreach (@_) {
        croak "Cannot use $REMOTE{Method}: missing Remote.$_ in pft.yaml"
            unless defined $REMOTE{$_}
    }
}

sub rsync_ssh {
    my $tree = shift;

    check qw/User Host Path/;

    my $src = catfile($tree->dir_build, '');
    my $dst = "$REMOTE{User}\@$REMOTE{Host}:$REMOTE{Path}";
    my $port = $REMOTE{Port} || 22;

    local $, = "\n\t";
    say STDERR 'Sending with RSync', "from $src", "to $dst";


    system('rsync',
        '-e', "ssh -p $port",
        '--recursive',
        '--verbose',
        '--copy-links',
        '--times',
        '--delete',
        '--human-readable',
        '--progress',
        $src, $dst,
    );
}

sub install {
    my $tree = shift;
    check qw/Path/;

    state $relstep = do {
        my $sup = File::Spec::Functions::updir;
        my $cur = File::Spec::Functions::curdir;
        qr/^(?:\Q$sup\E|\Q$cur\E)$/;
    };

    my $dst = (splitdir $REMOTE{Path})[0] =~ $relstep
        ? catdir($tree->basepath, $REMOTE{Path})
        : $REMOTE{Path}
        ;

    remove_tree $dst;
    make_path $dst, { verbose => 1 };

    local $File::Copy::Recursive::CopyLink = 0;
    File::Copy::Recursive::rcopy_glob(
        catfile($tree->dir_build, '*'),
        $dst,
    );
}

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

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

my $method = {
    'rsync+ssh' => \&rsync_ssh,
    'install'   => \&install,
}->{$REMOTE{Method}};
die 'Unknown method ', $REMOTE{Method} unless $method;

my $tree = App::PFT::Struct::Tree->new( basepath => $ROOT);
$method->($tree);
