#!/usr/bin/perl

use strict;
use warnings;
use lib './lib';

use English qw(-no_match_vars);
use Getopt::Long;
use Pod::Usage;
use XML::TreePP;

use FusionInventory::Agent::Task::NetInventory;
use FusionInventory::Agent::Tools::Hardware;
use FusionInventory::Agent::Logger;

my $options = {
    community => 'public'
};

my %setup = (
    confdir => './etc',
    datadir => './share',
    libdir  => './lib',
    vardir  => './var',
);


GetOptions(
    $options,
    'model=s',
    'host=s',
    'file=s',
    'community=s',
    'entity=s',
    'timeout=i',
    'verbose',
    'debug+',
    'help',
    'version',
) or pod2usage(-verbose => 0);

if ($options->{version}) {
  print "NetInventory task $FusionInventory::Agent::Task::NetInventory::VERSION\n";
  exit 0;
}
pod2usage(-verbose => 0, -exitval => 0) if $options->{help};

pod2usage(
    -message => "no model given, aborting\n", -verbose => 0
) unless $options->{model};
pod2usage(
    -message => "invalid file '$options->{model}', aborting\n", -verbose => 0
) unless -f $options->{model};
pod2usage(
    -message => "no host nor file given, aborting\n", -verbose => 0
) unless $options->{host} or $options->{file};

my $model = loadModel($options->{model});
my $type =
    $model->{TYPE} == 1 ? 'COMPUTER'   :
    $model->{TYPE} == 2 ? 'NETWORKING' :
    $model->{TYPE} == 3 ? 'PRINTER'    :
                          undef        ;

my $inventory = FusionInventory::Agent::Task::NetInventory->new(
    %setup,
    target => FusionInventory::Agent::Task::NetInventory::Target->new(),
    logger =>  FusionInventory::Agent::Logger->new(
        debug => $options->{debug}
    )
);

$inventory->{options} = {
    NAME => 'SNMPQUERY',
    PARAM => [
        {
            PID           => 1,
            THREADS_QUERY => 1,
            TIMEOUT       => $options->{timeout},
        }
    ],
    DEVICE => [
        {
            ID           => 0,
            TYPE         => $type,
            IP           => $options->{host},
            FILE         => $options->{file},
            AUTHSNMP_ID  => 1,
            MODELSNMP_ID => 1
        }
    ],
    MODEL => [ $model ],
    AUTHENTICATION => [
        {
            ID        => 1,
            COMMUNITY => $options->{community},
        }
    ]
};
if (defined($options->{entity})) {
    $inventory->{options}->{DEVICE}->[0]->{ENTITY} = $options->{entity};
}
$inventory->{client} =
    FusionInventory::Agent::Task::NetInventory::Client->new(
        verbose => $options->{verbose}
    );
# TODO: need to be dropped the day we will depend on agent >= 2.3.0
$inventory->{deviceid} = 'foo';

$inventory->run();

package FusionInventory::Agent::Task::NetInventory::Client;

sub new {
    my ($class, %params) = @_;

    return bless {
        verbose => $params{verbose}
    }, $class;
}

sub send {
    my ($self, %params) = @_;

    # don't display control message by default
    return unless $self->{verbose}
        or $params{message}->{h}->{CONTENT}->{DEVICE};

    print $params{message}->getContent();
}

package FusionInventory::Agent::Task::NetInventory::Target;

sub new {
    my ($class) = @_;

    return bless {}, $class;
}

sub getUrl {
    my ($self, %params) = @_;

    ## no critic (ExplicitReturnUndef)
    return undef;
}

__END__

=head1 NAME

fusioninventory-netinventory - Standalone network inventory

=head1 SYNOPSIS

fusioninventory-netinventory [options] [--host <host>--file <file>]
  [--model <model>]

  Options:
    --host host    host to inventorize
    --file         snmpwalk output file
    --model model  XML model file
    --community    community string (default: public)
    --timeout val  SNMP timeout (default: 15s)
    --entity       GLPI entity
    --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-netinventory> allows to run a network inventory task without
a GLPI server.
