import { spawnSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";

const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");

function run(scriptRelativePath) {
  const scriptPath = path.join(rootDir, scriptRelativePath);
  const result = spawnSync("bun", [scriptPath], {
    cwd: rootDir,
    stdio: "inherit",
    env: process.env,
  });

  if (result.status !== 0) {
    process.exit(result.status ?? 1);
  }
}

function runBash(scriptRelativePath) {
  const scriptPath = path.join(rootDir, scriptRelativePath);
  const result = spawnSync("bash", [scriptPath], {
    cwd: rootDir,
    stdio: "inherit",
    env: process.env,
  });

  if (result.status !== 0) {
    process.exit(result.status ?? 1);
  }
}

run("scripts/write-build-info.mjs");
run("scripts/build-linux-binary.mjs");
runBash("scripts/build-linux-packages.sh");
