#!/usr/bin/perl

# Copyright (C) 2004 Robert Vojta <robert.vojta@mandrake.cz> 
#
# 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, 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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use lib qw(/usr/share/drakxtools-ng/lib);
use preload;
use dialogs;
use comm;

use MDK::Common;

use lib qw(/usr/lib/libDrakX);
use run_program;

use Locale::gettext;
use POSIX;

use Gtk2 -init;
use Gtk2::GladeXML;

setuplocales();

#
# Check for cryptoloop module
#
my $load_tries = 0;

cryptoloop_module_check:

my $modules = cat_("/proc/modules");
if ($modules !~ /cryptoloop/) {
    if (!$> && $load_tries == 0) {
	if (dialogs::yesno_dialog(undef, undef,
				  T("Would you like to load cryptoloop module?"),
				  T("Cryptoloop module is not loaded and I'm not able to continue without this module. If you want to let me to load this module, click Yes button. Otherwise click No button and I will end."))) {
	    $load_tries++;
	    run_program::run('/sbin/modprobe', 'cryptoloop');
	    goto cryptoloop_module_check;
	} else {
	    exit;
	}
    } else {
	dialogs::warning_dialog(undef, undef,
				$load_tries == 0 ? T("Cryptoloop module is not loaded") : T("Failed to load cryptoloop module"),
				T("I'm sorry, I'm not able to continue until your administrator will solve this problem."));
	  exit;
    }
}

#
# Glade XML file informations
#
my $xmlfile = "$::gtkguidir/drakloop.glade";

my $gladexml = Gtk2::GladeXML->new($xmlfile, 'drakloopw');
my $mainw = $gladexml->get_widget('drakloopw');

#
# Main widgets
#
my $directory_entry = $gladexml->get_widget('directory_entry');
my $size_entry = $gladexml->get_widget('size_entry');
my $size_unit_combo = $gladexml->get_widget('size_unit_combo');
my $passphrase_entry = $gladexml->get_widget('passphrase_entry');
my $confirmation_entry = $gladexml->get_widget('confirmation_entry');
my $encryption_combo = $gladexml->get_widget('encryption_combo');

#
# Connect signals and run
#
$gladexml->signal_autoconnect_from_package('main');
Gtk2->main;
exit;

sub gtk_main_quit {
    Gtk2->main_quit;
}

sub on_cancel_button_clicked {
    Gtk2->main_quit;
}

sub check_user_entries {
    $directory = $directory_entry->get_text;
    if (length($directory) == 0) {
	dialogs::warning_dialog($main, undef, T("Wrong directory name"),
				T("Your directory is not configured properly."));
        return 0;
    }

    $size = $size_entry->get_text;
    if ($size <= 0) {
        dialogs::warning_dialog($mainw, undef, T("Wrong folder size"),
				$size eq "" ? T("Size is not configured.") : T("Current size is set to %d, should be greater than 0.", $size));
	return 0;
    }

    $passphrase = $passphrase_entry->get_text;
    if (length($passphrase) < 20) {
        dialogs::warning_dialog($mainw, undef, T("Passphrase is too short"),
				T("Your passphrase is too short, it's %d ".
				"characters length. It should be at least 20 characters ".
			    "length.", length($passphrase)));
	return 0;
    }

    $confirmation = $confirmation_entry->get_text;
    if ($passphrase ne $confirmation) {
        dialogs::warning_dialog($mainw, undef, T("Password mismatch"),
				T("Passphrase and confirmation entry doesn't match, please correct them."));
	return 0;
    }

    1;
}

sub apply_changes {
    $directory = $directory_entry->get_text;
    $size = $size_entry->get_text;
    $passphrase = $passphrase_entry->get_text;
    $encryption = $encryption_combo->entry->get_text;

    $size_unit = $size_unit_combo->entry->get_text;
    if ($size_unit eq "kB") {
	$size_unit = "k";
    } elsif ($size_unit eq "MB") {
	$size_unit = "M";
    } elsif ($size_unit eq "GB") {
	$size_unit = "G";
    } else {
	$size_unit = "B";
    }

    if (system("mkdir -p $directory && dd if=/dev/zero of=$directory/encfile bs=${size}${size_unit} count=1")) {
	dialogs::warning_dialog($mainw, undef, T("Failed to create directory"),
				$directory);
	  exit(1);
    }

    open F, "|encsetup $encryption $directory/encfile";
    print F $passphrase;
    if (!close F) {
	dialogs::warning_dialog($mainw, undef, T("Encsetup failed"),
				T("Failed to call encsetup.\n\n|encsetup %s %s/encfile",
							$encryption, $directory));
	  exit(1);
    }

    open F, "|mountloop $encryption $directory/encfile $directory";
    print F $passphrase;
    if (!close F) {
	dialogs::warning_dialog($mainw, undef, T("Mountloop failed"),
				T("Failed to call mountoop.\n\n|mountloop %s %s/encfile %s", $encryption, $directory, $directory));
	  exit(1);
    }

    my $home = $ENV{HOME};

    open F, ">>$home/.mountlooprc";
    print F "$encryption $directory/encfile $directory\n";
    if (!close F) {
	dialogs::warning_dialog($mainw, undef, T("Failed to write configuration"),
				T("Failed to access %s/.mountlooprc file.", $home));
	  exit(1);
    }

    1;
}

sub on_ok_button_clicked {
    return if !check_user_entries;
    return if !apply_changes;

    Gtk2->main_quit;
}

