#!/usr/bin/env php
<?php

/*
 * This file is part of the Kimai 2 - Remote Console.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

if (false === in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
    fwrite(STDERR, 'Warning: The console should be invoked via the CLI version of PHP, not the '.\PHP_SAPI.' SAPI'.\PHP_EOL);
    exit(1);
}

foreach (['json', 'iconv', 'mbstring', 'curl'] as $extension) {
    if (!extension_loaded($extension)) {
        fwrite(STDERR, sprintf("PHP extension ext-%s is missing from your system. Install or enable it.\n", $extension));
        exit(1);
    }
}
unset($extension);

setlocale(LC_ALL, 'C');

// -------------------------- AUTOLOAD --------------------------
$require = true;
if (class_exists('Phar')) {
    // Maybe this file is used as phar-stub? Let's try!
    try {
        Phar::mapPhar('kimai.phar');
        require_once 'phar://kimai.phar/vendor/autoload.php';
        $require = false;
    } catch (PharException $e) {
    }
}
if ($require) {
    // OK, it's not, let give Composer autoloader a try!
    $possibleFiles = [__DIR__.'/../../../autoload.php', __DIR__.'/../../autoload.php', __DIR__.'/../vendor/autoload.php'];
    $file = null;
    foreach ($possibleFiles as $possibleFile) {
        if (file_exists($possibleFile)) {
            $file = $possibleFile;
            break;
        }
    }

    if (null === $file) {
        fwrite(STDERR, "Unable to locate autoload.php file, you must set up the project dependencies using `composer install`.\n");
        exit(1);
    }

    require_once $file;
    unset($possibleFiles, $possibleFile, $file);
}
unset($require);
// -------------------------- AUTOLOAD --------------------------

error_reporting(E_ALL & ~E_DEPRECATED); // swagger generates php 5.5 code, which triggers deprecations on PHP 8
set_time_limit(0);

use KimaiConsole\Application;

if (function_exists('ini_set')) {
    @ini_set('display_errors', 1);
    $memoryInBytes = function ($value) {
        $unit = strtolower(substr($value, -1, 1));
        $value = (int) $value;
        switch($unit) {
            case 'g':
                $value *= 1024;
            // no break (cumulative multiplier)
            case 'm':
                $value *= 1024;
            // no break (cumulative multiplier)
            case 'k':
                $value *= 1024;
        }
        return $value;
    };
    $memoryLimit = trim(ini_get('memory_limit'));
    // Increase memory_limit if it is lower than 1.5GB
    if ($memoryLimit != -1 && $memoryInBytes($memoryLimit) < 1024 * 1024 * 1536) {
        @ini_set('memory_limit', '1536M');
    }
    // Set user defined memory limit
    if ($memoryLimit = getenv('KIMAI_MEMORY_LIMIT')) {
        @ini_set('memory_limit', $memoryLimit);
    }
    unset($memoryInBytes, $memoryLimit);
}
putenv('KIMAI_BINARY='.realpath($_SERVER['argv'][0]));

$application = new Application();
$application->run();

__HALT_COMPILER();
