#!/usr/bin/perl

use lib qw(/usr/lib/libDrakX);
use strict;
use warnings;
use standalone;
use wizards;
use interactive;
use common;

my %distrib = distrib();
my $windows_disk = get_windows_disk();
my @linux_users = list_users();
my @windows_users = list_windows_users($windows_disk);

my @windows_items;
my $linux_user;
my $windows_user;
my $files_migration_type;
my $bookmarks_migration_type;
my $mail_migration_type;
my $background_migration_type;

my @step_items = (
    "files", [ "My Documents", "My Music", "My Pictures" ],
    "bookmarks", [ "Internet Explorer", "Mozilla Firefox" ],
    "mail", [ "Outlook Express" ],
    "background", [ "Wallpaper" ],
);
my @steps;

my $icon = 'migrationtools-52';
$ugtk2::wm_icon = $icon;

my $in = 'interactive'->vnew('su');
my $wiz = wizards->new({
    name => N("Migration wizard"),
    pages => {
        welcome => {
            name => N("This wizard will help you to import Windows documents and settings in your %s distribution.", $distrib{system}) . "\n" . N("It allows two differents migration methods: you can either import all documents and settings by copying them, or share them between operating systems."),
            post => sub {
                $windows_disk or return 'no_windows';
                $linux_user = @linux_users == 1 && $linux_users[0];
                $windows_user = @windows_users == 1 && $windows_users[0];
                !$linux_user || !$windows_user ? 'users' : 'files';
            },
        },
        users => {
            name => N("Multiple users have been detected, please select a user in the list below."),
            data => [
                {
                    label => N("Windows user"),
                    type => 'combo',
                    val => \$windows_user,
                    list => \@windows_users,
                },
                {
                    label => N("Linux user"),
                    type => 'combo',
                    val => \$linux_user,
                    list => \@linux_users,
                },
            ],
            post => sub {
                @windows_items = list_windows_items($windows_disk, $windows_user);
                @steps = map { $_->[0] } grep { intersection(\@windows_items, $_->[1]) } group_by2(@step_items);
                @steps or return 'nothing';
                next_step();
            },
        },
        files => {
            name => N("You can migrate Windows documents to your home directory. Documents can be imported by copying them, or they can be shared with the other operating system"),
            data => [
                {
                    type => 'list',
                    val => \$files_migration_type,
                    list => [
                        N("Import documents (recommended)"),
                        N("Share documents"),
                        N("Skip step"),
                    ],
                },
            ],
            post => sub {
                my $_w = $in->wait_message('', N("Migration of documents in progress"));
                step_import('files');
                next_step();
            },
        },
        bookmarks => {
            name => N("You can migrate browser bookmarks."),
            data => [
                {
                    type => 'list',
                    val => \$bookmarks_migration_type,
                    list => [
                        N("Import bookmarks (recommended)"),
                        if_(member('Mozilla Firefox', @windows_items),
                            N("Share bookmarks")),
                        N("Skip step"),
                    ],
                },
            ],
            post => sub {
                my $_w = $in->wait_message('', N("Migration of bookmarks in progress"));
                step_import('bookmarks');
                next_step();
            },
        },
        mail => {
            name => N("You can migrate mail bookmarks."),
            data => [
                { type => 'list',
                  val => \$mail_migration_type,
                  list => [
                      N("Import mail (recommended)"),
                      if_(member('Mozilla Thunderbird', @windows_items),
                          N("Share mail")),
                      N("Skip step"),
                  ],
              },
            ],
            post => sub {
                my $_w = $in->wait_message('', N("Migration of mail in progress"));
                step_import('mail');
                next_step();
            },
        },
        background => {
            name => N("You can migrate your desktop background."),
            data => [
                { type => 'list',
                  val => \$background_migration_type,
                  list => [
                      N("Use Mandriva background"),
                      N("Import background"),
                  ],
              },
            ],
            post => sub {
                my $_w = $in->wait_message('', N("Migration of background in progress"));
                step_import('background');
                next_step();
            },
        },
        end => {
            name => N("Congratulations, your migration is now completed!"),
            end => 1,
        },
        no_windows => {
            name => N("No Windows installation has been detected."),
            end => 1,
        },
        nothing => {
            name => N("No documents and settings have been detected."),
            end => 1,
        },
    },
});

$wiz->process($in);

sub next_step { shift(@steps) || 'end' }

sub step_import {
    my ($stepname) = @_;
    my $step = find { $_->[0] eq $stepname } group_by2(@step_items);
    my @targets = intersection(\@windows_items, $step->[1]);
    import_target($windows_disk, $windows_user, $linux_user, $_) foreach @targets;
}

sub get_windows_disk {
    require fs;
    require fs::type;

    my $fstab = [ fs::read_fstab($::prefix, '/etc/fstab', '') ];
    fs::merge_info_from_mtab($fstab);

    my @win_devices = grep { fs::type::isFat_or_NTFS($_) && fs::type::isMounted($_) } @$fstab;
    find { -e "$_/WINDOWS/system32/config/software" } map { $_->{mntpoint} } @win_devices;
}

sub list_windows_users {
    my ($win_prefix) = @_;
    my @users = chomp_(split(/,\s*/, run_program::get_stdout("ma-search-users", "windowsxp", $win_prefix)));
    my ($standard, $admin) = partition { $_ ne "Administrator" } @users;
    sort(@$standard), @$admin;
}

sub list_windows_items {
    my ($win_prefix, $win_user) = @_;
    chomp_(split(/,\s*/, run_program::get_stdout("ma-search-items",
                                                 "--ostype=windowsxp",
                                                 "--path=$win_prefix",
                                                 "--user=$win_user")));
}

sub import_target {
    my ($win_prefix, $win_user, $linux_user, $target) = @_;
    my $lc_target = lc($_);
    $lc_target =~ s/\s//g;
    run_program::raw({ timeout => "never" },
                     "ma-import",
                     "--ostype=windowsxp",
                     "--fromuser=$win_user",
                     "--frompath=$win_prefix",
                     "--touser=$linux_user",
                     "--topath=/",
                     "--target=$lc_target");
}
