#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const Logger_1 = require("./Logger");
Logger_1.Logger.configure(true, true);
Logger_1.Logger.enabled["cli-lic"] = 7;
const Config_1 = require("./Config");
const openflow_api_1 = require("@openiap/openflow-api");
function printusage() {
    Logger_1.Logger.instanse.info("cli-lic", "", "openflow-cli [--months 3][--email email] domain");
    Logger_1.Logger.instanse.info("cli-lic", "", "   --months - Set number of months, default 3");
    Logger_1.Logger.instanse.info("cli-lic", "", "   --email - email to use in license");
    Logger_1.Logger.instanse.info("cli-lic", "", "   domain - Generate ofid and create v2 license");
    Logger_1.Logger.instanse.info("cli-lic", "", "Requires private key to be in ./config/private_key.pem");
}
const optionDefinitions = [
    { name: 'email', type: String },
    { name: 'months', type: Number },
    { name: 'domain', type: String, defaultOption: true }
];
let _lic_require = null;
try {
    _lic_require = require("./ee/license-file");
}
catch (error) {
}
if (_lic_require != null) {
    Logger_1.Logger.License = new _lic_require.LicenseFile();
}
else {
    Logger_1.Logger.License = {};
    Logger_1.Logger.License.ofid = function () {
        if (!openflow_api_1.NoderedUtil.IsNullEmpty(this._ofid))
            return this._ofid;
        var crypto = require('crypto');
        const openflow_uniqueid = Config_1.Config.openflow_uniqueid || crypto.createHash('md5').update(Config_1.Config.domain).digest("hex");
        Config_1.Config.openflow_uniqueid = openflow_uniqueid;
        this._ofid = openflow_uniqueid;
        return openflow_uniqueid;
    };
}
const commandLineArgs = require('command-line-args');
let options = null;
try {
    options = commandLineArgs(optionDefinitions, { partial: true });
    let months = 3;
    if (!options.email && !options.domain) {
        throw new Error("Domain is mandatory");
    }
    if (!options.email)
        options.email = "";
    if (options.months) {
        months = parseInt(options.months);
    }
    if (options._unknown) {
        Logger_1.Logger.instanse.info("cli-lic", "", "Unknown param " + options._unknown);
        process.exit();
    }
    if (!fs.existsSync('config/private_key.pem')) {
        Logger_1.Logger.instanse.info("cli-lic", "", "no such file or directory, open config/private_key.pem");
        process.exit();
    }
    const data = {};
    let template = Logger_1.Logger.License.template_v1;
    data.licenseVersion = 1;
    data.email = options.email;
    if (options.domain) {
        template = Logger_1.Logger.License.template_v2;
        data.licenseVersion = 2;
        data.domain = options.domain;
    }
    var dt = new Date(new Date().toISOString());
    dt.setMonth(dt.getMonth() + months);
    data.expirationDate = dt.toISOString();
    const licenseFileContent = Logger_1.Logger.License.generate({
        privateKeyPath: 'config/private_key.pem',
        template,
        data: data
    });
    Logger_1.Logger.instanse.info("cli-lic", "", Buffer.from(licenseFileContent).toString('base64'));
    process.exit();
}
catch (error) {
    Logger_1.Logger.instanse.error("cli-lic", "", error.message ? error.message : error);
    printusage();
    process.exit();
}
// node C:\code\openflow\dist\cli-lic.js --months 1 localhost.openiap.io
// openflow-lic --months 1 localhost.openiap.io | clip
//# sourceMappingURL=cli-lic.js.map