"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadenv = exports.hassourceenv = exports.getsourcedir = exports.haslocalenv = exports.getlocaldir = 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";
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;
// 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