#!/usr/bin/perl -w
use strict; # $Id: monotifier 73 2022-09-15 13:29:50Z abalama $

=encoding utf8

=head1 NAME

monotifier - extension for the monm notifications

=head1 SYNOPSIS

    monotifier [options] [commands [args]]

    monotifier
    monotifier show
    monotifier show <ID>
    monotifier remove <ID>
    monotifier clean
    monotifier truncate

=head1 OPTIONS

=over 4

=item B<-c CONFIG_FILE, --config=CONFIG_FILE>

Full path of the configuration file. The configuration file allows determine the
basic default settings, which will use the system if it fails to specify additional
configuration files in $CONFDIR directory. The default system
path /etc/monm/monm.conf

=item B<-d DATADIR, --datadir=DATADIR, --dir=DATADIR>

The directory of temporary files.

Default: system temp directory (/tmp/monm)

=item B<-h, --help>

Show short help information and quit

=item B<-H, --longhelp>

Show long help information and quit

=item B<-v, --verbose>

Enabling at which displays information about the progress on the screen

=item B<-V, --version>

Print the version number of the program and quit

=back

=head1 COMMANDS

=over 4

=item B<clean>

    monotifier clean

Remove incorrect messages

=item B<remove>

    monotifier remove <ID>

Remove the message by ID

=item B<show>

    monotifier show
    monotifier show <ID>

Show data table or selected record by id

=item B<truncate>

    monotifier truncate

Remove all messages

=back

=head1 DESCRIPTION

This is an extension for the monm notifications over different
communication channels

=head1 AUTHOR

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

=head1 COPYRIGHT

Copyright (C) 1998-2022 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 File::Spec;

use App::MonM::Notifier::Monotifier;
use App::MonM::Const;

use constant {
    CMDDEFAULT  => 'info',
};

my $options = {};
Getopt::Long::Configure("bundling");
GetOptions($options,
    # NoUsed keys map:
    #
    # a A b B   C   D e E
    # f F g G     i I j J
    # k K l L m M n N o O
    # p P q Q r R s S t T
    # u U     w W x X y Y
    # z Z

    # Information and debug
    "help|usage|h",         # Show help page
    "longhelp|H|?",         # Show long help page
    "version|vers|ver|V",   # Print VERSION of the App::MonM
    "verbose|v",            # Verbose mode

    # CTK Application
    "config|conf|c=s",      # Config file
    "datadir|dir|d=s",      # DataDir

) || pod2usage(-exitval => 1, -verbose => 0, -output => \*STDERR);
pod2usage(-exitval => 0, -verbose => 1) if $options->{help};
pod2usage(-exitval => 0, -verbose => 2) if $options->{longhelp};
printf("Version: %s\n", App::MonM::Notifier::Monotifier->VERSION) && exit(0) if $options->{version};

# VARS
my $command = shift(@ARGV) || CMDDEFAULT;
my @arguments = @ARGV;

# App::MonM::Monotifier instance
my $app = App::MonM::Notifier::Monotifier->new(
        project => PROJECTNAME,
        prefix  => PREFIX,
        ($options->{config} && -e $options->{config} ? (configfile => $options->{config}) : ()),
        ($options->{datadir} ? (datadir => $options->{datadir}) : ()),
        options => $options,
        verbose => $options->{verbose},
    );
pod2usage(-exitval => 1, -verbose => 99, -sections => 'SYNOPSIS|OPTIONS|COMMANDS', -output => \*STDERR)
    unless $command && grep {$_ eq $command} ($app->list_handlers());

# Run
my $exitval = $app->run($command, @arguments) ? 0 : 1;
printf STDERR "%s\n", $app->error if $exitval && $app->error;

exit $exitval;

1;

__END__
