"use strict";
/* eslint-disable @typescript-eslint/no-use-before-define */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getJavaGenLibPath = exports.getJavaExecPath = exports.getJreBaseDir = exports.getJreDownloadUrl = exports.javaExecProvided = exports.downloadJre = void 0;
var tslib_1 = require("tslib");
/**
 * Copyright 2017-2022 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 os = require("os");
var path = require("path");
var download_1 = require("./download");
var files_1 = require("./files");
function downloadJre() {
    var url = getJreDownloadUrl();
    var downloadDir = getJreDownloadDir();
    var title = 'JRE';
    return (0, 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 (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
        var execPath, execExists;
        return (0, tslib_1.__generator)(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, getJavaExecPath()];
                case 1:
                    execPath = _a.sent();
                    if (!execPath.endsWith('.exe')) {
                        throw new Error("Java exe path does not target an .exe: " + execPath);
                    }
                    return [4 /*yield*/, (0, files_1.exists)(execPath)];
                case 2:
                    execExists = _a.sent();
                    if (execExists) {
                        return [2 /*return*/, execPath];
                    }
                    throw new Error("Does not exist: " + execPath);
            }
        });
    });
}
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 = (0, 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 (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
        var baseDir;
        return (0, 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(baseDir, 'bin', getExePath())];
        });
    });
}
exports.getJavaExecPath = getJavaExecPath;
function getExePath() {
    return os.platform() === 'win32' ? 'java.exe' : 'java';
}
var getJreDownloadDir = function () { return path.join((0, files_1.getDistDir)(), 'jre'); };
var getJavaGenLibPath = function () { return path.join((0, files_1.getDistDir)(), 'lib', 'plexusgen.jar'); };
exports.getJavaGenLibPath = getJavaGenLibPath;
//# sourceMappingURL=java.js.map