<?php

declare(strict_types=1);

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2014-2023, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

require_once dirname(__DIR__) . '/vendor/autoload.php';
$baseDir = dirname(__DIR__);

use Vpn\Portal\Cfg\Config;
use Vpn\Portal\ConfigCheck;

try {
    $config = Config::fromFile($baseDir . '/config/config.php');

    $configStatus = ConfigCheck::verify($config);
    $globalProblems = $configStatus['global_problems'];
    $profileProblems = $configStatus['profile_problems'];

    $hasProblems = false;
    foreach ($globalProblems as $p) {
        $hasProblems = true;
        echo sprintf('[SERVER]: %s', $p) . PHP_EOL;
    }
    foreach ($profileProblems as $profileId => $problemList) {
        foreach ($problemList as $p) {
            $hasProblems = true;
            echo sprintf('[PROFILE=%s]: %s', $profileId, $p) . PHP_EOL;
        }
    }

    // if any problems were found we want to NOT have a 0 exit status
    if ($hasProblems) {
        exit(1);
    }
} catch (Exception $e) {
    echo sprintf('ERROR: %s', $e->getMessage()) . \PHP_EOL;

    exit(1);
}
