<?php

declare(strict_types=1);

$vendorDir = isset($argv[1]) ? rtrim((string) $argv[1], '/') : dirname(__DIR__) . '/vendor';
$autoload = $vendorDir . '/autoload.php';
$autoloadFiles = $vendorDir . '/composer/autoload_files.php';

if (!is_file($autoload)) {
    fwrite(STDERR, "Missing Composer autoload: {$autoload}\n");
    exit(1);
}

if (!is_file($autoloadFiles)) {
    exit(0);
}

$files = require $autoloadFiles;
if (!is_array($files)) {
    fwrite(STDERR, "Composer autoload files map did not return an array: {$autoloadFiles}\n");
    exit(1);
}

$missing = [];
foreach ($files as $file) {
    if (!is_string($file) || !is_file($file)) {
        $missing[] = is_scalar($file) ? (string) $file : 'unknown';
    }
}

if ($missing !== []) {
    fwrite(STDERR, "Composer vendor is incomplete; missing files:\n");
    foreach ($missing as $file) {
        fwrite(STDERR, " - {$file}\n");
    }
    exit(1);
}

exit(0);
