"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: calamares.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
const core_1 = require("@oclif/core");
const path_1 = tslib_1.__importDefault(require("path"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const utils_1 = tslib_1.__importDefault(require("../classes/utils"));
const settings_1 = tslib_1.__importDefault(require("../classes/settings"));
const incubator_1 = tslib_1.__importDefault(require("../classes/incubation/incubator"));
const pacman_1 = tslib_1.__importDefault(require("../classes/pacman"));
class Calamares extends core_1.Command {
    constructor() {
        super(...arguments);
        this.remix = {};
        this.incubator = {};
        this.settings = {};
    }
    async run() {
        utils_1.default.titles(this.id + ' ' + this.argv);
        this.settings = new settings_1.default();
        const { flags } = await this.parse(Calamares);
        let verbose = flags.verbose;
        let remove = flags.remove;
        let install = flags.install;
        let release = flags.release;
        let theme = 'eggs';
        if (flags.theme !== undefined) {
            theme = flags.theme;
            if (theme.includes('/')) {
                if (theme.endsWith('/')) {
                    theme = theme.substring(0, theme.length - 1);
                }
            }
            else {
                const wpath = `/home/${await utils_1.default.getPrimaryUser()}/.wardrobe/vendors/`;
                theme = wpath + flags.theme;
            }
            theme = path_1.default.resolve(theme);
            if (!fs_1.default.existsSync(theme + '/theme')) {
                console.log('Cannot find theme: ' + theme);
                process.exit();
            }
        }
        console.log(`theme: ${theme}`);
        let policies = flags.policies;
        const nointeractive = flags.nointeractive;
        if (utils_1.default.isRoot(this.id)) {
            let installer = 'krill';
            if (pacman_1.default.isInstalledGui()) {
                installer = 'calamares';
            }
            if (installer === 'calamares') {
                if (!nointeractive || await utils_1.default.customConfirm('Select yes to continue...')) {
                    if (remove) {
                        if (pacman_1.default.calamaresExists()) {
                            await pacman_1.default.calamaresRemove();
                            if (await this.settings.load()) {
                                this.settings.config.force_installer = false;
                                this.settings.save(this.settings.config);
                            }
                        }
                        process.exit();
                    }
                    /**
                     * Install
                     */
                    if (install) {
                        utils_1.default.warning('Installing calamares');
                        await pacman_1.default.calamaresInstall();
                        if (await this.settings.load()) {
                            this.settings.config.force_installer = true;
                            this.settings.save(this.settings.config);
                            policies = true;
                        }
                    }
                }
                /**
                * Configure
                */
                if (await this.settings.load()) {
                    utils_1.default.warning(`Configuring ${installer}`);
                    await this.settings.loadRemix(this.settings.config.snapshot_basename, theme);
                    const isClone = false;
                    this.incubator = new incubator_1.default(this.settings.remix, this.settings.distro, this.settings.config.user_opt, theme, isClone, verbose);
                    await this.incubator.config(release);
                    /**
                     * policies
                     */
                    if (policies) {
                        utils_1.default.warning('Configuring policies');
                        await pacman_1.default.calamaresPolicies();
                    }
                }
            }
        }
    }
}
Calamares.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    install: core_1.Flags.boolean({ char: 'i', description: "install calamares and its dependencies" }),
    nointeractive: core_1.Flags.boolean({ char: 'n', description: 'no user interaction' }),
    policies: core_1.Flags.boolean({ char: 'p', description: 'configure calamares policies' }),
    release: core_1.Flags.boolean({ char: 'r', description: "release: remove calamares and all its dependencies after the installation" }),
    remove: core_1.Flags.boolean({ description: "remove calamares and its dependencies" }),
    theme: core_1.Flags.string({ description: 'theme/branding for eggs and calamares' }),
    verbose: core_1.Flags.boolean({ char: 'v' }),
};
Calamares.description = 'configure calamares or install or configure it';
Calamares.examples = [
    'sudo eggs calamares',
    'sudo eggs calamares --install',
    'sudo eggs calamares --install --theme=/path/to/theme',
    'sudo eggs calamares --remove',
];
exports.default = Calamares;
