#!/usr/bin/env php
<?php
if (version_compare(phpversion(), '5.3.1', '<')) {
    if (substr(phpversion(), 0, 5) != '5.3.1') {
        // this small hack is because of running RCs of 5.3.1
        echo "Pyrus requires PHP 5.3.1 or newer.\n";
        exit -1;
    }
}

foreach (array('phar', 'spl', 'pcre', 'simplexml', 'libxml', 'xmlreader', 'sqlite3')
         as $ext) {
    if (!extension_loaded($ext)) {
        echo "The $ext extension is required.\n"
             . "You must compile PHP with $ext enabled, "
             . "or install the necessary extension for your distribution.\n";
        exit -1;
    }
}

// Reject old libxml installations
// moved to version 2.6.20 so XMLReader::setSchema can be used.
if (version_compare(LIBXML_DOTTED_VERSION, '2.6.20', '<')) {
    echo "Pyrus requires libxml >= 2.6.20."
         . " Version detected: " . LIBXML_DOTTED_VERSION . "\n";
    exit -1;
}

$autoload = '@php_dir@/PEAR2/Autoload.php';

if (!file_exists($autoload)) {
    // We are not running from an install or .phar, probably a clone of the repo
    if (file_exists(__DIR__ . '/../autoload.php')) {
        // Aha, use this local config file
        $autoload = __DIR__ . '/../autoload.php';
    } else {
        // Just use the bundled dependencies in the vendor dir
        $autoload = __DIR__ . '/../vendor/php/PEAR2/Autoload.php';
        require_once $autoload;
        \PEAR2\Autoload::initialize(__DIR__.'/../src/');
    }
}

require_once $autoload;

$frontend = new \PEAR2\Pyrus\ScriptFrontend\Commands;
@array_shift($_SERVER['argv']);
$frontend->run($_SERVER['argv']);
