"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: config.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
const core_1 = require("@oclif/core");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const utils_1 = tslib_1.__importDefault(require("../classes/utils"));
const pacman_1 = tslib_1.__importDefault(require("../classes/pacman"));
const bleach_1 = tslib_1.__importDefault(require("../classes/bleach"));
const dependencies_1 = require("../lib/dependencies");
const utils_2 = require("../lib/utils");
/**
 *
 */
class Config extends core_1.Command {
    async run() {
        const { flags } = await this.parse(Config);
        const nointeractive = flags.nointeractive;
        const noicons = flags.noicons;
        const verbose = flags.verbose;
        if (!nointeractive) {
            utils_1.default.titles(this.id + ' ' + this.argv);
            if (utils_1.default.isDebPackage()) {
                utils_1.default.warning('running as package .deb');
            }
            else if (utils_1.default.isSources()) {
                utils_1.default.warning('running as sources');
            }
        }
        if (utils_1.default.isRoot(this.id)) {
            if (flags.clean) {
                utils_1.default.warning('removing old configurations');
                await (0, utils_2.exec)('rm /etc/penguins-eggs.d -rf');
            }
            /**
             * Se stiamo utilizzando eggs da sorgenti
             * Aggiungo autocomplete e manPage
             */
            if (utils_1.default.isSources()) {
                utils_1.default.warning('creating autocomplete...');
                await pacman_1.default.autocompleteInstall(verbose);
                utils_1.default.warning('creating eggs man page...');
                await pacman_1.default.manPageInstall(verbose);
            }
            // Vediamo che cosa c'è da fare...
            utils_1.default.warning('what we need?');
            const i = await Config.thatWeNeed(nointeractive, noicons, verbose);
            if (i.needApt || i.configurationInstall || i.configurationRefresh || i.distroTemplate) {
                if (nointeractive) {
                    await Config.install(i, nointeractive, noicons, verbose);
                }
                else if (await utils_1.default.customConfirm()) {
                    await Config.install(i, nointeractive, noicons, verbose);
                }
            }
        }
        else {
            utils_1.default.useRoot(this.id);
        }
    }
    /**
     *
     *
     * @param verbose
     */
    static async thatWeNeed(nointeractive = false, noicons = false, verbose = false, cryptedclone = false) {
        const i = {};
        i.distroTemplate = !pacman_1.default.distroTemplateCheck();
        if (utils_1.default.uefiArch() !== 'i386') {
            i.efi = !pacman_1.default.isUefi();
        }
        if (!cryptedclone && !(pacman_1.default.calamaresExists()) && pacman_1.default.isInstalledGui() && pacman_1.default.isCalamaresAvailable() && !pacman_1.default.packageIsInstalled('live-installer')) {
            if (noicons) {
                i.calamares = false;
            }
            else {
                utils_1.default.warning('Config: you are on a graphic system, I suggest to install the GUI installer calamares');
                i.calamares = nointeractive ? false : await utils_1.default.customConfirm('Want You install calamares?');
            }
        }
        i.configurationInstall = !pacman_1.default.configurationCheck();
        if (!i.configurationInstall) {
            i.configurationRefresh = !pacman_1.default.configurationMachineNew();
        }
        i.prerequisites = !(await pacman_1.default.prerequisitesCheck());
        if (i.efi || i.calamares || i.prerequisites) {
            i.needApt = true;
        }
        /**
         * Visualizza cosa c'è da fare
         */
        if (!nointeractive) {
            if (i.needApt) {
                console.log('- update the system');
                if (pacman_1.default.distro().familyId === 'debian') {
                    console.log(chalk_1.default.yellow('  apt-get update --yes\n'));
                }
            }
            if (i.efi && pacman_1.default.distro().familyId === 'debian' && utils_1.default.uefiArch() !== 'i386') {
                console.log('- install efi packages');
                console.log(chalk_1.default.yellow('  apt install -y grub-efi-' + utils_1.default.uefiArch() + '-bin\n'));
            }
            if (i.prerequisites) {
                console.log('- install packages prerequisites');
                const packages = pacman_1.default.packages(verbose);
                if (packages.length > 0) {
                    console.log(chalk_1.default.yellow('  will install: ' + (0, dependencies_1.array2spaced)(packages)));
                }
            }
            if (i.configurationInstall) {
                console.log("- creating configuration's files...");
                pacman_1.default.configurationInstall(verbose);
            }
            if (i.configurationRefresh) {
                console.log("- refreshing configuration's files...");
                pacman_1.default.configurationFresh();
            }
            if (i.distroTemplate) {
                console.log('- copy distro template\n');
            }
            if (i.calamares) {
                console.log('- install calamares');
                if (pacman_1.default.distro().familyId === 'debian') {
                    const packages = pacman_1.default.debs4calamares;
                    console.log(chalk_1.default.yellow('  will install: ' + (0, dependencies_1.array2spaced)(packages) + '\n'));
                }
            }
            if (i.needApt) {
                console.log('- cleaning packages\n');
            }
            if (i.configurationInstall) {
                console.log('- creating/updating configuration');
                console.log('  files: ' + chalk_1.default.yellow('/etc/penguins-eggs.d/eggs.yaml'));
            }
            else if (i.configurationRefresh) {
                console.log('- refreshing configuration for new machine');
            }
            if (i.needApt) {
                utils_1.default.warning("Be sure! It's just a series of apt install from your repo.\nYou can follows them using flag --verbose");
            }
        }
        return i;
    }
    /**
     *
     * @param i
     * @param verbose
     */
    static async install(i, nointeractive = false, noicons = false, verbose = false) {
        const echo = utils_1.default.setEcho(verbose);
        utils_1.default.warning('config: so, we install');
        if (i.configurationInstall) {
            utils_1.default.warning('creating configuration...');
            await pacman_1.default.configurationInstall(verbose);
        }
        if (i.configurationRefresh) {
            utils_1.default.warning('refreshing configuration for new machine...');
            await pacman_1.default.configurationMachineNew(verbose);
        }
        if (i.distroTemplate) {
            utils_1.default.warning('copying distro templates...');
            await pacman_1.default.distroTemplateInstall(verbose);
        }
        if (i.needApt && !nointeractive && !nointeractive && pacman_1.default.distro().familyId === 'debian') {
            utils_1.default.warning('updating system...');
            await (0, utils_2.exec)('apt-get update --yes', echo);
        }
        if (i.efi) {
            if (nointeractive) {
                utils_1.default.error('config: you are on a system UEFI');
                utils_1.default.warning('I suggest You to install grub-efi-' + utils_1.default.uefiArch() + '-bin before to produce your ISO.\nJust write:\n    sudo apt install grub-efi-' + utils_1.default.uefiArch());
            }
            else if (pacman_1.default.distro().familyId === 'debian') {
                utils_1.default.warning('Installing uefi support...');
                await (0, utils_2.exec)('apt-get install grub-efi-' + utils_1.default.uefiArch() + '-bin --yes', echo);
            }
        }
        if (i.prerequisites) {
            if (nointeractive) {
                utils_1.default.warning("Can't install prerequisites now...");
            }
            else {
                await pacman_1.default.prerequisitesInstall(verbose);
            }
        }
        if (!noicons) { // se VOGLIO le icone
            if (i.calamares && pacman_1.default.isCalamaresAvailable()) {
                if (nointeractive) {
                    utils_1.default.warning('I suggest You to install calamares GUI installer before to produce your ISO.\nJust write:\n    sudo eggs calamares --install');
                }
                else {
                    utils_1.default.warning('Installing calamares...');
                    await pacman_1.default.calamaresInstall(verbose);
                    await pacman_1.default.calamaresPolicies();
                }
            }
        }
        if (i.needApt && !nointeractive) {
            utils_1.default.warning('cleaning the system...');
            if (pacman_1.default.distro().familyId === 'debian') {
                const bleach = new bleach_1.default();
                await bleach.clean(verbose);
            }
        }
    }
}
Config.flags = {
    clean: core_1.Flags.boolean({ char: 'c', description: 'remove old configuration before to create new one' }),
    help: core_1.Flags.help({ char: 'h' }),
    nointeractive: core_1.Flags.boolean({ char: 'n', description: 'no user interaction' }),
    noicons: core_1.Flags.boolean({ char: 'N', description: 'no icons' }),
    verbose: core_1.Flags.boolean({ char: 'v', description: 'verbose' }),
};
Config.description = 'Configure and install prerequisites deb packages to run it';
Config.examples = [
    'sudo eggs config',
    'sudo eggs config --clean',
    'sudo eggs config --clean --nointeractive',
];
exports.default = Config;
