#! /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 );

# globally allow sys_getpw* and sys_getgr* cache use
$ENV{'PW_GR_CACHE'} = 1;

my $remainingOptions = OVH::Bastion::Plugin::begin(
    argv    => \@ARGV,
    header  => "your access list",
    options => {
        "hide-groups" => \my $hideGroups,
        "reverse-dns" => \my $reverse,
        "include=s"   => \my @includes,
        "exclude=s"   => \my @excludes,
    },
    helptext => <<'EOF',
Show the list of servers you have access to

Usage: --osh SCRIPT_NAME [--hide-groups] [--reverse-dns]

  --hide-groups      Don't show the machines you have access to through group rights.
                       In other words, list only your personal accesses.
  --reverse-dns      Attempt to resolve the reverse hostnames (SLOW!)
  --include PATTERN  Only include accesses matching the given PATTERN (see below)
                        This option can be used multiple times to refine results
  --exclude PATTERN  Omit accesses matching the given PATTERN (see below)
                        This option can be used multiple times.
                        Note that --exclude takes precedence over --include

**Note:** PATTERN supports the ``*`` and ``?`` wildcards.
If PATTERN is a simple string without wildcards, then names containing this string will be considered.
The matching is done on the text output of the command.
EOF
);

my $fnret;

$fnret = OVH::Bastion::get_acls(account => $self);
$fnret or osh_exit $fnret;

if (not @{$fnret->value}) {
    osh_ok R('OK_EMPTY', msg => "Dear $self, you have no registered accesses to machines through this bastion yet");
}

osh_info "Dear $self, you have access to the following servers:\n";

OVH::Bastion::print_acls(
    acls       => $fnret->value,
    reverse    => $reverse,
    hideGroups => $hideGroups,
    includes   => \@includes,
    excludes   => \@excludes
);
osh_ok($fnret);
