#!/usr/bin/env node

import crypto from "crypto";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const rootDir = path.resolve(__dirname, "..");
const keyDir = path.join(rootDir, "license-keys");
const privatePath = path.join(keyDir, "update-private.pem");
const publicPath = path.join(keyDir, "update-public.pem");

if (existsSync(privatePath) || existsSync(publicPath)) {
  console.error("Update key files already exist. Remove them manually before generating a new pair.");
  process.exit(1);
}

const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519");
mkdirSync(keyDir, { recursive: true });
writeFileSync(privatePath, privateKey.export({ type: "pkcs8", format: "pem" }));
writeFileSync(publicPath, publicKey.export({ type: "spki", format: "pem" }));

console.log(privatePath);
console.log(publicPath);
