#!/usr/bin/env node
'use strict';

/**
 * Copy prebuilt .node binaries from build/bindings/ into bindings/prebuilds/
 * so they are included when running `npm publish` from the bindings/ directory.
 *
 * Run from the repository root:
 *   node bindings/nodejs/copy-prebuilds.js
 */

const fs   = require('fs');
const path = require('path');

const srcDir  = path.join(__dirname, '..', '..', 'build', 'bindings');
const destDir = path.join(__dirname, 'prebuilds');

if (!fs.existsSync(srcDir)) {
    console.error(`Source directory not found: ${srcDir}`);
    console.error('Run cmake build first: mkdir build && cd build && cmake .. && make');
    process.exit(1);
}

if (!fs.existsSync(destDir)) {
    fs.mkdirSync(destDir, { recursive: true });
}

const files = fs.readdirSync(srcDir).filter(f => f.endsWith('.node'));

if (files.length === 0) {
    console.warn('No .node files found in', srcDir);
    process.exit(1);
}

let copied = 0;
for (const file of files) {
    const src  = path.join(srcDir, file);
    const dest = path.join(destDir, file);
    fs.copyFileSync(src, dest);
    copied++;
    console.log(`  ${file}`);
}

console.log(`\nCopied ${copied} prebuilt binaries to ${destDir}`);
