#!/usr/bin/env node

import { chmodSync, statSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

const chmodSupportedPlatforms = new Set([
  "aix",
  "android",
  "darwin",
  "freebsd",
  "linux",
  "netbsd",
  "openbsd",
  "sunos",
]);

if (!chmodSupportedPlatforms.has(process.platform)) {
  process.exit(0);
}

const packageRoot = dirname(dirname(fileURLToPath(import.meta.url)));
const entryFile = join(packageRoot, "dist", "index.js");

try {
  const mode = statSync(entryFile).mode;
  chmodSync(entryFile, mode | 0o755);
} catch (error) {
  const reason = error instanceof Error ? error.message : String(error);
  console.warn(`[aws-runtime-bridge] Unable to mark CLI entry executable: ${reason}`);
}
