"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
/**
 * Copyright 2017-2020 Plexus Interop Deutsche Bank AG
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var path = require("path");
var os = require("os");
var files_1 = require("./files");
var download_1 = require("./download");
function downloadJre() {
    var url = getJreDownloadUrl();
    var downloadDir = getJreDownloadDir();
    var title = 'JRE';
    return download_1.downloadPackage(url, downloadDir, title, {
        connection: 'keep-alive',
        Cookie: 'gpw_e24=http://www.oracle.com/; oraclelicense=accept-securebackup-cookie'
    });
}
exports.downloadJre = downloadJre;
function javaExecProvided() {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var execPath, execExists;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, getJavaExecPath()];
                case 1:
                    execPath = _a.sent();
                    return [4 /*yield*/, files_1.exists(execPath)];
                case 2:
                    execExists = _a.sent();
                    if (execExists) {
                        return [2 /*return*/, execPath];
                    }
                    else {
                        throw new Error("Do not exist " + execPath);
                    }
                    return [2 /*return*/];
            }
        });
    });
}
exports.javaExecProvided = javaExecProvided;
function getJreDownloadUrl() {
    var platform = os.platform() + "-" + os.arch();
    return process.env["PLEXUS_JRE_DOWNLOAD_URL_" + platform.toUpperCase()]
        || process.env.PLEXUS_JRE_DOWNLOAD_URL
        || getDefaultDownloadUrl(platform);
}
exports.getJreDownloadUrl = getJreDownloadUrl;
function getDefaultDownloadUrl(platform) {
    switch (platform) {
        case 'win32-ia32':
        case 'win32-x32':
            return 'http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jre-8u161-windows-i586.tar.gz';
        case 'win32-x64':
            return 'http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jre-8u161-windows-x64.tar.gz';
        default:
            throw new Error(platform + " is not supported");
    }
}
function getJreBaseDir(downloadDir) {
    var childs = files_1.getDirectories(downloadDir);
    if (childs.length === 0) {
        throw new Error('No JRE found');
    }
    return path.join(downloadDir, childs[0]);
}
exports.getJreBaseDir = getJreBaseDir;
function getJavaExecPath() {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var baseDir;
        return tslib_1.__generator(this, function (_a) {
            if (process.env.PLEXUS_CLI_JAVA_EXE_PATH) {
                console.log("Using Java executable from env variable " + process.env.PLEXUS_CLI_JAVA_EXE_PATH);
                return [2 /*return*/, process.env.PLEXUS_CLI_JAVA_EXE_PATH];
            }
            baseDir = getJreBaseDir(getJreDownloadDir());
            return [2 /*return*/, path.join.apply(path, tslib_1.__spreadArrays([baseDir], getExePath()))];
        });
    });
}
exports.getJavaExecPath = getJavaExecPath;
function getExePath() {
    return os.platform() === 'win32' ? ['bin', 'java.exe'] : ['bin', 'java'];
}
var getJreDownloadDir = function () { return path.join(files_1.getDistDir(), 'jre'); };
exports.getJavaGenLibPath = function () { return path.join(files_1.getDistDir(), 'lib', 'plexusgen.jar'); };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiamF2YS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9jb21tb24vamF2YS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBQTs7Ozs7Ozs7Ozs7Ozs7O0dBZUc7QUFDSCwyQkFBNkI7QUFDN0IsdUJBQXlCO0FBQ3pCLGlDQUE2RDtBQUM3RCx1Q0FBNkM7QUFFN0MsU0FBZ0IsV0FBVztJQUN2QixJQUFNLEdBQU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