#!/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);
    }
}

$missing_extensions = array();
foreach (array('phar', 'spl', 'pcre', 'simplexml', 'libxml', 'xmlreader', 'sqlite3')
         as $ext) {
    if (!extension_loaded($ext)) {
        $missing_extensions[] = $ext;
    }
}
if ($missing_extensions) {
    echo "You must compile PHP with the following extensions enabled:\n",
        implode(', ', $missing_extensions), "\n",
        "or install the necessary extensions for your distribution.\n";
    exit(1);
}
unset($missing_extensions);

// 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(dirname(__DIR__) . '/src');
    }
}

require_once $autoload;

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