#!/usr/bin/env node
'use strict';

// Thin launcher that puts the packaged `cielara-code` sidecar binary on PATH.
// The real binary lives at cielara/<platform>/cielara-code(.exe) — a path that
// varies by OS/arch — so npm can't symlink it directly as a `bin`. This shim
// resolves the right binary at runtime and execs it with full arg/stdio/exit
// passthrough.
//
// Note: this is a DIRECT line to the sidecar. It bypasses the `cielara` CLI's
// auth/license/telemetry wrapper (the `cielara build|query|enable|…` path). The
// sidecar still enforces its own login checks for gated operations.

const { spawnSync } = require('child_process');
const { getBinary } = require('../lib/cielara');

const bin = getBinary('cielara-code');
if (!bin) {
  process.stderr.write(
    'cielara-code: pre-compiled binary not found for this platform.\n' +
    '  Reinstall: npm install -g cielara@latest\n',
  );
  process.exit(1);
}

const result = spawnSync(bin, process.argv.slice(2), { stdio: 'inherit' });

if (result.error) {
  process.stderr.write(`cielara-code: ${result.error.message}\n`);
  process.exit(1);
}
if (result.signal) {
  process.kill(process.pid, result.signal);
}
process.exit(result.status ?? 0);
