#! /usr/bin/perl -T
# vim: set filetype=perl ts=4 sw=4 sts=4 et:
# SUDOERS # to be able to generate an egress password for accounts
# SUDOERS %osh-accountGeneratePassword   ALL=(%bastion-users) NOPASSWD:/usr/bin/env perl -T /opt/bastion/bin/helper/osh-accountGeneratePassword *
# FILEMODE 0755
# FILEOWN 0 0

#>HEADER
use common::sense;
use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);

use File::Basename;
use lib dirname(__FILE__) . '/../../lib/perl';
use OVH::Result;
use OVH::Bastion;
use OVH::Bastion::Helper;
use OVH::Bastion::Plugin::generatePassword;

# Fetch command options
my ($result,  @optwarns);
my ($account, $size);
eval {
    local $SIG{__WARN__} = sub { push @optwarns, shift };
    $result = GetOptions(
        "account=s" => sub { $account //= $_[1] },
        "size=i"    => sub { $size    //= $_[1] },
    );
};
if ($@) { die $@ }

if (!$result) {
    local $" = ", ";
    HEXIT('ERR_BAD_OPTIONS', msg => "Error parsing options: @optwarns");
}

OVH::Bastion::Helper::check_spurious_args();

if (not $size or not $account) {
    HEXIT('ERR_MISSING_PARAMETER', msg => "Missing argument 'size' or 'account'");
}

#<HEADER

HEXIT(
    OVH::Bastion::Plugin::generatePassword::act(
        self    => $self,
        context => 'account',
        account => $account,
        size    => $size,
        sudo    => 1
    )
);
