#!/usr/local/bin/php
<?php
/**
 * This script manages themes.
 *
 * Copyright 2010-2013 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (LGPL-2). If you
 * did not receive this file, see http://www.horde.org/licenses/lgpl.
 *
 * @author   Michael Slusarz <slusarz@horde.org>
 * @category Horde
 * @license  http://www.horde.org/licenses/lgpl LGPL-2
 * @package  Horde
 */

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

$c = new Console_Getopt();
$argv = $c->readPHPArgv();
array_shift($argv);
$options = $c->getopt2($argv, '', array('expirecache'));
if (PEAR::isError($options)) {
    $cli->writeln($cli->red("ERROR: Invalid arguments."));
    $cli->writeln();
    _usage();
}

foreach ($options[0] as $val) {
    switch ($val[0]) {
    case '--expirecache':
        if ($cli->prompt($cli->red('Are you sure you want to expire all cached themes?'), array('y' => 'Yes', 'n' => 'No'), 'n') == 'y') {
            $tcache = $injector->getInstance('Horde_Core_Factory_ThemesCache');
            $tlist = array_keys(Horde_Themes::themeList());

            $cli->writeln();

            foreach ($registry->listAllApps() as $app) {
                foreach ($tlist as $theme) {
                    try {
                        if ($tcache->expireCache($app, $theme)) {
                            $cli->message('Cache entry expired [APP: ' . $app . '; THEME: ' . $theme . ']');
                        }
                    } catch (Horde_Exception $e) {
                        $cli->message('Could not expire cache entry [APP: ' . $app . '; THEME: ' . $theme . ']', 'cli.warning');
                    }
                }
            }
        }
        exit;
    }
}

_usage();


function _usage()
{
    global $cli;

    $cli->writeln($cli->bold('Usage: themes [--expirecache]'));
    $cli->writeln();
    $cli->writeln($cli->indent('--expirecache   Expire all cache entries'));

    exit;
}
