#!/usr/bin/env node

import { execFileSync } from "node:child_process";
import { dirname, join } from "node:path";

// --tools-version <ver> lets the user pin a specific version
const args = process.argv.slice(2);
let version = "latest";

const vIdx = args.indexOf("--tools-version");
if (vIdx !== -1) {
  version = args[vIdx + 1];
  // Remove --tools-version <ver> from forwarded args
  args.splice(vIdx, 2);
}

const allArgs = ["--yes", `@larksuite/openclaw-lark-tools@${version}`, ...args];

try {
  if (process.platform === "win32") {
    // On Windows, npx is a .cmd shim that can be broken or trigger
    // DEP0190. Bypass it entirely: run node with the npx-cli.js
    // script located next to the running node binary.
    const npxCli = join(
      dirname(process.execPath),
      "node_modules",
      "npm",
      "bin",
      "npx-cli.js",
    );
    execFileSync(process.execPath, [npxCli, ...allArgs], {
      stdio: "inherit",
      env: {
        ...process.env,
        NODE_OPTIONS: [
          process.env.NODE_OPTIONS,
          "--disable-warning=DEP0190",
        ]
          .filter(Boolean)
          .join(" "),
      },
    });
  } else {
    execFileSync("npx", allArgs, { stdio: "inherit" });
  }
} catch (error) {
  process.exit(error.status ?? 1);
}
