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

const BINARY_NAME = 'lightningcss.android-arm64.node';

// Cek apakah script berjalan di platform dan arsitektur yang diinginkan
if (process.platform !== 'android' || process.arch !== 'arm64') {
  console.log('Skipping, not on android-arm64 platform.');
  process.exit(0); // Keluar dengan sukses jika tidak pada platform target
}

console.log('Running for android-arm64...');

const currentPath = process.cwd();
const modulPath = path.dirname(currentPath);
const sourceFile = path.join(currentPath, BINARY_NAME);
const targetPath = path.join(modulPath, 'lightningcss');
const targetFile = path.join(targetPath, BINARY_NAME);

// 1. Cek apakah file sumber ada
if (!fs.existsSync(sourceFile)) {
  console.error(`Error: Source binary not found at ${sourceFile}`);
  process.exit(1); // Keluar dengan error
}

// 2. Cek apakah paket lightningcss terinstal
if (!fs.existsSync(targetPath)) {
  console.error(`Error: lightningcss not found in node_modules at ${targetPath}`);
  process.exit(1); // Keluar dengan error
}

// 3. Salin file binary
try {
  fs.copyFileSync(sourceFile, targetFile);
  console.log(`Successfully copied binary from ${sourceFile} to ${targetFile}`);
  process.exit(0); // Keluar dengan sukses
} catch (err) {
  console.error(`Error: Failed to copy binary from ${sourceFile} to ${targetFile}`, err);
  process.exit(1); // Keluar dengan error
}
