#!/usr/bin/env node
"use strict";
/**
 * OCR Provenance MCP Server - CLI Entry Point
 *
 * This file serves as the unified bin entry point for the ocr-provenance-mcp package.
 * It routes management subcommands (install, start, stop, etc.) to the wrapper CLI,
 * and starts the MCP server when no subcommand is provided.
 *
 * Usage:
 *   npx -y ocr-provenance-mcp              # start MCP server (stdio)
 *   npx -y ocr-provenance-mcp install      # pull Docker image + register AI clients
 *   ocr-provenance-mcp                     # after npm install -g
 *
 * @module bin
 */
const MANAGEMENT_COMMANDS = new Set([
    'install', 'start', 'stop', 'restart', 'status', 'logs',
    'update', 'config', 'serve', 'setup', 'uninstall',
    '--help', '-h', '--version', '-v',
]);
const args = process.argv.slice(2);
if (args.length > 0 && MANAGEMENT_COMMANDS.has(args[0])) {
    // Management commands — delegate to wrapper CLI
    import('./wrapper/cli.js').then(({ runCli }) => runCli(args)).catch((err) => {
        process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}\n`);
        process.exit(1);
    });
}
else {
    // Default: Start MCP server. Three modes:
    //   1. Bare-metal (OCR_BARE_METAL=1 in config): local MCP server + companion services
    //   2. Docker container running: bridge stdin/stdout to HTTP /mcp endpoint
    //   3. No Docker, no config: local MCP server + companion services (development)
    //
    // Bare-metal check MUST come first. A Mac user may have Docker Desktop installed
    // but chose `install --mac` for native GPU (MPS). If we check Docker first,
    // we'd try to bridge to a non-existent container and fail.
    (async () => {
        // Check bare-metal mode FIRST — this is set by `install --mac` / `install --bare`
        let isBareMetal = false;
        try {
            const { existsSync, readFileSync } = await import('fs');
            const { join } = await import('path');
            const { homedir } = await import('os');
            const configPath = join(homedir(), '.ocr-provenance-mcp', 'wrapper.json');
            if (existsSync(configPath)) {
                const config = JSON.parse(readFileSync(configPath, 'utf-8'));
                isBareMetal = config?.extraEnv?.OCR_BARE_METAL === '1';
            }
        }
        catch {
            // Config not found or malformed — not bare-metal
        }
        if (isBareMetal) {
            // Bare-metal mode: start companion services + local MCP server directly.
            // No Docker involved — MPS/CUDA GPU accessed natively.
            const { startBareMetalServices } = await import('./bare-metal-services.js');
            startBareMetalServices();
            await import('./index.js');
            return;
        }
        // Check if Docker container is running
        let useDocker = false;
        try {
            const { execFileSync } = await import('child_process');
            const result = execFileSync('docker', [
                'inspect', '--format', '{{.State.Running}}', 'ocr-provenance-mcp',
            ], { encoding: 'utf-8', stdio: 'pipe', timeout: 3000 }).trim();
            useDocker = result === 'true';
        }
        catch {
            // Docker not available or container doesn't exist
        }
        if (useDocker) {
            const { runCli } = await import('./wrapper/cli.js');
            await runCli(['serve']);
        }
        else {
            // No bare-metal config, no Docker — start local MCP server directly (development mode)
            const { startBareMetalServices } = await import('./bare-metal-services.js');
            startBareMetalServices();
            await import('./index.js');
        }
    })().catch((err) => {
        process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}\n`);
        process.exit(1);
    });
}
//# sourceMappingURL=bin.js.map