#!/usr/bin/env php
<?php
/**
 * @copyright Bluz PHP Team
 * @link https://github.com/bluzphp/bluzman
 */

if (PHP_SAPI !== 'cli') {
    die('Must run from command line');
}
if (stripos(PHP_OS, 'WIN') === 0) {
    // sorry windows lovers
    die('Windows is not supported');
}

// Root path, double level up
// UP to root from "/application/vendor/bluzphp/bluzman/bin"
$root = realpath(dirname(__DIR__, 4));

// Definitions
const DS = DIRECTORY_SEPARATOR;

defined('PATH_ROOT') ? : define('PATH_ROOT', $root);
defined('PATH_APPLICATION') ? : define('PATH_APPLICATION', $root . '/application');
defined('PATH_DATA') ? : define('PATH_DATA', $root . '/data');
defined('PATH_VENDOR') ? : define('PATH_VENDOR', $root . '/vendor');
defined('PATH_PUBLIC') ? : define('PATH_PUBLIC', $root . '/public');

// Environment
$env = getenv('BLUZ_ENV') ?: 'production';
define('BLUZ_ENV', $env);

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 0);
ini_set('html_errors', 0);

try {
    // Get project info from composer.json
    $composerConfig = json_decode(
        file_get_contents(PATH_VENDOR . DS . 'bluzphp' . DS . 'bluzman' . DS . 'composer.json'),
        true
    );
    require_once PATH_VENDOR . '/autoload.php';

    $application = new Bluzman\Application\Application(
        $composerConfig['description'],
        $composerConfig['version'] ?? 'dev'
    );
    $application->init();
    $application->run();
} catch (\Throwable $e) {
    echo "Error #{$e->getCode()}:\n\n";
    echo "\t{$e->getFile()}:{$e->getLine()}\n";
    echo "\t{$e->getMessage()}\n";
    echo "\n";
}
