"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * class: daddy.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
const utils_1 = tslib_1.__importDefault(require("../classes/utils"));
const pacman_1 = tslib_1.__importDefault(require("../classes/pacman"));
const settings_1 = tslib_1.__importDefault(require("../classes/settings"));
const inquirer = require('inquirer');
const chalk_1 = tslib_1.__importDefault(require("chalk"));
class Daddy {
    constructor() {
        this.settings = {};
    }
    async helpMe(loadDefault = false, verbose = false) {
        // Controllo configurazione
        if (!pacman_1.default.configurationCheck()) {
            console.log('- creating configuration dir...');
            await pacman_1.default.configurationInstall(verbose);
        }
        // Controllo prerequisites
        if (!(await pacman_1.default.prerequisitesCheck())) {
            console.log('- installing prerequisites...');
            await pacman_1.default.prerequisitesInstall(verbose);
        }
        // Templates
        if (!pacman_1.default.distroTemplateCheck()) {
            console.log('- distro template install...');
            await pacman_1.default.distroTemplateInstall(verbose);
        }
        // show and edit configuration
        this.settings = new settings_1.default();
        let config = {};
        if (await this.settings.load()) {
            config = this.settings.config;
            let jsonConf;
            if (!loadDefault) {
                jsonConf = await this.editConfig(config);
            }
            else {
                if (config.snapshot_prefix === '') {
                    config.snapshot_prefix = utils_1.default.snapshotPrefix(this.settings.distro.distroId, this.settings.distro.codenameId);
                    config.compression = 'fast';
                }
                jsonConf = JSON.stringify(config);
            }
            const newConf = JSON.parse(jsonConf);
            // salvo le modifiche
            config.snapshot_basename = newConf.snapshot_basename;
            config.snapshot_prefix = newConf.snapshot_prefix;
            config.user_opt = newConf.user_opt;
            config.user_opt_passwd = newConf.user_opt_passwd;
            config.root_passwd = newConf.root_passwd;
            config.theme = newConf.theme;
            await this.settings.save(config);
            console.log();
            console.log(chalk_1.default.cyan('Your configuration was saved on: /etc/penguins-eggs.d'));
            console.log();
            console.log(chalk_1.default.cyan(`You can create a clean ISO with: `) + chalk_1.default.white(`sudo eggs produce`));
            console.log(chalk_1.default.cyan(`or a full personal clone: `) + chalk_1.default.white(`sudo eggs produce --clone`));
            console.log();
            console.log(chalk_1.default.cyan(`If you don't have enough space to remaster, you can mount`));
            console.log(chalk_1.default.cyan(`some remote or local space. Follow the samples:`));
            console.log(chalk_1.default.cyan(`- first, create an hidden mountpoint under the nest:`));
            console.log(chalk_1.default.white(`sudo mkdir /home/eggs/.mnt -p`));
            console.log(chalk_1.default.cyan(`- then, mount remote space:`));
            console.log(chalk_1.default.white(`sudo sshfs -o allow_other root@192.168.1.2:/zfs/iso /home/eggs/.mnt`));
            console.log(chalk_1.default.cyan('- or, mount a local partition:'));
            console.log(chalk_1.default.white(`sudo mount /dev/sdx1 /home/eggs/.mnt`));
            console.log();
            console.log(chalk_1.default.cyan('More help? ') + chalk_1.default.white('eggs mom'));
            // await exec(`cat /etc/penguins-eggs.d/eggs.yaml`)
        }
    }
    /**
     *
     * @param c
     */
    editConfig(c) {
        // Utils.titles('dad')
        console.log(chalk_1.default.cyan('Edit and save Live system parameters'));
        console.log();
        let compressionOpt = 0;
        if (c.compression === 'xz') {
            compressionOpt = 1;
        }
        else if (c.compression === 'xz -Xbcj x86') {
            compressionOpt = 2;
        }
        if (c.snapshot_prefix === '') {
            c.snapshot_prefix = utils_1.default.snapshotPrefix(this.settings.distro.distroId, this.settings.distro.codenameId);
        }
        return new Promise(function (resolve) {
            const questions = [
                {
                    type: 'input',
                    name: 'snapshot_prefix',
                    message: 'LiveCD iso prefix: ',
                    default: c.snapshot_prefix,
                },
                {
                    type: 'input',
                    name: 'snapshot_basename',
                    message: 'LiveCD iso basename: ',
                    default: c.snapshot_basename,
                },
                {
                    type: 'input',
                    name: 'user_opt',
                    message: 'LiveCD user:',
                    default: c.user_opt,
                },
                {
                    type: 'input',
                    name: 'user_opt_passwd',
                    message: 'LiveCD user password: ',
                    default: c.user_opt_passwd,
                },
                {
                    type: 'input',
                    name: 'root_passwd',
                    message: 'LiveCD root password: ',
                    default: c.root_passwd,
                },
                {
                    type: 'list',
                    name: 'compression',
                    message: 'LiveCD compression: ',
                    choices: ['fast', 'max'],
                    default: compressionOpt,
                },
            ];
            inquirer.prompt(questions).then(function (options) {
                resolve(JSON.stringify(options));
            });
        });
    }
}
exports.default = Daddy;
