#!/usr/bin/env perl
use v5.24;
use utf8;

use Yukki;
use Yukki::TextUtil qw( prompt dump_file );
use Yukki::User;

my $app = Yukki->new;

my @user_fields = (
    login_name => [ 'Login name:',
        -must => {
            'be only letters, numbers, underscores, and hyphens' =>
                qr/^[a-zA-Z0-9_-]{3,20}$/
        },
    ],
    password   => [ 'Password:', -echo => '' ],
    name       => [ 'Name:' ],
    email      => [ 'Email:' ],
);

my %user;
while (my ($name, $label) = splice @user_fields, 0, 2) {
    $user{$name} = prompt @$label, -v;
}

while (my $group = prompt 'Group name [enter a blank line to end]:', -v) {
    last unless $group =~ /\S/;
    push @{ $user{groups} }, $group;
}

my $user = Yukki::User->new(\%user);
$app->model('User')->set_password($user);
$app->model('User')->save($user);

# ABSTRACT: add users to a Yukki installation
# PODNAME: yukki-add-user

__END__

=pod

=encoding UTF-8

=head1 NAME

yukki-add-user - add users to a Yukki installation

=head1 VERSION

version 0.991_005

=head1 SYNOPSIS

  yukki-add-user

=head1 DESCRIPTION

Interactively adds a user to a Yukki installation. There are no command-line
options.

=head1 ENVIRONMENT

Normally, this script tries to find F<etc/yukki.conf> from the current working
directory. If no configuraiton file is found, it checks C<YUKKI_CONFIG> for the
path to this file.

=head1 AUTHOR

Andrew Sterling Hanenkamp <hanenkamp@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Qubling Software LLC.

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

=cut
