#!/usr/bin/perl -w
# $Id: ftpchecker 8 2014-09-19 13:51:01Z abalama $
use strict;

=head1 NAME

ftpchecker - App::MonM checker FTP (R/RW mode)

=head1 VERSION

Version 1.01

=head1 SYNOPSIS

    ftpchecker [-w] [-u USER] [-p PASSWORD] [-d DIR] [-f VOIDFILE] HOST

    Type ftpchecker -h or ftpchecker -? for more information

=head1 DESCRIPTION

Testing FTP connection in R or RW mode. RW mode control by -w key

=head1 DEPENDENCES

L<CTK>

=head1 AUTHOR

Serz Minus (Lepenkov Sergey) L<http://www.serzik.com> E<lt>minus@mail333.comE<gt>.

=head1 COPYRIGHT

Copyright (C) 1998-2014 D&D Corporation. All Rights Reserved

=head1 LICENSE

This program is distributed under the GNU GPL v3.

This program 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.

This program 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.

See C<LICENSE> file

=cut

use CTK::Util qw/ftpgetlist ftptest/;
use Try::Tiny;
use Getopt::Long;
use Pod::Usage;

use vars qw/$VERSION/;
$VERSION = '1.01';

$SIG{INT} = sub { die "ABORTED\n"; };

$| = 1;  # autoflush

BEGIN {
    sub say { return unless -t; print STDOUT @_ ? @_ : '',"\n" }
    sub err { print STDERR @_ ? @_ : '',"\n" }
    sub tms { sprintf "[%s GMT]", scalar(gmtime(time())) }
    sub exception { say "FAILED"; err tms, " ", @_ }
}

Getopt::Long::Configure("bundling");
my %OPT;
GetOptions(\%OPT,
    "help|usage|h",
    "longhelp|man|m|?",
    "user|login|u=s",           # Login
    "password|passwd|pass|p=s", # Password
    "homedir|directory|home|dir|d=s", # HomeDir
    "voidfile|void|file|f=s",   # VoidFile
    "readwrite|rw|write|w",     # ReadWrite mode
) || pod2usage(-exitval => 1, -verbose => 0);
pod2usage(-exitval => 0, -verbose => 1) if $OPT{help};
pod2usage(-exitval => 0, -verbose => 2) if $OPT{longhelp};

my @args = @ARGV ? @ARGV : (); # Arguments
my $host        = shift(@args) || '';
my $user        = $OPT{user} || '';
my $password    = $OPT{password} || '';
my $homedir     = $OPT{homedir} || '';
my $voidfile    = $OPT{voidfile} || '';
pod2usage(-exitval => 1, -verbose => 0) unless $host;

my $err = '';
my %ftpct = (
        ftphost     => $host,
        ftpuser     => $user,
        ftppassword => $password,
        ftpdir      => $homedir,
        voidfile    => $voidfile,
        ftpattr     => { # See Net::FTP
                    Passive => 1,
                },
    );

say "App::MonM ftpchecker/$VERSION";
say;
START: say "START ", tms;
try {
    if ($OPT{readwrite}) {
      $err = "Can't connect to \"ftp://$user\@$host/$homedir\" for RW access" unless ftptest(\%ftpct);
    } else {
      my $rfiles = ftpgetlist(\%ftpct);
      unless ($rfiles && ref($rfiles) eq 'ARRAY' && @$rfiles) {
        $err = "Files not found in \"ftp://$user\@$host/$homedir\""
      }
    }
}
catch {
    $err = $_;
};
FINISH: say "FINISH ", tms;
say;

if ($err) { 
    exception($err);
    print STDOUT "FAILED" unless -t;
} else { 
    say "OK";
    print STDOUT "OK" unless -t;
}

exit 0;
__END__
