#!/usr/bin/env php
<?php
/**
 * This script iterates each directory and forces an install from the
 * package.xml file for each package.
 *
 * @category Horde
 * @package  tools
 */

/* Don't die if time limit exceeded. */
set_time_limit(0);

/* Console_Getopt is not E_STRICT. */
error_reporting(error_reporting() & ~E_STRICT);

/* Get any arguments. */
require_once 'Console/Getopt.php';
$args = Console_Getopt::readPHPArgv();
$options = Console_Getopt::getopt($args, 'b:d:c:p:', array('base-dir=', 'install-dir=', 'config=', 'packages='));
if (is_a($options, 'PEAR_Error')) {
    echo <<<USAGE
Usage: pear_batch_install [[-b|--base-dir] DIRECTORY]
                          [[-d|--install-dir] DIRECTORY]
                          [[-c|--config] CONFIGFILE]
                          [[-p|--packages] PACKAGE1,PACKAGE2[,...]]

USAGE;
    exit;
}

/* Set these options to empty by default. */
$config_file = $install_dir = '';

$dir = dirname(__DIR__);
foreach ($options[0] as $option) {
    switch ($option[0]) {
    case 'b':
    case '--base-dir':
        $dir = $option[1];
        break;

    case 'd':
    case '--install-dir':
        /* Alternate install directory requested. */
        $install_dir = ' -d php_dir=' . $option[1] .
                       ' -d test_dir=' . $option[1] . '/tests' .
                       ' -d doc_dir=' . $option[1] . '/doc' .
                       ' -d data_dir=' . $option[1] . '/data' .
                       ' -d bin_dir=' . $option[1] . '/bin';
        break;
    case 'c':
    case '--config':
        /* Alternate config file requested. */
        $config_file = ' -c ' . $option[1];
        break;
    case 'p':
    case '--packages':
        /* Only these specific packages will be installed. */
        $packages = explode(',', $option[1]);
    }
}

/* Check for the Horde channel. */
$channel_check = 'pear' . $config_file . ' list-channels';
$channels = shell_exec($channel_check);
if (strpos($channels, 'pear.horde.org') == false) {
    $channel_register = 'pear' . $config_file . ' channel-discover pear.horde.org';
    system($channel_register);

    /* Check again. */
    $channels = shell_exec($channel_check);
    if (strpos($channels, 'pear.horde.org') == false) {
        echo "\nFailed to register pear.horde.org; you must fix this before continuing.\n";
        exit(1);
    }

    echo "\n\n";
}

/* Overwrite old files, ignore dependancies (for ease of ordering),
 * upgrade if already installed, etc. */
$pear = 'pear' . $config_file . $install_dir . ' install --force --nodeps';

if (!empty($packages)) {
    /* Installing only specific packages. */
    foreach ($packages as $entry) {
        $package = $dir . '/' . $entry . '/' . 'package.xml';
        if (file_exists($package)) {
            echo "Installing $entry:\n";
            system("$pear \"$package\"");
            echo "\n\n";
        }
    }
} else {
    /* Installing everything. */
    $dh = opendir($dir);
    while (($entry = readdir($dh)) !== false) {
        if ($entry == '.' || $entry == '..' || !is_dir($dir . '/' . $entry)) {
            continue;
        }

        $package = $dir . '/' . $entry . '/' . 'package.xml';
        if (file_exists($package)) {
            echo "Installing $entry:\n";
            system("$pear \"$package\"");
            echo "\n\n";
        }
    }
    closedir($dh);
}
