<?php
/*
* Copyright (c) 2009-2010 ATMAIL. All rights reserved
* See http://atmail.com/license.php for license agreement
*/

/*
* Courier to dovecot migration script 
* Migration
*
* Author : John Contad 
* Usage : Migrates UIDL data of courier maildirs to dovecot
*/

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/usr/local/atmail/webmail/library/Atmail/CalDav');
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/usr/local/atmail/webmail/library');
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/usr/local/atmail/webmail/');

session_name('ATMAIL6');

define('APP_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);

require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
//$loader->registerNamespace('App_'); //for model namespacing
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);

//Load general functions
Zend_Loader::loadFile('Atmail/General.php', null, true);
Zend_Loader::loadFile('Atmail/RenderHelpers.php', null, true);
Zend_Loader::loadFile('Atmail/Exception.php', null, true);

$siteBaseUrl = substr($_SERVER["SCRIPT_NAME"],0,strpos($_SERVER["SCRIPT_NAME"],'index.php'));

Zend_Registry::set( 'siteBaseUrl', $siteBaseUrl );

// Get database configuration    
$dbConfig = new Zend_Config_Ini('config/dbconfig.ini', 'production');
Zend_Registry::set('dbConfig', $dbConfig);
$dbAdapter = dbAdapterFactory($dbConfig);
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter', $dbAdapter);

//Get main configuration from database
$config = new Atmail_Config_Mysql($dbConfig, true);

//$config->global['cacheEnabled'] = false;
$config->global['allowLocalPop3'] = true; //for testing/debugging
$config->global['allowLocalImap'] = true; //for testing/debugging 
Zend_Registry::set('config', $config);

$locale = new Zend_Locale();
Zend_Registry::set('Zend_Locale', $locale);


$maildir = $dbAdapter->fetchCol("select MailDir from Users");

foreach($maildir as $maildirs)
{
print "Converting $maildirs ... \n";
system("/usr/local/atmail/webmail/utilities/migration/courier-dovecot-migrate.pl --quiet --to-dovecot --recursive --convert $maildirs > /dev/null 2>&1");
}

print "Verifying permissions for mail directories ... \n";
system("chown -R atmail /usr/local/atmail/users");

