#!/usr/bin/env node

/**
 * This literally only updates the old replays template.
 * It can be removed once replays/manage is ported to New Replays.
 */

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

process.chdir(__dirname + '/../replay.pokemonshowdown.com');

function updateIndex() {
	let indexContents = fs.readFileSync('theme/wrapper.inc.template.php', { encoding: 'utf8' });

	// add hashes to js and css files
	process.stdout.write("Updating hashes... ");
	// Check for <script, <link or <img so we don't add useless hashes to <a
	indexContents = indexContents.replace(
		/(<script[^>]+?src|<link[^>]+?href|<img[^>]+?src)="\/(.*?)(\?[a-z0-9]*?)?"/g, runReplace
	);
	console.log("DONE");

	process.stdout.write("Writing new `wrapper.inc.php` file... ");
	fs.writeFileSync('theme/wrapper.inc.php', indexContents);
	console.log("DONE");
}

function runReplace(a, b, c) {
	let hash = Math.random(); // just in case creating the hash fails
	const routes = JSON.parse(fs.readFileSync('../config/routes.json'));
	try {
		let filepath = c;
		if (c.includes('/' + routes.client + '/')) {
			const filename = c.replace('/' + routes.client + '/', '');
			filepath = '../' + filename;
		}
		const fstr = fs.readFileSync(filepath, { encoding: 'utf8' });
		hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
	} catch {}
	c = c.replace('/replay.pokemonshowdown.com/', '/' + routes.replays + '/');
	c = c.replace('/dex.pokemonshowdown.com/', '/' + routes.dex + '/');
	c = c.replace('/play.pokemonshowdown.com/', '/' + routes.client + '/');
	c = c.replace('/pokemonshowdown.com/users/', '/' + routes.users + '/');
	c = c.replace('/pokemonshowdown.com/', '/' + routes.root + '/');

	return b + '="/' + c + '?' + hash + '"';
}

updateIndex();
