<?php
/**
 * Migrates users, contacts and groups from Roundcube
 * into Atmail
 *
 * Usage:
 * php migrate-roundcube.php [options] -c <path to roundcube db.inc.php config file>
 *     Options:
 *         -l [email-address]   Migrate users at login, i.e. Migrate their data after they have logged into Atmail
 *              for the first time (requires a separate script/plugin that detects first login and calls this script).
                If this option is used then -s, -w and -p are ignored and just the settings and contacts are migrated. 
 *         -s   Migrate to Atmail server mode, create domains and users on the Atmail system if they do not already exist (this option requires -p)
 *         -w   Migrate to Atmail Webmail, do not create domains but just create users
 *         -p [default-password] Specifies the default password to use for creating users on Atmail (only used if -s is specified)
 *
 * @author Brad Kowalczyk <brad@staff.atmail.com>
 */     
 
require_once(dirname(dirname(__FILE__)) . "/nfc-bootloader.php");

$opt = getopt("c:l:swp");

if (!isset($opt["c"]) || empty($opt["c"]) || !is_readable($opt["c"])) {
    die("You must pass the path to the roundcube database config file with -c [path-to-config]\n");
}

if (isset($opt['l'])) {
    if (empty($opt['l'])) {
        die("When using -l option you must pass a user account you want migrated: -l [user-account]\n");
    }
    $mode = "login";
    $user = $opt['l'];
} elseif (isset($opt['s'])) {
    $mode = "server";
} elseif (isset($opt['w'])) {
    $mode = "webmail";
}

if (!isset($mode)) {
    die("You must specify a migration mode, either -l (migrate a user at login), -s (migrate to server mode) or -w (migrate to webmail only mode)\n");
}

if ($mode == "server") {
    if (!isset($opt["p"])) {
        die("If you are migrating to Atmail server mode then you are required to pass a default password with -p [password]\n");
    }
}

$adapter = new Atmail_Migrate_Adapter_Roundcube($opt['c'], $user);
if ($mode == "server") {
    $adapter->setDefaultPass($opt['p']);
}

$migrate = new Atmail_Migrate($adapter, $mode);
$migrate->start();
