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

/**
 * This file is part of Galette (https://galette.eu).
 * SPDX-FileCopyrightText: Copyright © 2003-2026 The Galette Team
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

declare(strict_types=1);

use function Safe\define;
use function Safe\session_start;

if (!isset($basepath)) {
    if (file_exists('../galette/index.php')) {
        $basepath = '../galette/';
    } elseif (file_exists('galette/index.php')) {
        $basepath = 'galette/';
    } else {
        die('Unable to define GALETTE_BASE_PATH :\'(');
    }
}

\define('GALETTE_ENV', 'CLI'); //@phpstan-ignore theCodingMachineSafe.function (dependencies not loaded yet)
if (getenv('GALETTE_TESTS')) {
    require_once __DIR__ . '/../tests/test_env.inc.php';
}
\define('GALETTE_ROOT', \realpath(__DIR__ . '/../galette/') . '/'); //@phpstan-ignore theCodingMachineSafe.function, theCodingMachineSafe.function (dependencies not loaded yet)

//specific logfile for console, read back by includes/galette.inc.php through the
//scope of the require below, hence unused as far as the sniff can tell
$logfile = 'galette_cli'; // phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable

require_once $basepath . 'vendor/autoload.php';
require_once $basepath . 'includes/sys_config/versions.inc.php';

$args = $_SERVER['argv'] ?? [];
// Compiling locales only works on files; boot without database, like the installer
$installer = is_array($args) && in_array($args[1] ?? null, ['galette:install', 'galette:compile-locales'], strict: true);

// In test mode, GALETTE_CONFIG_PATH is already set to the test config directory,
// so skip the production config file check to allow all commands to be registered.
if (!getenv('GALETTE_TESTS') && ($installer || !file_exists($basepath . 'config/config.inc.php'))) {
    define('GALETTE_INSTALLER', value: true);
    $installer = true; //consider we're installing
} elseif ($installer) {
    define('GALETTE_INSTALLER', value: true);
}
require_once $basepath . 'includes/galette.inc.php';

session_start();
$session_name = 'galette_cli_' . str_replace('.', '_', GALETTE_VERSION);
$session = &$_SESSION['galette'][$session_name]; // phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable

/** @var \Galette\Core\Plugins $plugins */
$gapp = new \Galette\Core\SlimApp($plugins);
$app = $gapp->getApp();
$container = $app->getContainer();

$console = new \Galette\Console\GaletteApplication($basepath);
$console->init(
    $container,
    $app,
    $container->get(\Slim\Routing\RouteCollector::class)
);
$console->run();
