#!/usr/bin/perl

use strict;
use warnings;
use LATMOS::Accounts::Maintenance;
use Getopt::Long;
use Pod::Usage;

=head1 NAME

    la-warn-expire - query expiration of user and send mail

=head1 SYNOPSIS

    la-warn-expire [-m]

=cut

GetOptions(
    'c|config=s' => \my $config,
#    'b|base=s'   => \my $base,
    'm|mail'     => \my $mail,
    'help'       => sub { pod2usage(0) },
    'users|u=s'    => \my @users,
    'to=s'       => \my $to,
) or pod2usage();

=head1 OPTIONS

=over 4

=item -c|--config configfile

Use this cofngiuration file instead the default one

=item -m|mail

Send a mail to user concerned by the futur expiration of their account

=item u|user USER

Send email only to this user(s) if their account will expire. Usefull for
testing and not sending hundred of mail.

Multiple users can be specified by using -u several times

=item --to MAIL

Instead sending email to user and eventually their manager, send the mail to
only this email address.

=back

=head1 CONFIGURATION

Some variables are taken from configuration file:

=over 4

=item mailFrom

Specify the From address to use in mail

=item smtp

The smtp server to use to send mail

=item alwayscc

In normal mode (without --to), the mail is also sent this email address.

=back

=head1 USERS EMAIL ADDRESSES

The mail use to send warning to users and manager is taken from 'mail'
attributes. If the attributes is unset, the user will not receive the mail.

=cut

my $LA = LATMOS::Accounts::Maintenance->new($config);

if ($mail) {
    $LA->warn_next_expire_users(to => $to, users => (@users ? \@users : undef));
} else {
    foreach ($LA->find_next_expire_users()) {
        printf("%-10s %s (%s)\n", $_->{delay}, $_->{obj}->get_c_field('displayName'),
            $_->{obj}->id);
    }
}
