#!/usr/bin/env php
<?php
/*
 * This file is part of XRL, a simple XML-RPC Library for PHP.
 *
 * Copyright (c) 2012, XRL Team. All rights reserved.
 * XRL is licensed under the 3-clause BSD License.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

if (version_compare(phpversion(), '5.6.0', '<')) {
    echo "XRL requires PHP 5.6.0 or newer." . PHP_EOL;
    exit -1;
}

$extensions = array(
    'phar',
    'xmlreader',
    'xmlwriter',
    'libxml',
    'simplexml',
    'spl',
    'reflection',
);

foreach ($extensions as $ext) {
    if (!extension_loaded($ext)) {
        echo "Extension $ext is required" . PHP_EOL;
        exit -1;
    }
}

@include_once(
    dirname(__DIR__) .
    DIRECTORY_SEPARATOR . 'vendor' .
    DIRECTORY_SEPARATOR . 'autoload.php'
);

$error = new \RuntimeException();
try {
    Phar::mapPhar();
    $base = 'phar://' . __FILE__;
} catch (\Exception $error) {
    $base = dirname(__DIR__);
}

$autoload = $base .
            DIRECTORY_SEPARATOR . 'src' .
            DIRECTORY_SEPARATOR . 'Autoload.php';
if (!class_exists('\\fpoirotte\\XRL\\Autoload')) {
    if (file_exists($autoload)) {
        require_once($autoload);
        \fpoirotte\XRL\Autoload::register();
    } else {
        echo "Cannot process the archive: " . $error->getMessage() . PHP_EOL;
        exit -1;
    }
}

$cli = new \fpoirotte\XRL\CLI();
die($cli->run($_SERVER['argv']));

__HALT_COMPILER();
