const path = require('path');
const fs = require('fs');
const os = require('os');
const { execSync } = require('child_process');

// This script packages the compiled binary into the prebuilds directory
// It should be run after building for each platform/arch combination

const platform = process.argv[2] || os.platform();
const arch = process.argv[3] || os.arch();

// Map Node.js arch to our naming convention
const archMap = {
  'x64': 'x64',
  'ia32': 'x86',
  'arm64': 'arm64',
  'arm': 'arm'
};

const normalizedArch = archMap[arch] || arch;
const platformArch = `${platform}-${normalizedArch}`;

const sourcePath = path.join(__dirname, '..', 'build', 'Release', 'HypertubeNodejsRuntimeAddon.node');
const targetDir = path.join(__dirname, '..', 'prebuilds', platformArch);
const targetPath = path.join(targetDir, 'HypertubeNodejsRuntimeAddon.node');

if (!fs.existsSync(sourcePath)) {
  console.error(`✗ Source binary not found: ${sourcePath}`);
  console.error(`  Please build the addon first using: npm run prebuild`);
  process.exit(1);
}

// Create target directory if it doesn't exist
if (!fs.existsSync(targetDir)) {
  fs.mkdirSync(targetDir, { recursive: true });
}

// Copy the binary
fs.copyFileSync(sourcePath, targetPath);
console.log(`✓ Packaged binary for ${platformArch}`);
console.log(`  From: ${sourcePath}`);
console.log(`  To: ${targetPath}`);

