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

const FILE_SUFFIX = '.user.js';
const DIST_DIR = 'dist';

const metadata = {
  'extended-player-profile': `// ==UserScript==
// @name         Extended player profile
// @version      1.2.0
// @description  Adds additional info and actions on a player overview.
// @author       Dawid Wysokiński - Kichiyaki - contact@twhelp.app
// @match        https://*/game.php?*screen=info_player*
// @downloadURL  ${process.env.PUBLIC_URL}/extended-player-profile.user.js
// @updateURL    ${process.env.PUBLIC_URL}/extended-player-profile.user.js
// @icon         https://www.google.com/s2/favicons?domain=plemiona.pl
// @grant        none
// @run-at       document-end
// ==/UserScript==`,
  'extended-map-popup': `// ==UserScript==
// @name         Extended map popup
// @version      1.1.0
// @description  Extends the map popup with additional info.
// @author       Dawid Wysokiński - Kichiyaki - contact@twhelp.app
// @match        https://*/game.php?*screen=map*
// @downloadURL  ${process.env.PUBLIC_URL}/extended-map-popup.user.js
// @updateURL    ${process.env.PUBLIC_URL}/extended-map-popup.user.js
// @icon         https://www.google.com/s2/favicons?domain=plemiona.pl
// @grant        none
// @run-at       document-end
// ==/UserScript==`,
  'extended-village-profile': `// ==UserScript==
// @name         Extended village profile
// @version      1.1.0
// @description  Adds additional info and actions on a village overview.
// @author       Dawid Wysokiński - Kichiyaki - contact@twhelp.app
// @match        https://*/game.php?*screen=info_village*
// @downloadURL  ${process.env.PUBLIC_URL}/extended-village-profile.user.js
// @updateURL    ${process.env.PUBLIC_URL}/extended-village-profile.user.js
// @icon         https://www.google.com/s2/favicons?domain=plemiona.pl
// @grant        none
// @run-at       document-end
// ==/UserScript==`,
  'extended-tribe-profile': `// ==UserScript==
// @name         Extended tribe profile
// @version      1.1.0
// @description  Adds additional info and actions on a tribe overview.
// @author       Dawid Wysokiński - Kichiyaki - contact@twhelp.app
// @match        https://*/game.php?*screen=info_ally*
// @downloadURL  ${process.env.PUBLIC_URL}/extended-tribe-profile.user.js
// @updateURL    ${process.env.PUBLIC_URL}/extended-tribe-profile.user.js
// @icon         https://www.google.com/s2/favicons?domain=plemiona.pl
// @grant        none
// @run-at       document-end
// ==/UserScript==`,
};

Object.entries(metadata).forEach(([name, m]) => {
  const p = path.join('.', DIST_DIR, `${name}${FILE_SUFFIX}`);
  const data = fs.readFileSync(p);
  const fd = fs.openSync(p, 'w+');
  const insert = Buffer.from(`${m}\n`);
  fs.writeSync(fd, insert, 0, insert.length, 0);
  fs.writeSync(fd, data, 0, data.length, insert.length);
  fs.closeSync(fd);
});
