const fs = require('fs');
let html = fs.readFileSync('index.html', 'utf8');
const now = Date.now();

// Switch back to db.js
if (html.includes('src="db_server.js')) {
  html = html.replace(/src="db_server\.js\?v=[^"]*"/, 'src="db.js?v=' + now + '"');
  console.log('Switched back to db.js');
} else if (!html.includes('src="db.js')) {
  console.log('WARNING: neither db.js nor db_server.js found');
} else {
  console.log('Already using db.js');
}

// Update cache versions
html = html.replace(/app_final\.js\?v=[^"]+/, 'app_final.js?v=' + now);
html = html.replace(/store_module\.js\?v=[^"]+/, 'store_module.js?v=' + now);
html = html.replace(/vouchers_returns\.js\?v=[^"]+/, 'vouchers_returns.js?v=' + now);

fs.writeFileSync('index.html', html);
console.log('Done. Version: ' + now);
