"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");
var getDownloadDir = function () { return path.join(files_1.getDistDir(), 'protoc'); };
function downloadProtoc() {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var url, downloadDir, title, downloadedExe, targetFile;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    url = getProtocDownloadUrl();
                    downloadDir = getDownloadDir();
                    title = 'Protoc';
                    return [4 /*yield*/, 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;
                            files_1.iterateFiles(downloadDir, /protoc.*\.exe/g, function (f) { return temp = f; }, false);
                            if (temp) {
                                resolve(temp);
                            }
                            else {
                                reject(new Error('Protoc EXE not found'));
                            }
                        })];
                case 2:
                    downloadedExe = _a.sent();
                    console.log('Downloaded exe file', downloadedExe);
                    files_1.mkdirsSync(path.join(downloadDir, 'bin'));
                    targetFile = path.join(downloadDir, 'bin', 'protoc.exe');
                    console.log('Copying to', targetFile);
                    return [4 /*yield*/, files_1.copyFile(downloadedExe, targetFile)];
                case 3:
                    _a.sent();
                    console.log("Clearing " + downloadedExe);
                    files_1.removeSync(downloadedExe);
                    _a.label = 4;
                case 4: return [2 /*return*/, downloadDir];
            }
        });
    });
}
exports.downloadProtoc = downloadProtoc;
function protocExecProvided() {
    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:
                    execPath = getProtocExecPath();
                    return [4 /*yield*/, files_1.exists(execPath)];
                case 1:
                    execExists = _a.sent();
                    if (execExists) {
                        return [2 /*return*/, execPath];
                    }
                    else {
                        throw new Error("Do not exist " + execPath);
                    }
                    return [2 /*return*/];
            }
        });
    });
}
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, tslib_1.__spreadArrays([baseDir], getExePath()));
}
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/google/protobuf/releases/download/v3.5.1/protoc-3.5.1-win32.zip';
        default:
            throw new Error(platform + " is not supported");
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJvdG9jLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2NvbW1vbi9wcm90b2MudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUE7Ozs7Ozs7Ozs7Oz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