#!/usr/bin/perl

use strict;
use warnings;

use lib './bin';
use setup;

use English qw(-no_match_vars);
use Getopt::Long;
use File::Temp;
use Pod::Usage;

use FusionInventory::Agent::Task::Inventory;
use FusionInventory::Agent::Target::Local;
use FusionInventory::Agent::Logger;
use FusionInventory::Agent::Config;
use FusionInventory::Agent::Version;

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

GetOptions(
    $options,
    'additional-content=s',
    'backend-collect-timeout=s',
    'html',
    'no-category=s',
    'scan-homedirs',
    'scan-profiles',
    'tag|t=s',
    'verbose',
    'debug+',
    'help',
    'version',
) or pod2usage(-verbose => 0);

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

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

my $verbosity =
    $options->{debug} == 0 ? LOG_INFO   :
    $options->{debug} == 1 ? LOG_DEBUG  :
    $options->{debug} == 2 ? LOG_DEBUG2 :
                             LOG_DEBUG2 ;

my $inventory = FusionInventory::Agent::Task::Inventory->new(
    target => FusionInventory::Agent::Target::Local->new(
        path       => '-',
        html       => $options->{html},
        basevardir => File::Temp->newdir(CLEANUP => 1),
    ),
    logger  => FusionInventory::Agent::Logger->new(verbosity => $verbosity),
    datadir => $setup{datadir},
    confdir => $setup{confdir},
    config  => $config
);

$inventory->run();

__END__

=head1 NAME

fusioninventory-inventory - Standalone inventory

=head1 SYNOPSIS

fusioninventory-inventory [options]

  Options:
    --scan-homedirs                scan use home directories (false)
    --scan-profiles                scan user profiles (false)
    --html                         save the inventory as HTML (false)
    --no-category=CATEGORY         do not list given category items
    -t --tag=TAG                   mark the machine with given tag
    --backend-collect-timeout=TIME timeout for inventory modules
                                     execution (30)
    --additional-content=FILE      additional inventory content file
    --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<fusioninventory-inventory> allows to run an inventory task without a GLPI
server.
