<?php

declare(strict_types=1);

$options = getopt('', ['target::']);
require __DIR__ . DIRECTORY_SEPARATOR . 'diar6b4-r2-lib.php';

try {
    diarR2Bootstrap($options);
    diarR2VerifyControlDatabase();
    $matrix = diarR2RoleMatrix();
    echo 'DIAR_6B_4_R2_ROLE_MATRIX|PASS|ROLES=' . count((array)$matrix['roles'])
        . '|REQUIRED_GRANTS=' . (int)$matrix['required_grants']
        . '|FORBIDDEN_ALLOWS=0|SERVICE_GUARDS=STATIC_TESTED|WRITES=NONE' . PHP_EOL;
    echo json_encode($matrix, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
    exit(0);
} catch (Throwable $error) {
    fwrite(STDERR, 'DIAR_6B_4_R2_ROLE_MATRIX|FAIL|ERROR=' . $error->getMessage() . PHP_EOL);
    exit(23);
}
