#!/usr/bin/env node

const PLATFORMS = {
    "linux-x64": "@nizm-cli/linux-x64",
    "linux-arm64": "@nizm-cli/linux-arm64",
    "darwin-x64": "@nizm-cli/darwin-x64",
    "darwin-arm64": "@nizm-cli/darwin-arm64",
    "win32-x64": "@nizm-cli/win32-x64",
};

const key = `${process.platform}-${process.arch}`;
const pkg = PLATFORMS[key];

if (!pkg) {
    console.error(`nizm: unsupported platform ${key}`);
    process.exit(1);
}

const ext = process.platform === "win32" ? ".exe" : "";
let bin;
try {
    bin = require.resolve(`${pkg}/nizm${ext}`, { paths: [__dirname] });
} catch {
    console.error(
        `nizm: package ${pkg} not installed — reinstall with npm install -g nizm-cli`,
    );
    process.exit(1);
}

const result = require("child_process").spawnSync(bin, process.argv.slice(2), {
    shell: false,
    stdio: "inherit",
});

if (result.error) throw result.error;
process.exitCode = result.status ?? 1;
