#!/usr/bin/perl

=head1 NAME

parcimonie-applet - GNOME monitoring applet for parcimonie

=cut

use strict;
use warnings;
use 5.10.0;

our $VERSION = '0.7.1'; # VERSION

use FindBin;
use lib "$FindBin::Bin/../lib";

use Carp;
use Try::Tiny;

my $mu;
sub record_memory_usage { 1 }
sub report_memory_usage { 1 }

BEGIN {
    if (exists $ENV{REPORT_MEMORY_USAGE}
            && defined $ENV{REPORT_MEMORY_USAGE}
            && $ENV{REPORT_MEMORY_USAGE}) {
        try {
            require Memory::Usage;
        } catch {
            croak "Memory::Usage is needed when REPORT_MEMORY_USAGE is set."
        };
        $mu = Memory::Usage->new();
        no warnings 'redefine';
        *record_memory_usage = sub { $mu->record(shift) };
        *report_memory_usage = sub { $mu->dump() };
    }
}

BEGIN {
    record_memory_usage('before loading App::Parcimonie::Applet');
    require App::Parcimonie::Applet;
    App::Parcimonie::Applet->import();
    record_memory_usage('after loading App::Parcimonie::Applet');
}

$SIG{'INT'}  = $SIG{'TERM'} = sub { report_memory_usage(); exit(0); };
$SIG{'USR1'} = sub { report_memory_usage(); };

App::Parcimonie::Applet->new->run;
report_memory_usage();
