#!/usr/bin/perl

use lib qw(/usr/lib/libDrakX);
use standalone;
use common;
use interactive;
use run_program;
use fs::format;
use fs::mount;

my $conf_file = '/etc/sysconfig/draklive-resize';
my %conf = getVarsFromSh($conf_file);

my $part = {
	    real_device => $conf{LOOPBACK},
	    fs_type => $conf{TYPE},
	    mntpoint => $conf{MOUNT},
	    options => "loop",
};

sub safe_resize {
    my ($in, $initial_size, $new_size) = @_;
    eval { resize($in, $initial_size, $new_size) };
    my $error = $@;
    $in->ask_warn(N("Error"), N("Unable to resize system space: %s", formatError($error))) if $error;
}

sub resize {
    my ($in, $initial_size, $new_size) = @_;

    if (defined $new_size) {
	my $_wait = $in->wait_message(N("Please wait"), N("Resizing live system"));
	my $count = $new_size - $initial_size;
	run_program::raw({ timeout => 'never' }, 'dd', 'if=/dev/zero', 'of=' . $conf{LOOPBACK}, 'bs=1M', ($count > 0 ? ('seek=' . $initial_size, 'count=' . $count) : ('seek=' . $new_size, 'count=0')))
	    or die "dd failed";
    }

    my $_wait = $in->wait_message(N("Please wait"), N("Preparing live system"));
    {
      local $part->{mntpoint} = "/home";
      fs::format::part_raw($part);
    }
    fs::mount::part($part);

    run_program::run("unionctl", $conf{UNION}, "--mode", "ro", $conf{OLD_MOUNT})
	or die "unionctl ro failed";
    if (my @old_files = glob_($conf{OLD_MOUNT} . "/*")) {
	run_program::run("cp", "-a", @old_files, $conf{MOUNT})
	    or die "cp failed";
    }
    run_program::run("unionctl", $conf{UNION}, "--add", "--mode", "rw", $conf{MOUNT})
	or die "unionctl rw failed";

    addVarsInSh($conf_file, { DRAKLIVE_RESIZE => 'no' });

    run_program::run("sync");
}

my $resize = 0;
my $loopback_size = int((stat($conf{LOOPBACK}))[7] / 1024 / 1024);
my $new_size = $loopback_size;
my ($loopback_du) = split(' ', run_program::get_stdout("du", "-k", $conf{LOOPBACK}));
my (undef, $available_size) = MDK::Common::System::df(dirname($conf{LOOPBACK}));
$available_size = int(($available_size + $loopback_du) / 1024);

my $force_resize;
if ($loopback_size < $conf{MIN_SIZE}) {
    $force_resize = 1;
    $resize = 1;
}

my $icon = 'MandrivaOne-resize-icon.png';
$ugtk2::wm_icon = $icon;

my $in = 'interactive'->vnew('su');
$in->{no_Window_Manager} = 1;

$in->ask_from_({
    cancel => '',
    title => N("Live system resizing wizard"),
    messages => [ N("This live media uses %s MB to save system modifications and home directories.", $loopback_size) . N("You can resize this system space."), "", N("The remaining space will be used to share documents with other operating systems."), "" ],
    banner_title => N("Live system resizing wizard"),

    icon => $icon,
    	       },
	       [
		{
		    text => N("Resize live system (it will take some minutes)"),
		    type => "bool", val => \$resize,
		    disabled => sub { $force_resize },
		},
		{
		    label => N("System and home space (in MB)"),
		    type => "range",
		    val => \$new_size,
		    min => $conf{MIN_SIZE},
		    max => $available_size,
		    disabled => sub { !$resize },
		},
	       ]);

safe_resize($in, $loopback_size, $resize ? $new_size : undef);

$in->exit;
