const { execSync } = require('child_process');
const args = process.argv.slice(2);

let url = '';

args.forEach((arg, index) => {
    if (arg === '-u' || arg === '--url') {
        url = args[index + 1];
    } else if (arg === '-h' || arg === '--help') {
        console.log('Usage: npm run build -- [-u|--url <url>]');
        console.log('Options:');
        console.log('  -u, --url <url>  URL to use for the extension collector server');
        console.log('  -h, --help       Display this help message');
        process.exit(0);
    } else if (arg.startsWith('-')) {
        console.error(`Unrecognized argument: ${arg}`);
        process.exit(1);
    }
});

if (url) {
  console.log(`Building with URL: ${url}`);
  execSync(`webpack --env URL=${url}`, { stdio: 'inherit' });
} else {
  execSync('webpack', { stdio: 'inherit' });
}