#!/usr/bin/perl -T

# needrestart-session - check for processes need to be restarted in user sessions
#
# Authors:
#   Thomas Liske <thomas@fiasko-nw.net>
#
# Copyright Holder:
#   2014 - 2015 (C) Thomas Liske [http://fiasko-nw.net/~thomas/]
#
# License:
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this package; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

use Net::DBus;
use Net::DBus::Service;
use Net::DBus::Reactor;
use Sys::Syslog qw(:standard :macros);

use warnings;
use strict;

$0 = q(needrestart-dbus-system);

BEGIN {
    openlog(q(needrestart-dbus-system), 'pid', LOG_DAEMON);
}

END {
    syslog(LOG_INFO, q(terminated));
}

sub WARN_handler {
    my $signal = shift;
    syslog(LOG_WARNING, q(%s), $signal);
}

sub DIE_handler {
    my $signal = shift;
    syslog(LOG_ERR, q(%s), $signal);
    exit;
}

$SIG{__WARN__} = q(WARN_handler);
$SIG{__DIE__}  = q(DIE_handler);
$SIG{TERM}  = q(DIE_handler);

syslog(LOG_INFO, q(needrestart-dbus-system %s launched), $NeedRestart::VERSION);

package NeedRestart::DBus;

use NeedRestart;
use Net::DBus::Exporter qw(net.ibh.NeedRestart.System);
use Sys::Syslog qw(:standard :macros);
use base qw(Net::DBus::Object);

sub new {
    my $class = shift;
    my $service = shift;

    my $self = $class->SUPER::new($service, q(/net/ibh/NeedRestart/System));
    bless $self, $class;

    syslog(LOG_INFO, q(new instance));

    return $self;
}

dbus_signal(q(NotifySessions));
dbus_method(q(emitNotifySessions));
sub emitNotifySessions {
    my $self = shift;

    syslog(LOG_INFO, q(emit NotifySessions signal));

    return $self->emit_signal(q(NotifySessions));
}


package main;

my $bus = Net::DBus->system;
my $service = $bus->export_service('net.ibh.NeedRestart.System');
my $object = NeedRestart::DBus->new($service);

syslog(LOG_INFO, q(entering event loop...));
Net::DBus::Reactor->main->run();
syslog(LOG_INFO, q(leaving event loop));
