#!/usr/bin/perl

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../../lib";
use Wigo::Probe qw/:all/;

###
# DEFAULT CONFIG
###

my $conf = {
    'needrestart' => '/usr/sbin/needrestart',
};

init( config => $conf );

my $needrestart = config->{'needrestart'};
unless ( -x $needrestart )
{
    status  403;
    message "needrestart $needrestart is not executable";
    output  13;
}

###
# GET STATUS LIST
###

my $outNeedRestart = `$needrestart -k -p`;
my $returnCode = $?;
my @lines = split("\n", $outNeedRestart);
detail->{'output'} = $lines[0];
detail->{'returncode'} = $returnCode;
if($returnCode == 512)
{
    status 200;
    message "Restart needed";
}
elsif($returnCode == 256)
{
  message "Restart not required";
}
elsif($returnCode)
{
    status 500;
    message "Error while getting restart status";
    output 1;
}
else
{
  message "Restart not needed";
}

output 0;
