#!/usr/local/bin/php
<?php
/**
 * This script migrates Ingo's share data from the SQL Horde_Share
 * driver to the next-generation SQL Horde_Share driver.
 *
 * It is supposed to run at any time after migrating Ingo to the latest DB
 * schema version. The schema migration already migrates the data once, but
 * this script can be used to migrate the data again, e.g. if starting to use
 * the NG driver at a later time.
 *
 * Copyright 2012-2013 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL).  If you
 * did not receive this file, see http://www.horde.org/licenses/apache.
 *
 * @category Horde
 * @license  http://www.horde.org/licenses/apache ASL
 * @package  Ingo
 */

if (file_exists(__DIR__ . '/../../ingo/lib/Application.php')) {
    $baseDir = __DIR__ . '/../';
} else {
    require_once 'PEAR/Config.php';
    $baseDir = PEAR_Config::singleton()
        ->get('horde_dir', null, 'pear.horde.org') . '/ingo/';
}
require_once $baseDir . 'lib/Application.php';
Horde_Registry::appInit('ingo', array('cli' => true));

require_once $baseDir . 'migration/3_ingo_upgrade_sqlng.php';

$db = $injector->getInstance('Horde_Db_Adapter');
$migration = new IngoUpgradeSqlng($db);

$delete = $cli->prompt('Delete existing shares from the NEW backend before migrating the OLD backend? This should be done to avoid duplicate entries or primary key collisions in the storage backend from earlier migrations.', array('y' => 'Yes', 'n' => 'No'), 'n');

if ($delete == 'y' || $delete == 'Y') {
    $db->delete('DELETE FROM ingo_sharesng');
    $db->delete('DELETE FROM ingo_sharesng_users');
    $db->delete('DELETE FROM ingo_sharesng_groups');
}

$migration->dataUp();
