#!/usr/pkg/bin/perl

use strict;
use warnings;

use lib "/usr/pkg/share/fusioninventory/lib";
use setup;

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

use FusionInventory::Agent::Task::WakeOnLan;
use FusionInventory::Agent::Logger;
use FusionInventory::Agent::Tools::Network;
use FusionInventory::Agent::Version;

my $options = {
    debug => 0,
};

GetOptions(
    $options,
    'mac=s',
    'methods=s',
    'debug+',
    'help',
    'version',
) or pod2usage(-verbose => 0);

if ($options->{version}) {
    my $PROVIDER = $FusionInventory::Agent::Version::PROVIDER;
    map { print $_."\n" }
        "WakeOnLan task $FusionInventory::Agent::Task::WakeOnLan::VERSION",
        "based on $PROVIDER Agent v$FusionInventory::Agent::Version::VERSION",
        @{$FusionInventory::Agent::Version::COMMENTS}
        ;
    exit 0;
}
pod2usage(-verbose => 0, -exitval => 0) if $options->{help};
pod2usage(
    -message => "no mac address given, aborting\n", -verbose => 0
) unless $options->{mac};
pod2usage(
    -message => "invalid mac address given, aborting\n", -verbose => 0
) if $options->{mac} !~ /^$mac_address_pattern$/;

my $task = FusionInventory::Agent::Task::WakeOnLan->new(
    target => {},
    logger => FusionInventory::Agent::Logger->new(config => $options)
);

$task->{options} = {
    NAME => 'WAKEONLAN',
    PARAM => [
        {
            MAC           => $options->{mac},
        }
    ],
};

my %params = $options->{methods} ?
    (methods => [ split(/,/, $options->{methods}) ]) : ();
$task->run(%params);

__END__

=head1 NAME

fusioninventory-wakeonlan - Standalone wake-on-lan

=head1 SYNOPSIS

fusioninventory-wakeonlan [options]

  Options:
    --mac=MAC         target mac address
    --methods=METHODS comma-separated list of methods to use (ethernet, udp)
    --debug           debug output (execution traces)
    -h --help         print this message and exit
    --version         print the task version and exit

=head1 DESCRIPTION

F<fusioninventory-wakeonlan> can be used to run a wakeonlan task without a GLPI
server.
