'use strict';
const fs = require('fs');
const path = require('path');
const targetFile = path.join(__dirname, '../../frontend/js/labs/opticsbench.js');
let src = fs.readFileSync(targetFile, 'utf-8');

// Add ifSim to _obRedraw
const oldRedrawLine = "  if (_obMode === 'waves'      && diffrSim)   { diffrSim.draw(); diffrSim._updateHUD(); }";
const newRedrawLine = "  if (_obMode === 'waves'      && diffrSim)   { diffrSim.draw(); diffrSim._updateHUD(); }\r\n  if (_obMode === 'interf'     && ifSim)      { ifSim.draw(); }";

if (src.indexOf(newRedrawLine) < 0 && src.indexOf(oldRedrawLine) >= 0) {
  src = src.replace(oldRedrawLine, newRedrawLine);
  console.log('Added ifSim to _obRedraw');
} else {
  console.log('ifSim redraw already present or old line not found');
}

fs.writeFileSync(targetFile, src, 'utf-8');
console.log('Done. Size:', src.length);
