import { resolve } from "node:path";
import { assertKnownArgs, optionalArg, parseArgs, reportCliError, requiredArg } from "./cli.ts";
import { verifyReleaseBundle } from "./release-bundle.ts";

async function main(): Promise<void> {
  const args = parseArgs(process.argv.slice(2));
  assertKnownArgs(args, ["directory", "public-key", "schema-ref", "require-signature"]);
  const publicKeyPath = optionalArg(args, "public-key");
  const expectedSchemaRef = optionalArg(args, "schema-ref");
  const manifest = await verifyReleaseBundle({
    releaseDirectory: resolve(requiredArg(args, "directory")),
    ...(publicKeyPath === undefined ? {} : { publicKeyPath: resolve(publicKeyPath) }),
    ...(expectedSchemaRef === undefined ? {} : { expectedSchemaRef }),
    requireSignature: args.get("require-signature") === true,
  });
  process.stdout.write(
    `OK: bundle ${manifest.payload.releaseId}, signature, artefacts, schémas et allowlist SHA-256 valides.\n`,
  );
}

main().catch(reportCliError);
