#!/usr/bin/perl -w
use strict;
use utf8;

=encoding utf8

=head1 NAME

ctklib - The CTKlib projects maker

=head1 SYNOPSIS

    ctklib [-dv] [-t regular|tiny|module|daemon] [-D /project/dir] create [PROJECTNAME]
    ctklib create <PROJECTNAME>
    ctklib create

    ctklib [-dv] test

=head1 OPTIONS

=over 4

=item B<-d, --debug>

Enable debug mode

=item B<-D PATH, --dir=PATH>

Specifies directory to new project saving

=item B<-h, --help>

Show short help information and quit

=item B<-H, --longhelp>

Show long help information and quit

=item B<-t TYPE, --type=TYPE>

Select output's type your project. Supported types: "regular", "tiny", "module" and "daemon"

=item B<-v, --verbose>

Enable verbose mode

=item B<-V, --ver, --version>

Version of CTK module

=item B<-y, --yes>

Will answer "yes" to all questions

=back

=head1 COMMANDS

=over 4

=item B<create>

    ctklib [-dv] [-t regular|tiny|module|daemon] [-D /project/dir] create [PROJECTNAME]

Create new project by project name

=item B<test>

    ctklib [-dv] test

Tests work environment and shows result as summary table

=back

=head1 DESCRIPTION

The CTKlib projects maker

=head1 SEE ALSO

L<CTK>

=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.

=cut

use feature qw/say/;
use Getopt::Long;
use Pod::Usage;
use CTK;
use CTK::Helper;

use constant CMDDEFAULT => 'usage';

my %options;
Getopt::Long::Configure("bundling");
GetOptions(\%options,
    # NoUsed keys map:
    #
    # a A b B c 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
    # u U     w W x X   Y
    # z Z
    "help|usage|h",         # Show help page
    "longhelp|H",           # Show long help page
    "debug|d",              # Debug mode
    "verbose|v",            # Verbose mode
    "dir|D=s",              # Save output data
    "type|t=s",             # Project type
    "version|ver|V",        # Show version
    "yes|y",                # Use defaults
) || pod2usage(-exitval => 1, -verbose => 0, -output => \*STDERR);
pod2usage(-exitval => 0, -verbose => 1) if $options{help};
pod2usage(-exitval => 0, -verbose => 2) if $options{longhelp};
$options{tty} = -t STDOUT ? 1 : 0;
my $command = @ARGV ? shift @ARGV : CMDDEFAULT;
$command = "version" if $options{version};
my @arguments = @ARGV ? @ARGV : ();

my $app = CTK::Helper->new(
        debug   => $options{debug},
        verbose => $options{verbose},
        datadir => $options{dir},
        options => \%options,
    );
$command = "usage" unless grep { $_ eq $command } ($app->list_handlers);

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

__END__
