const fs = require('fs');
const path = require('path');
const filePath = process.argv[2] || path.join(__dirname, 'index.html');
let html = fs.readFileSync(filePath, 'utf8');
html = html.replace(/\u2019/g, "'");

// Remove first (duplicate) gallery block so we have Gallery, Products, Team; then we swap Gallery and Products
const fakeStart = html.indexOf('  <section class="gallery" id="gallery">\n    <div class="container container--relative">');
const fakeEnd = html.indexOf('  </section>\n\n  <section class="gallery" id="gallery">', fakeStart + 10);
if (fakeStart !== -1 && fakeEnd !== -1 && fakeEnd > fakeStart) {
  const endMarker = '  </section>\n\n  <section class="gallery" id="gallery">';
  html = html.slice(0, fakeStart) + '  <section class="gallery" id="gallery">' + html.slice(fakeEnd + endMarker.length);
}

const gs = html.indexOf('  <section class="gallery" id="gallery">');
const ps = html.indexOf('  <section class="products" id="products"');
if (gs === -1 || ps === -1) {
  process.stderr.write('sections not found gs=' + gs + ' ps=' + ps);
  process.exit(1);
}
const ge = html.indexOf('  </section>', gs) + 14;
const pe = html.indexOf('  </section>', ps) + 14;
const out = html.slice(0, gs) + html.slice(ps, pe) + '\n\n' + html.slice(gs, ge) + '\n\n' + html.slice(pe);
fs.writeFileSync(filePath, out);
