#!/usr/bin/perl -w
use strict; #$Id: mtoken 70 2019-06-09 18:25:29Z minus $

=head1 NAME

mtoken - tool for initializing Token Devices

=head1 SYNOPSIS

    mtoken [options] commands [args]

    mtoken [-dv] init [NAME]

=head1 OPTIONS

=over 4

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

Use CONFIG_FILE as configuration file

=item B<--datadir=DIR, --workdir=DIR>

Use DIR as DataDir directory

=item B<-d, --debug>

Print debug information on STDOUT

=item B<-D /foo/bar/baz, --directory=/foo/bar/baz>

Sets directory for initializing files structure

=item B<-h, --help>

Show short help information and quit

=item B<-H, --longhelp>

Show long help information and quit

=item B<-v, --verbose>

Verbose option. Include Verbose debug data in the STDOUT and to error-log output

=item B<-V, --version>

Print the version number of the program and quit

=back

=head1 COMMANDS

=over 4

=item B<init>

Initialize the Token device. Creates file structure in specifies directory
See --directory option

=back

=head1 DESCRIPTION

Tool for initializing Your Token Devices. This tool provides first steps to working with Token Devices.

For next steps see L<MToken>

=head1 AUTHOR

Serz 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 CTK::Util qw/preparedir/;
use MToken;
use MToken::Const;
use MToken::Util;
use Cwd qw/getcwd/;
use File::Spec;

$| = 1;  # autoflush

my $options = {};
Getopt::Long::Configure ("bundling");
GetOptions($options,
    # NoUsed keys map:
    #
    # a A b B   C     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 MToken
    "debug|d",              # Debug mode
    "verbose|v",            # Verbose mode

    # CTK Application
    "config|c=s",           # Config file
    "datadir|workdir=s",    # DataDir
    "directory|dir|D=s",    # Destination directory

) || 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", MToken->VERSION) && exit(0) if $options->{version};

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

# MToken Application instance
my $mt = new MToken(
        project => PROJECTNAME,
        prefix  => PREFIX,
        configfile => ($options->{config} && -e $options->{config})
            ? $options->{config}
            : File::Spec->catfile(getcwd(), sprintf("%s.conf", PREFIX)),
        ($options->{datadir} ? (datadir => $options->{datadir}) : ()),
        root    => getcwd(),
        options => $options,
        debug   => $options->{debug},
        verbose => $options->{verbose},
    );
pod2usage(-exitval => 1, -verbose => 99, -sections => 'SYNOPSIS|OPTIONS|COMMANDS', -output => \*STDERR)
    unless $command && grep {$_ eq $command} ($mt->list_handlers());

preparedir({
        datadir => $mt->datadir(),
        tempdir => $mt->tempdir(),
    });

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

exit $exitval;

1;

__END__
