import { spawn } from "node:child_process";
import { mkdir, chown } from "node:fs/promises";

const cacheDir = String(process.env.MAP_CACHE_DIR || "").trim();
if (cacheDir) {
  await mkdir(cacheDir, { recursive: true });
  await chown(cacheDir, 1000, 1000);
}
if (process.getuid?.() === 0) {
  process.setgid(1000);
  process.setuid(1000);
}
const [command, ...args] = process.argv.slice(2);
if (!command) throw new Error("map_gateway_runtime_command_required");
const child = spawn(command, args, { stdio: "inherit" });
child.on("exit", (code, signal) => process.exitCode = code ?? (signal ? 1 : 0));
child.on("error", (error) => { throw error; });
