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

const platform = os.platform();
const arch = os.arch();

const key = `${platform}-${arch}`;

const keyStore = {
  'darwin-arm64': 'aarch64-apple-darwin',
  // 'darwin-x64': 'x86_64-apple-darwin',
  'linux-arm64': 'aarch64-unknown-linux-gnu',
  'linux-x64': 'x86_64-unknown-linux-gnu',
  'win32-x64': 'x86_64-pc-windows-gnu',
  'win32-arm64': 'aarch64-pc-windows-gnu',
};

const sourceDir = path.join(__dirname, '../target', keyStore[key], 'release');
const sourceFile = path.join(sourceDir, 'dpdm');

const targetDir = path.join(__dirname, '../target', 'release');
const targetFile = path.join(
  targetDir,
  platform === 'win32' ? 'dpdm.exe' : 'dpdm',
);

if (!fs.existsSync(targetDir)) {
  fs.mkdirSync(targetDir, { recursive: true });
}
fs.copyFile(sourceFile, targetFile, (err) => {
  if (err) {
    console.error('Copy failed:', err);
  }
});
