#!/usr/bin/env php
<?php
/**
 * We are heavily relying on the PEAR libraries which are not clean with regard
 * to E_STRICT.
 */
if (defined('E_DEPRECATED')) {
    error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
} else {
    error_reporting(E_ALL & ~E_STRICT);
}

require_once 'PEAR/Config.php';
require_once 'PEAR/Command/Channels.php';
require_once 'PEAR/Command/Install.php';
require_once 'PEAR/Frontend/CLI.php';

$framework = __DIR__ . '/../../framework';
require_once $framework . '/Exception/lib/Horde/Exception.php';
require_once $framework . '/Exception/lib/Horde/Exception/Pear.php';
require_once $framework . '/Pear/lib/Horde/Pear/Package/Xml.php';
require_once $framework . '/Pear/lib/Horde/Pear/Package/Xml/Factory.php';
require_once $framework . '/Pear/lib/Horde/Pear/Package/Dependencies.php';
require_once $framework . '/Pear/lib/Horde/Pear/Remote.php';
require_once $framework . '/Pear/lib/Horde/Pear/Rest.php';
require_once $framework . '/Pear/lib/Horde/Pear/Rest/Access.php';
require_once $framework . '/Pear/lib/Horde/Pear/Rest/Dependencies.php';
require_once $framework . '/Http/lib/Horde/Http.php';
require_once $framework . '/Http/lib/Horde/Http/Client.php';
require_once $framework . '/Http/lib/Horde/Http/Request/Factory.php';
require_once $framework . '/Http/lib/Horde/Http/Request/Base.php';
require_once $framework . '/Http/lib/Horde/Http/Request/Curl.php';
require_once $framework . '/Http/lib/Horde/Http/Request/Fopen.php';
require_once $framework . '/Http/lib/Horde/Http/Response/Base.php';
require_once $framework . '/Http/lib/Horde/Http/Response/Curl.php';
require_once $framework . '/Http/lib/Horde/Http/Response/Fopen.php';

$base = __DIR__ .'/../lib';
require_once $base . '/Components/Exception.php';
require_once $base . '/Components/Exception/Pear.php';
require_once $base . '/Components/Dependencies.php';
require_once $base . '/Components/Dependencies/Bootstrap.php';
require_once $base . '/Components/Component.php';
require_once $base . '/Components/Component/Base.php';
require_once $base . '/Components/Component/Dependency.php';
require_once $base . '/Components/Component/DependencyList.php';
require_once $base . '/Components/Component/Match.php';
require_once $base . '/Components/Component/Source.php';
require_once $base . '/Components/Component/Remote.php';
require_once $base . '/Components/Component/Resolver.php';
require_once $base . '/Components/Helper/Installer.php';
require_once $base . '/Components/Helper/Root.php';
require_once $base . '/Components/Pear/Environment.php';
require_once $base . '/Components/Pear/Package.php';

$path = __DIR__ . '/../../lib';
if (!file_exists($path)) {
    mkdir($path, 0777, true);
}
$environment = realpath($path);

$dependencies = new Components_Dependencies_Bootstrap();
$factory = $dependencies->getInstance('Components_Pear_Factory');
$options = $dependencies->getInstance('Components_Config')->getOptions();
$options['pearrc'] = $environment . '/pear.conf';
$options['allow_remote'] = true;
$options['symlink'] = true;
$options['instructions'] = array('ALL' => array('exclude'));
$component = $dependencies->getInstance('Components_Component_Factory')
    ->createSource(realpath(__DIR__ .'/../'));
$environment = $factory->createEnvironment($environment, $environment . '/pear.conf');
$environment->provideChannel('pear.horde.org', $options);
$environment->getPearConfig()->setChannels(array('pear.horde.org', true));
$environment->getPearConfig()->set('horde_dir', $base . '/horde', 'user', 'pear.horde.org');
Components_Exception_Pear::catchError($environment->getPearConfig()->store());
$installer = new Components_Helper_Installer($dependencies->getInstance('Components_Output'));
$installer->installTree($environment, $component, $options);

$php_ini = 'include_path = ".:' . realpath($path) . '/pear/php"';
file_put_contents(realpath($path) . '/php.ini', $php_ini);

$env = '#!/bin/sh

env PHPRC="' . realpath($path) . '" PATH="' . realpath($path) . '/pear:$PATH" PHP_PEAR_SYSCONF_DIR="' . realpath($path) . '" $SHELL';
file_put_contents(realpath($path) . '/devenv', $env);
chmod(realpath($path) . '/devenv', 0755);
symlink(realpath($path) . '/pear/horde-components', realpath($path) . '/pear/hc');
