#!/usr/bin/perl -wT
use v5.14;
use strict;

use Apache2::Authen::Passphrase qw/pwset USER_REGEX/;

use IO::Prompter [qw/-e* -stdio/];

die "Usage: aap-passwd [rootdir] username\n" unless $#ARGV >= 0 && $#ARGV <= 1;
($Apache2::Authen::Passphrase::rootdir) = (shift =~ /(.*)/) if $#ARGV == 1;

my $username = shift;
die "Invalid username" unless $username =~ USER_REGEX;
($username) = ($username =~ /(.*)/);

my $passwd = prompt 'Enter new Apache2::Authen::Passphrase password: ';
my $confirm = prompt 'Retype new Apache2::Authen::Passphrase password: ';

die "Sorry, passwords do not match\n" unless $passwd eq $confirm;
pwset $username, $passwd;
say "Password updated successfully";

__END__

=head1 NAME

aap-passwd - passwd utility for Apache2::Authen::Passphrase

=head1 SYNOPSIS

  user@hostname:~$ aap-passwd /path/to/rootdir marius
  Enter new Apache2::Authen::Passphrase password: ***
  Retype new Apache2::Authen::Passphrase password: ***
  Password updated successfully

=head1 DESCRIPTION

aap-passwd updates the password of an Apache2::Authen::Passphrase user.

It is used like this:

    aap-passwd /path/to/rootdir username

where the C<rootdir> is the first argument and the username whose password is to be changed is the second argument, or like this:

    aap-passwd username

where the C<rootdir> is taken from the environment and the username is the only argument.

=head1 ENVIRONMENT

=over

=item AAP_ROOTDIR

The default Apache2::Authen::Passphrase C<rootdir>, used if the C<rootdir> is not explicitly given.

=back

=head1 AUTHOR

Marius Gavrilescu C<< <marius@ieval.ro> >>

=head1 COPYRIGHT AND LICENSE

Copyright 2013 Marius Gavrilescu

This file is part of fonbotd.

fonbotd is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

fonbotd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with fonbotd.  If not, see <http://www.gnu.org/licenses/>


=cut

