"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: produce.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 ovary_1 = tslib_1.__importDefault(require("../classes/ovary"));
const compressors_1 = tslib_1.__importDefault(require("../classes/compressors"));
const config_1 = tslib_1.__importDefault(require("./config"));
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
const node_path_1 = tslib_1.__importDefault(require("node:path"));
class Produce extends core_1.Command {
    async run() {
        utils_1.default.titles(this.id + ' ' + this.argv);
        const { flags } = await this.parse(Produce);
        if (utils_1.default.isRoot()) {
            /**
             * ADDONS dei vendors
             * Fino a 3
             */
            const addons = [];
            if (flags.addons) {
                const addons = flags.addons; // array
                for (let addon of addons) {
                    // se non viene specificato il vendor il default è eggs
                    if (!addon.includes('//')) {
                        addon = 'eggs/' + addon;
                    }
                    const dirAddon = node_path_1.default.resolve(__dirname, `../../addons/${addon}`);
                    if (!node_fs_1.default.existsSync(dirAddon)) {
                        console.log(dirAddon);
                        utils_1.default.warning('addon: ' + chalk_1.default.white(addon) + ' not found, terminate!');
                        process.exit();
                    }
                    const vendorAddon = addon.slice(0, Math.max(0, addon.search('/')));
                    const nameAddon = addon.substring(addon.search('/') + 1, addon.length);
                    if (nameAddon === 'theme') {
                        flags.theme = vendorAddon;
                    }
                }
            }
            /**
             * composizione dei flag
             */
            // filters
            const filters = {};
            filters.custom = false;
            filters.dev = false;
            filters.homes = false;
            filters.usr = false;
            if (flags.filters) {
                if (flags.filters.includes('custom')) {
                    filters.custom = true;
                }
                if (flags.filters.includes('dev')) {
                    filters.dev = true;
                }
                if (flags.filters.includes('homes')) {
                    filters.homes = true;
                }
                if (flags.filters.includes('usr')) {
                    filters.usr = true;
                }
            }
            let prefix = '';
            if (flags.prefix !== undefined) {
                prefix = flags.prefix;
            }
            let basename = ''; // se vuoto viene definito da loadsetting (default nome dell'host)
            if (flags.basename !== undefined) {
                basename = flags.basename;
            }
            const compressors = new compressors_1.default();
            await compressors.populate();
            let compression = compressors.fast();
            if (flags.max) {
                compression = compressors.max();
            }
            else if (flags.standard) {
                compression = compressors.standard();
            }
            const release = flags.release;
            const cryptedclone = flags.cryptedclone;
            const clone = flags.clone;
            const verbose = flags.verbose;
            const scriptOnly = flags.script;
            const yolkRenew = flags.yolk;
            const nointeractive = flags.nointeractive;
            const noicons = flags.noicons;
            // if clone or cryptedclone unsecure = true
            const unsecure = flags.unsecure || clone || cryptedclone;
            /**
             * theme: if not defined will use eggs
             */
            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 = node_path_1.default.resolve(theme);
                if (!node_fs_1.default.existsSync(theme + '/theme')) {
                    console.log('Cannot find theme: ' + theme);
                    process.exit();
                }
            }
            const i = await config_1.default.thatWeNeed(nointeractive, noicons, verbose, cryptedclone);
            if ((i.needApt || i.configurationInstall || i.configurationRefresh || i.distroTemplate) && (await utils_1.default.customConfirm('Select yes to continue...'))) {
                await config_1.default.install(i, nointeractive, noicons, verbose);
            }
            const myAddons = {};
            if (flags.addons != undefined) {
                if (flags.addons.includes('adapt')) {
                    myAddons.adapt = true;
                }
                if (flags.addons.includes('ichoice')) {
                    myAddons.ichoice = true;
                }
                if (flags.addons.includes('pve')) {
                    myAddons.pve = true;
                }
                if (flags.addons.includes('rsupport')) {
                    myAddons.rsupport = true;
                }
            }
            utils_1.default.titles(this.id + ' ' + this.argv);
            const ovary = new ovary_1.default();
            utils_1.default.warning('Produce an egg...');
            if (await ovary.fertilization(prefix, basename, theme, compression, !nointeractive)) {
                await ovary.produce(clone, cryptedclone, scriptOnly, yolkRenew, release, myAddons, filters, nointeractive, noicons, unsecure, verbose);
                ovary.finished(scriptOnly);
            }
        }
        else {
            utils_1.default.useRoot(this.id);
        }
    }
}
Produce.flags = {
    addons: core_1.Flags.string({ multiple: true, description: 'addons to be used: adapt, ichoice, pve, rsupport' }),
    basename: core_1.Flags.string({ description: 'basename' }),
    clone: core_1.Flags.boolean({ char: 'c', description: 'clone' }),
    cryptedclone: core_1.Flags.boolean({ char: 'C', description: 'crypted clone' }),
    filters: core_1.Flags.string({ multiple: true, description: 'filters can be used: custom. dev, homes, usr' }),
    help: core_1.Flags.help({ char: 'h' }),
    max: core_1.Flags.boolean({ char: 'm', description: 'max compression' }),
    noicons: core_1.Flags.boolean({ char: 'N', description: 'no icons on desktop' }),
    nointeractive: core_1.Flags.boolean({ char: 'n', description: 'no user interaction' }),
    prefix: core_1.Flags.string({ char: 'p', description: 'prefix' }),
    release: core_1.Flags.boolean({ description: 'release: max compression, remove penguins-eggs and calamares after installation' }),
    script: core_1.Flags.boolean({ char: 's', description: 'script mode. Generate scripts to manage iso build' }),
    standard: core_1.Flags.boolean({ char: 'f', description: 'standard compression' }),
    theme: core_1.Flags.string({ description: 'theme for livecd, calamares branding and partitions' }),
    unsecure: core_1.Flags.boolean({ char: 'u', description: '/root contents are included on live' }),
    verbose: core_1.Flags.boolean({ char: 'v', description: 'verbose' }),
    yolk: core_1.Flags.boolean({ char: 'y', description: 'force yolk renew' }),
};
Produce.description = 'produce a live image from your system whithout your data';
Produce.examples = [
    'sudo eggs produce',
    'sudo eggs produce --standard',
    'sudo eggs produce --max',
    'sudo eggs produce --max --basename=colibri',
    'sudo eggs produce --cryptedclone',
    'sudo eggs produce --clone',
    'sudo eggs produce --basename=colibri',
    'sudo eggs produce --basename=colibri --theme theme --addons adapt',
];
exports.default = Produce;
