#!/usr/bin/env node

const { spawn } = require("child_process");
const path = require("path");

const appDir = __dirname;
const nextBin = path.join(appDir, "node_modules", ".bin", "next");

console.log("==================================================");
printGlow("Starting Jukaza Web Server...");
console.log(`Directory: ${appDir}`);
console.log("==================================================");

// Determine port (default to 20128)
const portIndex = process.argv.indexOf("-p");
const port = portIndex !== -1 && process.argv[portIndex + 1]
  ? process.argv[portIndex + 1]
  : "20128";

// On Windows, Next's executable in .bin is next.cmd
const cmd = process.platform === "win32" ? `${nextBin}.cmd` : nextBin;

const child = spawn(cmd, ["start", "-p", port], {
  cwd: appDir,
  stdio: "inherit",
  shell: true
});

child.on("exit", (code) => {
  process.exit(code || 0);
});

function printGlow(text) {
  // Bold magenta/purple-esque glow terminal print
  console.log(`\x1b[1m\x1b[35m${text}\x1b[0m`);
}
