#!/usr/bin/perl -w
use strict; # $Id: monm_whois 87 2019-07-14 15:36:48Z abalama $
use utf8;

=encoding utf8

=head1 NAME

monm_whois - WHOIS checker for App::MonM

=head1 VERSION

Version 1.00

=head1 SYNOPSIS

    monm_whois [-d DAYS] HOST

=head1 OPTIONS

=over 4

=item B<-h, --help>

Show short help information and quit

=item B<-H, --longhelp>

Show long help information and quit

=item B<-d DAYS, --days=DAYS>

Alert days

Default: 14

=item B<-w /path/to/whois, --whois=/path/to/whois>

Path to whois program

Default: whois

=back

=head1 DESCRIPTION

WHOIS checker for App::MonM

=head1 AUTHOR

Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>

=head1 COPYRIGHT

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

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See L<https://dev.perl.org/licenses/>

=cut

use Getopt::Long;
use Pod::Usage;

use Time::Local;
use List::Util qw/ max /;
use CTK::Util qw/ execute trim /;

use App::MonM::Const qw/
        OK DONE ERROR SKIPPED PASSED FAILED UNKNOWN PROBLEM
    /;

use constant {
    DAYS    => 14,
    COMMAND => "whois",
};

$| = 1;  # autoflush

my $options = {};
Getopt::Long::Configure("bundling");
GetOptions($options,
    # Information
    "help|usage|h",         # Show help page
    "longhelp|H|?",         # Show long help page

    # General
    "days|day|d=i",         # Days
    "whois|w=s",            # The whois command

) || pod2usage(-exitval => 1, -verbose => 0, -output => \*STDERR);
pod2usage(-exitval => 0, -verbose => 1) if $options->{help};
pod2usage(-exitval => 0, -verbose => 2) if $options->{longhelp};

my $host    = shift(@ARGV);
my $days    = $options->{days} || DAYS;
my $whois   = $options->{whois} || COMMAND;
unless ($host) { 
    print STDERR "Incorrect host\n";
    print ERROR, "\n";
    exit 1;
}

my $command = sprintf("%s -H %s", $whois, $host);

# Run command
my $exe_err = '';
my $exe_out = execute($command, undef, \$exe_err);
my $stt = ($? >> 8);
my $exe_stt = $stt ? 0 : 1;
   $exe_stt = 1 if $stt == 1;
unless ($exe_stt) {
    if ($exe_err) {
        printf STDERR "%s\n", $exe_err;
    } else {
        printf STDERR "Command error [%d]: %s\n", $stt, $command;
    }
    print ERROR, "\n";
    exit 1;
}
unless ($exe_out) {
    print STDERR "Incorrect whois output data\n";
    print ERROR, "\n";
    exit 1;
}

# Parse pool
my @lines = (0);
foreach my $line (split /\n/, $exe_out) {
    chomp($line);
    next unless $line;
    my $l = trim($line);
    next unless $l;
    next if $l =~ /free|update/;
    if ($l =~ /([0-9]{4})[^0-9]+([0-9]{2})[^0-9]+([0-9]{2})/) {
        push @lines, timegm( 0, 0, 0, $3, $2-1, $1 );
    } elsif ($l =~ /([0-9]{2})[^0-9]+([0-9]{2})[^0-9]+([0-9]{4})/) {
        push @lines, timegm( 0, 0, 0, $1, $2-1, $3 );
    }
}

# Get max
my $exp = max(@lines);
unless ($exp) {
    print STDERR "Can't get the expiry date\n";
    print ERROR, "\n";
    exit 1;
}

# Got
my $secs = $exp - time();
my $got = ($secs - ($secs % (24 * 60 * 60))) / (24 * 60 * 60);
if ($got <= $days) {
    printf STDERR "%d days left (alert=%d; expiry=%s)\n", $got, $days, scalar(gmtime($exp));
    print ERROR, "\n";
    exit 1;
}

print OK, "\n";
exit 0;

__END__
