#!/usr/bin/env node

/**
 * This script can be used to convert databases from one format to another
 * Format:
 * `node tools/modlog/convert --db path/to/database --logdir path/to/modlogs --from <database format> --to <database format>`
 * Usage example:
 * `node tools/modlog/convert --db databases/modlog.db --logdir logs/modlog --from txt --to sqlite`
 * @author jetou
 */

'use strict';

// support TypeScript
require('child_process').execSync('node ' + __dirname + "/../build");
const ModlogConverter = require(`${__dirname}/converter.ts`).ModlogConverter;
const path = require('path');

function parseFlags() {
	const args = process.argv.slice(2);
	const flags = Object.create(null);
	for (const [idx, arg] of args.entries()) {
		if (idx % 2 === 0) {
			flags[arg] = args[idx + 1];
		}
	}
	return flags;
}

const flags = parseFlags();

const databasePath = flags['--db'];
const textLogPath = flags['--logdir'];
const outputLogPath = flags['--outputlogdir'];
const from = flags['--from'];
const to = flags['--to'];
const newestAllowedTimestamp = parseInt(flags['--newest-allowed-timestamp']);

if (newestAllowedTimestamp && isNaN(newestAllowedTimestamp)) {
	throw new Error(`The newest allowed timestamp must be a number of milliseconds.`);
}

if (
	!((databasePath || outputLogPath) && textLogPath && from && to) ||
	!['txt', 'sqlite'].includes(from) ||
	!['txt', 'sqlite'].includes(to)
) {
	throw new Error(`Invalid arguments specified.\nUsage: node tools/modlog/convert --db path/to/database --logdir path/to/modlogs --from <database format> --to <database format>.`);
}

console.log(`Converting ${from === 'txt' ? `the text modlog files in ${textLogPath}` : `${databasePath}`} from ${from} to ${to}...`);
ModlogConverter.convert(from, to, path.resolve(databasePath || ""), path.resolve(textLogPath), path.resolve(outputLogPath || ""), newestAllowedTimestamp);
