#!/usr/bin/env perl
#    GNU Public License (GPL)
#
#    Copyright (c) 2014 Stéphane GALLAND <galland@arakhne.org>
#
#    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 program; see the file COPYING. If not, write to the Free
#    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#    02111-1307, USA.

require 5.014;

use strict;
use utf8;

#--------------------------------------------------------------

use constant VERSION => '8.2';
use constant APPNAME => 'kernel-cleaner-gtk3';

#--------------------------------------------------------------

use POSIX qw(setlocale);
use Locale::gettext;
use File::Basename;
use File::Spec;

my $SOURCE_DIRECTORY;
BEGIN {
	my $LANG_DIRECTORY;
	$SOURCE_DIRECTORY = dirname($0);
	if (-r File::Spec->catfile($SOURCE_DIRECTORY, 'po', 'fr', 'LC_MESSAGES', 'kernel-cleaner-gtk3.mo')) {
		$LANG_DIRECTORY = File::Spec->rel2abs(File::Spec->catfile($SOURCE_DIRECTORY, 'po'));
	}
	else {
		$SOURCE_DIRECTORY = dirname($SOURCE_DIRECTORY);
		if (-r File::Spec->catfile($SOURCE_DIRECTORY, 'po', 'fr', 'LC_MESSAGES', 'kernel-cleaner-gtk3.mo')) {
			$LANG_DIRECTORY = File::Spec->rel2abs(File::Spec->catfile($SOURCE_DIRECTORY, 'po'));
		}
		else {
			$LANG_DIRECTORY = '/usr/share/locale';
		}
	}
	if (-f File::Spec->catfile($SOURCE_DIRECTORY, "Gtk3", "Notify.pm")) {
		unshift @INC, $SOURCE_DIRECTORY;
	}
	bindtextdomain(APPNAME, $LANG_DIRECTORY);
	bind_textdomain_codeset(APPNAME, 'UTF-8');
	textdomain(APPNAME);
}

#--------------------------------------------------------------

use Getopt::Long;
use Gtk3::Notify -init, APPNAME;

use constant FALSE => (0==1);
use constant CLEANER_CMD => '/usr/sbin/kernel-cleaner';
use constant CLEANER_CMD2 => File::Spec->catfile($SOURCE_DIRECTORY,'kernel-cleaner');

#--------------------------------------------------------------

my $cleanThread;
my $icon = undef;

sub _T {
	return gettext(@_);
};

sub _GTK($) {
	if (!Encode::is_utf8($_[0])) {
		return Encode::decode('UTF-8', $_[0]);
	}
	return $_[0];
}

sub usage() {
    print _T("usage:")."\n";
    print "\t".APPNAME." [--version] [--force]\n";
    exit(1);
}

sub notify($$) {
	my $title = shift || '';
	my $text = shift || '';
	print "$title\n";
	my $notification = Gtk3::Notify::Notification->new(_GTK($title), _GTK($text));
	$notification->show;
}

#--------------------------------------------------------------

my %cmdlineOptions = ();
my $removable = FALSE;

if (!GetOptions('force' => \$cmdlineOptions{'force'},
                'version' => \$cmdlineOptions{'version'})) {
	usage();
	exit(2);
}

if ($cmdlineOptions{'version'}) {
	print VERSION."\n";
	exit(0);
}

my $cmd;
if (-x CLEANER_CMD2) {
	$cmd = CLEANER_CMD2;
}
else {
	$cmd = CLEANER_CMD;
}
system("$cmd", '--testremove');
$removable = ($? == 0);


if ($removable || $cmdlineOptions{'force'}) {

	notify(
		_T("Old operating system kernels are detected"),
		_T("The files associated with the old versions of the Linux kernel will be removed after entering your password."));

	if (system('pkexec', CLEANER_CMD, '--remove')==0) {
		notify(
			_T("Kernels removed"),
			_T("The old Linux kernels were removed for your system."));
	}
}

exit(0);
