const fs = require('fs');
const path = require('path');
const AdmZip = require('adm-zip');

const zip = new AdmZip();
const targetZipPath = path.join(__dirname, 'dist', 'mikrobil-cpanel-ready.zip');

if (fs.existsSync(targetZipPath)) {
  fs.unlinkSync(targetZipPath);
}

function addDirectoryToZip(localDirPath, zipSubPath) {
  const items = fs.readdirSync(localDirPath);
  for (const item of items) {
    const localItemPath = path.join(localDirPath, item);
    const itemRelativeZipPath = zipSubPath ? `${zipSubPath}/${item}` : item;
    const stat = fs.statSync(localItemPath);

    if (stat.isDirectory()) {
      zip.addFile(itemRelativeZipPath + '/', Buffer.alloc(0));
      addDirectoryToZip(localItemPath, itemRelativeZipPath);
    } else {
      if (item.endsWith('.zip') || item.includes('.zip.bak')) {
        continue;
      }
      const content = fs.readFileSync(localItemPath);
      zip.addFile(itemRelativeZipPath, content);
    }
  }
}

addDirectoryToZip(path.join(__dirname, 'dist'), '');
zip.writeZip(targetZipPath);
console.log('Successfully created mikrobil-cpanel-ready.zip!');
