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

define('LARAVEL_START', microtime(true));

// Auto-detect if PDO extensions are missing; reload with local php.ini if available
if (!extension_loaded('pdo_mysql') && !extension_loaded('pdo_sqlite')) {
    $localIni = __DIR__ . DIRECTORY_SEPARATOR . 'php.ini';
    if (file_exists($localIni) && empty($_SERVER['__HMIS_PHP_INI_RELOADED'])) {
        $_SERVER['__HMIS_PHP_INI_RELOADED'] = '1';
        putenv('__HMIS_PHP_INI_RELOADED=1');

        $phpBinary = PHP_BINARY;
        $args = array_slice($argv, 1);
        $escapedArgs = array_map('escapeshellarg', $args);

        $cmd = escapeshellarg($phpBinary) . ' -c ' . escapeshellarg($localIni) . ' ' . escapeshellarg(__FILE__) . ' ' . implode(' ', $escapedArgs);

        passthru($cmd, $exitCode);
        exit($exitCode);
    }
}

// Register The Auto Loader
if (file_exists(__DIR__.'/vendor/autoload.php')) {
    require __DIR__.'/vendor/autoload.php';
}

$app = require_once __DIR__.'/bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$status = $kernel->handle(
    $input = new Symfony\Component\Console\Input\ArgvInput,
    new Symfony\Component\Console\Output\ConsoleOutput
);

$kernel->terminate($input, $status);

exit($status);
