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

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

GetOptions(
    $options,
    'model=s',
    'host=s',
    'file=s',
    'community=s',
    'entity=s',
    'verbose',
    'debug+',
    'help',
) or pod2usage(-verbose => 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 ? 'PRINTER'    :
    $model->{TYPE} == 2 ? 'NETWORKING' :
                          undef        ;

my $inventory = FusionInventory::Agent::Task::NetInventory->new(
    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
        }
    ],
    DEVICE => [
        {
            TYPE         => $type,
            IP           => $options->{host},
            FILE         => $options->{file},
            AUTHSNMP_ID  => 1,
            MODELSNMP_ID => 1
        }
    ],
    MODEL => [ $model ],
    AUTHENTICATION => [
        {
            ID        => 1,
            COMMUNITY => $options->{community},
        }
    ]
};
if ($options->{entity}) {
    $inventory->{options}->{DEVICE}->[0]->{ENTITY} = $options->{entity};
}
$inventory->{client} =
    FusionInventory::Agent::Task::NetInventory::Client->new(
        verbose => $options->{verbose}
    );

$inventory->run();

sub loadModel {
    my ($file) = @_;

    my $model = XML::TreePP->new()->parsefile($file)->{model};

    my @get = map {
        {
            OID    => $_->{oid},
            OBJECT => $_->{mapping_name},
            VLAN   => $_->{vlan},
        }
    } grep {
        $_->{dynamicport} == 0
    } @{$model->{oidlist}->{oidobject}};

    my @walk = map {
        {
            OID    => $_->{oid},
            OBJECT => $_->{mapping_name},
            VLAN   => $_->{vlan},
        }
    } grep {
        $_->{dynamicport} == 1
    } @{$model->{oidlist}->{oidobject}};

    return {
        ID   => 1,
        NAME => $model->{name},
        TYPE => $model->{type},
        GET  => \@get,
        WALK => \@walk
    }
}

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) = @_;

    return;
}

__END__

=head1 NAME

fusioninventory-netinventory - Network inventory from command line

=head1 SYNOPSIS

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

  Options:
    -h --help      this menu
    --host host    host to inventorize
    --file         snmpwalk output file
    --model model  XML model file
    --community    community string (default: public)
    --entity       GLPI entity
    --verbose      verbose output (control messages)
    --debug        debug output (execution traces)
