import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { applyReleaseManifest } from './release-manifest-lib.mjs';

const invokedPath = process.argv[1] ? resolve(process.argv[1]) : '';
if (invokedPath === fileURLToPath(import.meta.url)) {
  const [, , source, destination] = process.argv;
  if (!source || !destination) {
    console.error('Aufruf: node tools/apply-release-manifest.mjs <Release-Paket> <geklontes Zielrepository>');
    process.exit(64);
  }
  try {
    const result = applyReleaseManifest(source, destination);
    console.log(`Release-Manifest angewendet: ${result.copied} Payload-Dateien und ${result.controlCopied} Steuerdatei geprüft und kopiert; ${result.removed} explizite Löschungen.`);
  } catch (error) {
    console.error(`[FEHLER] Release-Manifest konnte nicht angewendet werden: ${error.message}`);
    process.exit(1);
  }
}
