"use strict";
/**
 * 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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProtocDownloadUrl = exports.getProtocExecPath = exports.protocExecProvided = exports.downloadProtoc = void 0;
var tslib_1 = require("tslib");
/* eslint-disable @typescript-eslint/no-use-before-define */
var os = require("os");
var path = require("path");
var download_1 = require("./download");
var files_1 = require("./files");
var getDownloadDir = function () { return path.join((0, files_1.getDistDir)(), 'protoc'); };
function downloadProtoc() {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
        var url, downloadDir, title, downloadedExe, targetFile;
        return (0, tslib_1.__generator)(this, function (_a) {
            switch (_a.label) {
                case 0:
                    url = getProtocDownloadUrl();
                    downloadDir = getDownloadDir();
                    title = 'Protoc';
                    return [4 /*yield*/, (0, download_1.downloadPackage)(url, downloadDir, title)];
                case 1:
                    _a.sent();
                    if (!url.endsWith('.exe')) return [3 /*break*/, 4];
                    return [4 /*yield*/, new Promise(function (resolve, reject) {
                            var temp = null;
                            (0, files_1.iterateFiles)(downloadDir, /protoc.*\.exe/g, function (f) {
                                temp = f;
                            });
                            if (temp) {
                                resolve(temp);
                            }
                            else {
                                reject(new Error('Protoc EXE not found'));
                            }
                        })];
                case 2:
                    downloadedExe = _a.sent();
                    console.log('Downloaded exe file', downloadedExe);
                    (0, files_1.mkdirsSync)(path.join(downloadDir, 'bin'));
                    targetFile = path.join(downloadDir, 'bin', 'protoc.exe');
                    console.log('Copying to', targetFile);
                    return [4 /*yield*/, (0, files_1.copyFile)(downloadedExe, targetFile)];
                case 3:
                    _a.sent();
                    console.log("Clearing " + downloadedExe);
                    (0, files_1.removeSync)(downloadedExe);
                    _a.label = 4;
                case 4: return [2 /*return*/, downloadDir];
            }
        });
    });
}
exports.downloadProtoc = downloadProtoc;
function protocExecProvided() {
    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:
                    execPath = getProtocExecPath();
                    if (!execPath.endsWith('.exe')) {
                        throw new Error("Protoc exe path does not target an .exe: " + execPath);
                    }
                    return [4 /*yield*/, (0, files_1.exists)(execPath)];
                case 1:
                    execExists = _a.sent();
                    if (execExists) {
                        return [2 /*return*/, execPath];
                    }
                    throw new Error("Does not exist " + execPath);
            }
        });
    });
}
exports.protocExecProvided = protocExecProvided;
function getProtocExecPath() {
    if (process.env.PLEXUS_CLI_PROTOC_EXE_PATH) {
        console.log("Using protoc from env variable " + process.env.PLEXUS_CLI_PROTOC_EXE_PATH);
        return process.env.PLEXUS_CLI_PROTOC_EXE_PATH;
    }
    var baseDir = getDownloadDir();
    return path.join.apply(path, (0, tslib_1.__spreadArray)([baseDir, 'bin'], getExePath(), false));
}
exports.getProtocExecPath = getProtocExecPath;
function getProtocDownloadUrl() {
    var platform = os.platform() + "-" + os.arch();
    return (process.env["PLEXUS_PROTOC_DOWNLOAD_URL_" + platform.toUpperCase()] ||
        process.env.PLEXUS_PROTOC_DOWNLOAD_URL ||
        getDefaultDownloadUrl(platform));
}
exports.getProtocDownloadUrl = getProtocDownloadUrl;
function getExePath() {
    return os.platform() === 'win32' ? ['bin', 'protoc.exe'] : ['bin', 'protoc'];
}
function getDefaultDownloadUrl(platform) {
    switch (platform) {
        case 'win32-ia32':
        case 'win32-x32':
        case 'win32-x64':
            return 'https://github.com/protocolbuffers/protobuf/releases/download/v3.19.1/protoc-3.19.1-win32.zip';
        case 'darwin-x64':
            return 'https://github.com/protocolbuffers/protobuf/releases/download/v3.19.1/protoc-3.19.1-osx-x86_64.zip';
        default:
            throw new Error(platform + " is not supported");
    }
}
//# sourceMappingURL=protoc.js.map