"use strict";

global.log = require("../log.js");

const _ = require("lodash");
const fs = require("fs");
const path = require("path");
const program = require("commander");
const colors = require("colors/safe");
const Helper = require("../helper");
const Utils = require("./utils");

if (require("semver").lt(process.version, "6.0.0")) {
	log.warn(`Support of Node.js v4 is ${colors.bold.red("deprecated")} and will be removed in The Lounge v3.`);
	log.warn("Please upgrade to Node.js v6 or more recent.");
}

program.version(Helper.getVersion(), "-v, --version")
	.option("--home <path>", `${colors.bold.red("[DEPRECATED]")} Use the ${colors.green("THELOUNGE_HOME")} environment variable instead.`)
	.option(
		"-c, --config <key=value>",
		"override entries of the configuration file, must be specified for each entry that needs to be overriden",
		Utils.parseConfigOptions
	)
	.on("--help", Utils.extraHelp);

// Parse options from `argv` returning `argv` void of these options.
const argvWithoutOptions = program.parseOptions(process.argv);

if (program.home) {
	log.warn(`${colors.bold("--home")} is ${colors.bold.red("deprecated")} and will be removed in The Lounge v3. Use the ${colors.bold("THELOUNGE_HOME")} environment variable instead.`);
}

// Check if the app was built before calling setHome as it wants to load manifest.json from the public folder
if (!fs.existsSync(path.join(
	__dirname,
	"..",
	"..",
	"public",
	"manifest.json"
))) {
	log.error(`The client application was not built. Run ${colors.bold("NODE_ENV=production npm run build")} to resolve this.`);
	process.exit(1);
}

if (process.env.LOUNGE_HOME) {
	log.warn(`${colors.green("LOUNGE_HOME")} is ${colors.bold.red("deprecated")} and will be removed in The Lounge v3.`);
	log.warn(`Use ${colors.green("THELOUNGE_HOME")} instead.`);
}

let home = process.env.THELOUNGE_HOME || program.home || process.env.LOUNGE_HOME;

if (!home) {
	home = Utils.defaultHome();
}

Helper.setHome(home);

// Merge config key-values passed as CLI options into the main config
_.merge(Helper.config, program.config);

require("./start");
require("./config");
if (!Helper.config.public && !Helper.config.ldap.enable) {
	require("./users");
}
require("./install");

// TODO: Remove this when releasing The Lounge v3
if (process.argv[1].endsWith(`${require("path").sep}lounge`)) {
	log.warn(`The ${colors.red("lounge")} CLI is ${colors.bold.red("deprecated")} and will be removed in v3.`);
	log.warn(`Use ${colors.green("thelounge")} instead.`);
	process.argv[1] = "thelounge";
}

// `parse` expects to be passed `process.argv`, but we need to remove to give it
// a version of `argv` that does not contain options already parsed by
// `parseOptions` above.
// This is done by giving it the updated `argv` that `parseOptions` returned,
// except it returns an object with `args`/`unknown`, so we need to concat them.
// See https://github.com/tj/commander.js/blob/fefda77f463292/index.js#L686-L763
program.parse(argvWithoutOptions.args.concat(argvWithoutOptions.unknown));

if (!program.args.length) {
	program.help();
}

@@ -562,144 +562,8 @@
 n%22)%0A
-%09.option(%22--home %3Cpath%3E%22, %60$%7Bcolors.bold.red(%22%5BDEPRECATED%5D%22)%7D Use the $%7Bcolors.green(%22THELOUNGE_HOME%22)%7D environment variable instead.%60)%0A
 %09.op
@@ -568,16 +568,16 @@
 option(%0A
+
 %09%09%22-c, -
@@ -908,215 +908,8 @@
 );%0A%0A
-if (program.home) %7B%0A%09log.warn(%60$%7Bcolors.bold(%22--home%22)%7D is $%7Bcolors.bold.red(%22deprecated%22)%7D and will be removed in The Lounge v3. Use the $%7Bcolors.bold(%22THELOUNGE_HOME%22)%7D environment variable instead.%60);%0A%7D%0A%0A
 // C
@@ -1255,369 +1255,72 @@
 %0A%7D%0A%0A
-if (process.env.LOUNGE_HOME) %7B%0A%09log.warn(%60$%7Bcolors.green(%22LOUNGE_HOME%22)%7D is $%7Bcolors.bold.red(%22deprecated%22)%7D and will be removed in The Lounge v3.%60);%0A%09log.warn(%60Use $%7Bcolors.green(%22THELOUNGE_HOME%22)%7D instead.%60);%0A%7D%0A%0Alet home = process.env.THELOUNGE_HOME %7C%7C program.home %7C%7C process.env.LOUNGE_HOME;%0A%0Aif (!home) %7B%0A%09home = Utils.defaultHome();%0A%7D%0A%0AHelper.setHome(home
+Helper.setHome(process.env.THELOUNGE_HOME %7C%7C Utils.defaultHome()
 );%0A%0A

Remove deprecated support for the `--home` option and `$LOUNGE_HOME` environment variable