#!/home/product/code/webui-perl/bin/perl -T -I /home/product/code/firmware/current/lib/perl5
#SUDOER_USERS: nobody
#SUDOER_RUNAS_USERS: root, mta
#SUDOER_TAGS: NOPASSWD, EXEC
use strict;
use warnings;
use Barracuda::Sudo::Lib;
use IPC::Run qw(run);

=pod

=head1 NAME

ethtool

=head1 DESCRIPTION

Script to call ethtool with sudo permissions. Root priviliges
are not mandatory for ethtool. However, ethtool will not
output some information without root priviliges.

The location of the ethtool binary will be determined in runtime
The first match in the following list will be used:

        /home/product/code/firmware/current/sbin/ethtool

        /root/ethtool

        /boot/os_tools/ethtool

        /sbin/ethtool


=cut


my @ethtools = (
        "/home/product/code/firmware/current/sbin/ethtool",
        "/root/ethtool",
        "/boot/os_tools/ethtool",
        "/sbin/ethtool",
);

my $ethtool;
my $outfile = sudo_out();

my ($ifc) = get_argv(qr/^[a-zA-Z]+[0-9.-]*|[a-zA-Z]+\-\d\-\d$/);

# Figure out which ethtool to use
foreach my $et (@ethtools) {
    if( -x $et) {
        $ethtool = $et;
        last;
    }
}

die "No ethtool found" unless defined $ethtool;

my $status = run([$ethtool, $ifc], \undef, $outfile, $outfile);

exit !$status;

