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

declare(strict_types=1);

$options = getopt('', ['bytes:', 'env-line', 'help']);
if (isset($options['help'])) {
    fwrite(STDOUT, "Usage: generate-bearer-token.php [--bytes 32] [--env-line]\n");
    exit(0);
}

$bytes = isset($options['bytes']) ? (int) $options['bytes'] : 32;
if ($bytes < 16) {
    fwrite(STDERR, "--bytes must be at least 16.\n");
    exit(1);
}

$token = rtrim(strtr(base64_encode(random_bytes($bytes)), '+/', '-_'), '=');
if (isset($options['env-line'])) {
    fwrite(STDOUT, 'MCP_SERVER_TOKEN=' . $token . PHP_EOL);
    exit(0);
}

fwrite(STDOUT, $token . PHP_EOL);
