"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadenv = exports.hassourceenv = exports.getsourcedir = exports.haslocalenv = exports.getlocaldir = exports.RunService = exports.InstallService = exports.RemoveService = exports.StopService = exports.StartService = exports.isOpenFlow = exports.isMac = exports.isWin = exports.servicename = exports.envfilepathname = exports.envfilename = exports.logger = void 0;
var Logger_1 = require("../Logger");
var Config_1 = require("../Config");
var fs = require("fs");
exports.logger = Logger_1.Logger.configure();
var cp = require('child_process');
var path = require('path');
var envfile = require('envfile');
exports.envfilename = ".env";
exports.envfilepathname = "";
exports.servicename = "openflow-nodered";
var service = require("os-service");
function isWin() {
    return process.platform === "win32";
}
exports.isWin = isWin;
function isMac() {
    return process.platform === "darwin";
}
exports.isMac = isMac;
function isOpenFlow() {
    var check1 = path.join(__dirname, "..", "DatabaseConnection.ts");
    var check2 = path.join(__dirname, "..", "DatabaseConnection.js");
    if (fs.existsSync(check1) || fs.existsSync(check2))
        return true;
    return false;
}
exports.isOpenFlow = isOpenFlow;
function StartService(servicename) {
    try {
        if (isWin()) {
            cp.execSync("net start " + servicename);
        }
        else if (isMac()) {
            // https://medium.com/craftsmenltd/building-a-cross-platform-background-service-in-node-js-791cfcd3be60
            // cp.execSync(`sudo launchctl load ${LAUNCHD_PLIST_PATH}`);
        }
        else {
            cp.execSync("service " + servicename + " start");
        }
    }
    catch (error) {
        exports.logger.info(error.message ? error.message : error);
    }
}
exports.StartService = StartService;
function StopService(servicename) {
    try {
        if (isWin()) {
            cp.execSync("net stop " + servicename);
        }
        else if (isMac()) {
            // https://medium.com/craftsmenltd/building-a-cross-platform-background-service-in-node-js-791cfcd3be60
            // cp.execSync(`sudo launchctl unload ${LAUNCHD_PLIST_PATH}`);
        }
        else {
            cp.execSync("service " + servicename + " stop");
        }
    }
    catch (error) {
        exports.logger.info(error.message ? error.message : error);
    }
}
exports.StopService = StopService;
function RemoveService(servicename) {
    StopService(servicename);
    exports.logger.info("Uninstalling service" + servicename);
    service.remove(servicename, function (error) {
        if (error) {
            exports.logger.info(error.message ? error.message : error);
            return;
        }
        exports.logger.info("Service" + servicename + " uninstalled");
    });
}
exports.RemoveService = RemoveService;
function InstallService(servicename, configfile) {
    exports.logger.info("Installing service" + servicename);
    service.add(servicename, { programArgs: [servicename, "--run", "--config", configfile] }, function (error) {
        if (error) {
            exports.logger.info(error.message ? error.message : error);
            return;
        }
        exports.logger.info("Service" + servicename + " installed");
        StartService(servicename);
    });
}
exports.InstallService = InstallService;
function RunService(callback) {
    service.run(function () {
        exports.logger.info("Service" + exports.servicename + " stopping");
        if (callback != null)
            callback();
        service.stop(0);
    });
}
exports.RunService = RunService;
// use and copy current env file, unless we have a /config folder in root
function getlocaldir() {
    var local = __dirname;
    if (fs.existsSync(path.join(local, "..", "config"))) {
        local = path.join(local, "..", "config");
    }
    else if (fs.existsSync(path.join(local, "..", "..", "config"))) {
        local = path.join(local, "..", "..", "config");
    }
    else if (fs.existsSync(path.join(local, "..", "..", "..", "config"))) {
        local = path.join(local, "..", "..", "..", "config");
    }
    else {
        local = process.cwd();
    }
    return local;
}
exports.getlocaldir = getlocaldir;
function haslocalenv() {
    var localenv = path.join(getlocaldir(), exports.envfilename);
    return fs.existsSync(localenv);
}
exports.haslocalenv = haslocalenv;
// set source to location of source files, unless we have a /config folder in root
function getsourcedir() {
    var source = __dirname;
    if (fs.existsSync(path.join(source, "..", "config"))) {
        source = path.join(source, "..", "config");
    }
    else if (fs.existsSync(path.join(source, "..", "..", "config"))) {
        source = path.join(source, "..", "..", "config");
    }
    else if (fs.existsSync(path.join(source, "..", "..", "..", "config"))) {
        source = path.join(source, "..", "..", "..", "config");
    }
    return source;
}
exports.getsourcedir = getsourcedir;
function hassourceenv() {
    var sourceenv = path.join(getsourcedir(), exports.envfilename);
    return fs.existsSync(sourceenv);
}
exports.hassourceenv = hassourceenv;
function loadenv() {
    exports.logger.info("NodeJS version " + process.version + " Config " + exports.envfilepathname);
    var parsedFile = envfile.parse(fs.readFileSync(exports.envfilepathname));
    for (var k in parsedFile) {
        process.env[k] = parsedFile[k];
        // logger.verbose(k + " = " + parsedFile[k]);
    }
    Config_1.Config.reload();
}
exports.loadenv = loadenv;
//# sourceMappingURL=cliutil.js.map