<?php

include dirname(__FILE__) . '/../nfc-bootloader.php';
include 'Atmail/Dav/Migrator.php';
include 'Atmail/Update/Utils.php';

sabmig_displayWelcome();

/* defaults */
$dryRun = false;
$logLevel = Atmail_Dav_Migrator::LOG_ERROR;

$arguments = $argv;
array_shift($arguments);

foreach($arguments as $key=>$arg) {
    if (strpos($arg,'-')===0) {

        switch($arg) {

            case '--dry-run' :
                $dryRun = true;
            break;
            case '-v' :
                $logLevel = Atmail_Dav_Migrator::LOG_INFO;
                break;
            case '-vv' :
                $logLevel = Atmail_Dav_Migrator::LOG_DEBUG;
                break;
            default :
            {
            	sabmig_displayUsage();
                die('Unknown option: ' . $arg . "\n");
			}
        }

    } else {
    }

}

$db = Zend_Registry::get('dbAdapter');
$db->getConnection();

try
{
	$opt = new Atmail_Dav_Optimizer($dataDir);
	$opt->dryRun = $dryRun;
	$opt->logLevel = $logLevel;
	$opt->start();
}
catch(Exception $e)
{
	$deathHints = "\n";
	$deathMessage = $e->getMessage();
	if($deathMessage == "could not find driver")
	{
		$deathHints = "Please ensure that the sqlite PDO driver for php is available.\n";
	}
	die("Exception caught. \n\nError: "  . $e->getMessage() . "\n" . $deathHints);
}

echo "\n\nCompleted.\n\n";

function sabmig_displayWelcome()
{
	echo "\n\033[1;32mWelcome to the Atmail calendar optimization script\033[0;39m\n";
	echo "-------------------------------------------------\n";
}

function sabmig_displayUsage()
{
	echo "\n";
	echo "Usage : " . $_SERVER["SCRIPT_FILENAME"] . " [-v/-vv/--dry-run]\n\n";
	echo "--dry-run : Do not actually perform writes.\n";
	echo "-v[v] : verbose or very verbose debug output.\n";
	echo "data_directory : Specify darwin data directory.\n";
	echo "\n";
}
