#!/usr/bin/perl

use strict;
use warnings;

use lib './lib';
use setup;

use UNIVERSAL::require;
use English qw(-no_match_vars);
use Getopt::Long;
use File::Temp;

use GLPI::Agent::Task::Inventory;
use GLPI::Agent::Target::Local;
use GLPI::Agent::Logger;
use GLPI::Agent::Config;
use GLPI::Agent::Event;
use GLPI::Agent::Version;

my $options = {
    debug  => 0,
    config => 'none'
};

unless (GetOptions(
    $options,
    'assetname-support=i',
    'additional-content=s',
    'backend-collect-timeout=s',
    'credentials=s@',
    'glpi_version=s',
    'html',
    'itemtype=s',
    'json',
    'no-category=s',
    'partial=s',
    'required-category=s',
    'scan-homedirs',
    'scan-profiles',
    'tag|t=s',
    'verbose',
    'debug+',
    'help',
    'version',
)) {
    Pod::Usage->use();
    pod2usage(-verbose => 0);
}

if ($options->{version}) {
    my $PROVIDER = $GLPI::Agent::Version::PROVIDER;
    map { print $_."\n" }
        "$PROVIDER Inventory task $GLPI::Agent::Task::Inventory::VERSION",
        "based on $PROVIDER Agent v$GLPI::Agent::Version::VERSION",
        @{$GLPI::Agent::Version::COMMENTS}
        ;
    exit 0;
}

if ($options->{help}) {
    Pod::Usage->use();
    pod2usage(-verbose => 0, -exitstatus => 0);
}

my $event;
if ($options->{partial}) {

    # Merge required category with partial option
    $options->{partial} = join(",", $options->{partial}, $options->{"required-category"})
        if $options->{"required-category"};

    $event = GLPI::Agent::Event->new(
        task        => "inventory",
        partial     => 1,
        category    => $options->{partial},
        name        => "partial inventory",
    );
}

my $config = GLPI::Agent::Config->new(
    options => $options,
);

my $inventory = GLPI::Agent::Task::Inventory->new(
    target => GLPI::Agent::Target::Local->new(
        path       => '-',
        html       => $options->{html},
        json       => $options->{json},
        basevardir => File::Temp->newdir(CLEANUP => 1),
        glpi       => $options->{"glpi-version"},
    ),
    logger  => GLPI::Agent::Logger->new(config => $config),
    datadir => $setup{datadir},
    event   => $event,
    config  => $config
);

$inventory->{credentials} = $options->{credentials}
    if $options->{credentials};

$inventory->{nochecksum} = 1;

$inventory->run();

__END__

=head1 NAME

glpi-inventory - Standalone inventory

=head1 SYNOPSIS

glpi-inventory [options]

  Options:
    --scan-homedirs                scan use home directories (false)
    --scan-profiles                scan user profiles (false)
    --html                         save the inventory as HTML (false)
    --json                         save the inventory as JSON (false)
    --no-category=CATEGORY         do not list given category items
    --partial=CATEGORY             make a partial inventory of given category
                                     items, this option implies --json
    --required-category=CATEGORY   list of category required even when generating a partial inventory
    --credentials                  set credentials to support database inventory
    -t --tag=TAG                   mark the machine with given tag
    --backend-collect-timeout=TIME timeout for inventory modules
                                     execution (30)
    --glpi-version=VERSION         set targeted glpi version to enable supported features
    --itemtype=TYPE                set asset type for target supporting genericity like GLPI 11+
    --additional-content=FILE      additional inventory content file
    --assetname-support=1|2        [unix/linux only] set the asset name depending on the given value:
                                    - 1 (the default), the short hostname is used as asset name
                                    - 2, the as-is hostname (can be fqdn) is used as asset name
                                   this feature is not supported on MacOS or Windows
    --verbose                      verbose output (control messages)
    --debug                        debug output (execution traces)
    -h --help                      print this message and exit
    --version                      print the task version and exit

=head1 DESCRIPTION

F<glpi-inventory> can be used to run an inventory task without a GLPI
server.
