"use strict";
/* TODO auto-generated eslint ignore, please fix! */
/* eslint node/no-unpublished-import: "off", @typescript-eslint/no-var-requires: "off", no-process-exit: "off" */
/**
 * Start the Liza Server
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var path = require("path");
var ConfLoader_1 = require("../src/conf/ConfLoader");
var ConfStore_1 = require("../src/conf/ConfStore");
var version = require("../src/version");
// kluge for now
var conf_path = (process.argv[2] === '-c' ? process.argv[3] : '') ||
    __dirname + '/../conf/vanilla-server.json';
var conf_dir = path.dirname(conf_path);
new ConfLoader_1.ConfLoader(fs, ConfStore_1.ConfStore)
    .fromFile(conf_path)
    .then(function (conf) {
    return Promise.all([
        conf.get('name'),
        conf.get('daemon'),
        conf.get('pidfile'),
        Promise.resolve(conf),
    ]);
})
    .then(function (_a) {
    var name = _a[0], daemon = _a[1], pidfile = _a[2], conf = _a[3];
    var daemon_path = _resolvePath(conf_dir, daemon);
    var pid_path = _resolvePath(conf_dir, pidfile || '.pid');
    writePidFile(pid_path);
    greet(name, pid_path);
    return require(daemon_path)(conf).start();
})
    .catch(function (e) {
    console.error(e.stack);
    process.exit(1);
});
/**
 * Produce an absolute path if `path` is absolute, otherwise a path relative
 * to the configuration directory
 *
 * @param conf_path - configuration path (for relative `path`)
 * @param path      - path to resolve
 *
 * @return resolved path
 */
function _resolvePath(conf_path, path) {
    return path[0] === '/' ? path : conf_path + '/' + path;
}
/**
 * Write process id (PID) file
 *
 * @param pid_path - path to pid file
 */
function writePidFile(pid_path) {
    fs.writeFileSync(pid_path, '' + process.pid);
    process.on('exit', function () { return fs.unlink(pid_path, function () { }); });
}
/**
 * Output greeting
 *
 * The greeting contains the program name, version, configuration path,
 * and PID file path.
 *
 * @param name     - program name
 * @param pid_path - path to PID file
 */
function greet(name, pid_path) {
    console.log(name + " (liza-" + version + ")");
    console.log("Server configuration: " + conf_path);
    console.log("PID file: " + pid_path);
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VydmVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsic2VydmVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxvREFBb0Q7QUFDcEQsaUhBQWlIO0FBQ2pIOzs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBbUJHOztBQUVILHVCQUEwQjtBQUMxQiwyQkFBOEI7QUFFOUIscURBQWtEO0FBQ2xELG1EQUFnRDtBQUNoRCx3Q0FBMEM7QUFFMUMsZ0JBQWdCO0FBQ2hCLElBQU0sU0FBUyxHQUNiLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUMsS0FBSyxJQUFJLENBQUMsQ0FBQyxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQztJQUNqRCxTQUFTLEdBQUcsOEJBQThCLENBQUM7QUFFN0MsSUFBTSxRQUFRLEdBQUcsSUFBSSxDQUFDLE9BQU8sQ0FBQyxTQUFTLENBQUMsQ0FBQztBQUV6QyxJQUFJLHVCQUFVLENBQUMsRUFBRSxFQUFFLHFCQUFTLENBQUM7S0FDMUIsUUFBUSxDQUFDLFNBQVMsQ0FBQztLQUNuQixJQUFJLENBQUMsVUFBQSxJQUFJO0lBQ1IsT0FBQSxPQUFPLENBQUMsR0F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