<?php
/**
 * Migrates users, preferences and contacts from SquirrelMail
 * into Atmail
 *
 * Usage:
 * php migrate-squirrelmail.php [options]
 *     Options:
 *         -c [path-to-squirrelmail-config-file] Only -c OR -d can be specified
 *         -d [path-to-squirrelmail-data-directory] Only -c OR -d can be specified
 *         -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>
 */     
 
$usage = <<<EOF
 * Usage:
 * php migrate-squirrelmail.php [options]
 *     Options:
 *         -c [path-to-squirrelmail-config-file] Only -c OR -d can be specified
 *         -d [path-to-squirrelmail-data-directory] Only -c OR -d can be specified
 *         -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)
 
EOF;
 
require_once(dirname(dirname(__FILE__)) . "/nfc-bootloader.php");

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

if (!isset($opt["c"]) && !isset($opt["d"])) {
    die("You must specify either -c OR -d\n\n$usage\n");
}

if (isset($opt["c"]) && (empty($opt["c"]) || !is_readable($opt["c"]))) {
    die("If you specify -c you must pass the path to the squirrelmail config file: -c [path-to-config]\n\n$usage\n");
}

if (isset($opt["d"]) && (empty($opt["d"]) || !is_dir($opt["d"]))) {
    die("If you specify -d you must pass the path to the squirrelmail data directory: -d [path-to-data-dir]\n\n$usage\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\n$usage\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\n$usage\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\n$usage\n");
    }
}

if (isset($opt["c"])) {
    $adapter = new Atmail_Migrate_Adapter_Squirrelmail($opt['c'], $user);
} else {
    $adapter = new Atmail_Migrate_Adapter_Squirrelmail($opt['d'], $user, true);
}

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

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