const fs = require('fs');
const path = require('path');

const root = path.join(__dirname, '../public/60sapi');
const tag = '<script src="/60sapi/ig-embed.js"></script>';

function walk(dir) {
  for (const f of fs.readdirSync(dir)) {
    const p = path.join(dir, f);
    if (fs.statSync(p).isDirectory()) walk(p);
    else if (f.endsWith('.html')) {
      let c = fs.readFileSync(p, 'utf8');
      if (c.includes('ig-embed.js')) continue;
      if (!c.includes('</head>')) {
        console.warn('skip', p);
        continue;
      }
      c = c.replace('</head>', `${tag}\n</head>`);
      fs.writeFileSync(p, c, 'utf8');
      console.log('ok', p);
    }
  }
}

walk(root);
