"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: update.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
const core_1 = require("@oclif/core");
const utils_1 = tslib_1.__importDefault(require("../classes/utils"));
const tools_1 = tslib_1.__importDefault(require("../classes/tools"));
const pacman_1 = tslib_1.__importDefault(require("../classes/pacman"));
const utils_2 = require("../lib/utils");
const inquirer = require('inquirer');
/**
 *
 */
class Update extends core_1.Command {
    async run() {
        utils_1.default.titles(this.id + ' ' + this.argv);
        const { flags } = await this.parse(Update);
        utils_1.default.titles(this.id + ' ' + this.argv);
        if (utils_1.default.isRoot()) {
            if (utils_1.default.isSources()) {
                utils_1.default.warning(`You are on penguins-eggs v. ${utils_1.default.getPackageVersion()} from sources`);
            }
            else if (utils_1.default.isDebPackage()) {
                utils_1.default.warning(`You are on eggs-${utils_1.default.getPackageVersion()} installed as package .deb`);
            }
            await this.chooseUpdate();
        }
        else {
            utils_1.default.useRoot(this.id);
        }
    }
    /**
     * Altrimenti, seleziona il tipo di
     * aggiornamento desiderato
     * indipendentemente dal flag
     */
    async chooseUpdate() {
        console.log();
        const choose = await this.chosenDeb();
        utils_1.default.titles(`updating via ${choose}`);
        switch (choose) {
            case 'apt': {
                await this.getDebFromApt();
                break;
            }
            case 'lan': {
                await this.getDebFromLan();
                break;
            }
            case 'manual': {
                this.getDebFromManual();
                break;
            }
            case 'sources': {
                this.getFromSources();
                break;
            }
            // No default
        }
    }
    /**
     *
     */
    async chosenDeb() {
        const choices = ['abort'];
        choices.push('apt', 'lan', 'manual', 'sources');
        const questions = [
            {
                type: 'list',
                message: 'select update method',
                name: 'selected',
                choices: choices,
            },
        ];
        const answer = await inquirer.prompt(questions);
        if (answer.selected === 'abort') {
            process.exit(0);
        }
        return answer.selected;
    }
    /**
     *
     * @param aptVersion
     */
    getFromSources() {
        console.log('You can upgrade getting a new version from git:');
        console.log('cd ~/penguins-eggs');
        console.log('git pull');
        console.log('');
        console.log('Or You can also create a fresh installation on your hone:');
        console.log('cd ~');
        console.log('git clone https://github.com/pieroproietti/penguins-eggs');
        console.log('');
        console.log('Before to use eggs from sources, remember to install npm packages:');
        console.log('cd ~/penguins-eggs');
        console.log('npm install');
    }
    /**
     *
     */
    async getDebFromManual() {
        console.log('Download package from: \n\nhttps://sourceforge.net/projects/penguins-eggs/files/packages-deb/');
        console.log('\nand install it with:');
        console.log('\nsudo dpkg -i eggs_x.x.x-1.deb');
    }
    /**
     * download da LAN
     */
    async getDebFromLan() {
        const Tu = new tools_1.default();
        await Tu.loadSettings();
        utils_1.default.warning('import from lan');
        const cmd = `scp ${Tu.config.remoteUser}@${Tu.config.remoteHost}:${Tu.config.remotePathDeb}${Tu.config.filterDeb}${utils_1.default.uefiArch()}.deb /tmp`;
        await (0, utils_2.exec)(cmd, { echo: true, capture: true });
        if (await utils_1.default.customConfirm(`Want to install ${Tu.config.filterDeb}${utils_1.default.uefiArch()}.deb`)) {
            await (0, utils_2.exec)(`dpkg -i /tmp/${Tu.config.filterDeb}${utils_1.default.uefiArch()}.deb`);
        }
    }
    /**
     *
     */
    async getDebFromApt() {
        if (await pacman_1.default.packageAptAvailable('eggs')) {
            await (0, utils_2.exec)('apt reinstall eggs');
        }
        else {
            console.log('eggs is not present in your repositories');
            console.log('but you can upgrade from internet');
        }
    }
}
Update.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    verbose: core_1.Flags.boolean({ char: 'v', description: 'verbose' }),
};
Update.description = "update the Penguins' eggs tool";
Update.examples = [
    'eggs update',
];
exports.default = Update;
