#! /usr/bin/env perl
# vim: set filetype=perl ts=4 sw=4 sts=4 et:
use common::sense;

use File::Basename;
use lib dirname(__FILE__) . '/../../../lib/perl';
use OVH::Result;
use OVH::Bastion;
use OVH::Bastion::Plugin qw( :DEFAULT help );

OVH::Bastion::Plugin::begin(
    argv     => \@ARGV,
    header   => "freeze an account",
    options  => {"account=s" => \my $account, "reason=s" => \my $reason},
    helptext => <<'EOF',
Freeze an account, to prevent it from connecting

Usage: --osh SCRIPT_NAME --account ACCOUNT [--reason "'SOME REASON'"]

  --account ACCOUNT          Account to freeze
  --reason  "'SOME REASON'"  Optional reason for the account to be frozen (will be displayed to the user),
                               if you are in a shell (and not in interactive mode), quote it twice as shown.
EOF
);

my $fnret;

if (not $account) {
    help();
    osh_exit 'ERR_MISSING_PARAMETER', "Missing 'account' parameter";
}

if ($account eq $self) {
    osh_exit 'ERR_INVALID_PARAMETER', "Please don't freeze yourself, dear $self, we need you!";
}

# Here we parse account name
$fnret = OVH::Bastion::is_bastion_account_valid_and_existing(account => $account, localOnly => 1);
$fnret or osh_exit $fnret;
$account = $fnret->value->{'account'};

osh_info("Freezing $account...");

my @command = qw{ sudo -n -u allowkeeper -- /usr/bin/env perl -T };
push @command, $OVH::Bastion::BASEPATH . '/bin/helper/osh-accountFreezeToggle', '--action', 'freeze';
push @command, ('--account', $account);
push @command, ('--reason',  $reason) if $reason;

$fnret = OVH::Bastion::helper(cmd => \@command);
if (!$fnret) {
    osh_warn("An error occurred while attempting to freeze account ($fnret), not killing sessions");
    osh_exit($fnret);
}
osh_info($fnret);

osh_info("\nTerminating sessions if any...");

@command = qw{ sudo -n -u root -- /usr/bin/env perl -T };
push @command, $OVH::Bastion::BASEPATH . '/bin/helper/osh-accountKillSessions';
push @command, ('--account', $account);

# don't check result for this, our behaviour doesn't depend on it
OVH::Bastion::helper(cmd => \@command);

osh_exit($fnret);
