"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseCommandLine = exports.version = exports.config = void 0;
const command_line_args_1 = __importDefault(require("command-line-args"));
const command_line_usage_1 = __importDefault(require("command-line-usage"));
const graceful_fs_1 = __importDefault(require("graceful-fs"));
const url_1 = __importDefault(require("url"));
const sparkson_1 = require("sparkson");
const types_1 = require("./types");
// @ts-ignore
exports.config = {};
exports.version = "unknown";
function parseCommandLine() {
    const commandLineDefs = [
        { name: 'help', alias: 'h', type: Boolean, description: 'Show this help' },
        { name: 'config', alias: 'c', type: String, description: 'Location of config.json' },
        { name: 'version', alias: 'v', type: Boolean, description: 'Show ffffng version' }
    ];
    let commandLineOptions;
    try {
        commandLineOptions = command_line_args_1.default(commandLineDefs);
    }
    catch (e) {
        console.error(e.message);
        console.error('Try \'--help\' for more information.');
        process.exit(1);
    }
    const packageJsonFile = __dirname + '/../package.json';
    if (graceful_fs_1.default.existsSync(packageJsonFile)) {
        exports.version = JSON.parse(graceful_fs_1.default.readFileSync(packageJsonFile, 'utf8')).version;
    }
    function usage() {
        console.log(command_line_usage_1.default([
            {
                header: 'ffffng - ' + exports.version + ' - Freifunk node management form',
                optionList: commandLineDefs
            }
        ]));
    }
    if (commandLineOptions.help) {
        usage();
        process.exit(0);
    }
    if (commandLineOptions.version) {
        console.log('ffffng - ' + exports.version);
        process.exit(0);
    }
    if (!commandLineOptions.config) {
        usage();
        process.exit(1);
    }
    const configJSONFile = commandLineOptions.config;
    let configJSON = {};
    if (graceful_fs_1.default.existsSync(configJSONFile)) {
        configJSON = JSON.parse(graceful_fs_1.default.readFileSync(configJSONFile, 'utf8'));
    }
    else {
        console.error('config.json not found: ' + configJSONFile);
        process.exit(1);
    }
    exports.config = sparkson_1.parse(types_1.Config, configJSON);
    function stripTrailingSlash(url) {
        return url.endsWith("/") ? url.substr(0, url.length - 1) : url;
    }
    exports.config.server.baseUrl = stripTrailingSlash(exports.config.server.baseUrl);
    exports.config.client.map.mapUrl = stripTrailingSlash(exports.config.client.map.mapUrl);
    exports.config.server.rootPath = url_1.default.parse(exports.config.server.baseUrl).pathname || "/";
    exports.config.client.rootPath = exports.config.server.rootPath;
}
exports.parseCommandLine = parseCommandLine;
//# sourceMappingURL=config.js.map