"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: install.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 krill_prepare_1 = tslib_1.__importDefault(require("../krill/krill-prepare"));
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const axios_1 = tslib_1.__importDefault(require("axios"));
const node_https_1 = tslib_1.__importDefault(require("node:https"));
const agent = new node_https_1.default.Agent({
    rejectUnauthorized: false,
});
/**
 * Class Krill
 */
class Install extends core_1.Command {
    /**
     * Execute
     */
    async run() {
        utils_1.default.titles(this.id + ' ' + this.argv);
        const { flags } = await this.parse(Install);
        let custom = flags.custom;
        let unattended = flags.unattended;
        if (unattended) {
            custom = 'us';
        }
        // krillConfig
        let krillConfig = {};
        if (custom !== undefined) {
            const fname = node_path_1.default.basename(custom);
            const url = `https://raw.githubusercontent.com/pieroproietti/penguins-wardrobe/main/config/${fname}.yaml`;
            let res;
            await axios_1.default.get(url, { httpsAgent: agent })
                .then(function (response) {
                krillConfig = js_yaml_1.default.load(response.data);
            })
                .catch(function (error) {
                const content = fs_1.default.readFileSync('/etc/penguins-eggs.d/krill.yaml', 'utf8');
                krillConfig = js_yaml_1.default.load(content);
            });
        }
        // nointeractive
        const nointeractive = flags.nointeractive;
        // halt
        let halt = flags.halt;
        // hostname
        const ip = flags.ip;
        const random = flags.random;
        let domain = '';
        if (flags.domain) {
            domain = flags.domain;
        }
        // swap
        const suspend = flags.suspend;
        const small = flags.small;
        const none = flags.none;
        let crypted = flags.crypted;
        const pve = flags.pve;
        if (pve) {
            crypted = false;
        }
        const verbose = flags.verbose;
        if (utils_1.default.isRoot()) {
            if (utils_1.default.isLive()) {
                const krill = new krill_prepare_1.default(unattended, nointeractive, halt);
                await krill.prepare(krillConfig, ip, random, domain, suspend, small, none, crypted, pve, verbose);
            }
            else {
                utils_1.default.warning('You are in an installed system!');
            }
        }
        else {
            utils_1.default.useRoot(this.id);
        }
    }
}
Install.flags = {
    crypted: core_1.Flags.boolean({ char: 'k', description: 'Crypted CLI installation' }),
    custom: core_1.Flags.string({ char: 'c', description: 'custom unattended configuration' }),
    domain: core_1.Flags.string({ char: 'd', description: 'Domain name, defult: .local' }),
    halt: core_1.Flags.boolean({ char: 'H', description: 'Halt the system after installation' }),
    help: core_1.Flags.help({ char: 'h' }),
    ip: core_1.Flags.boolean({ char: 'i', description: 'hostname as ip, eg: ip-192-168-1-33' }),
    nointeractive: core_1.Flags.boolean({ char: 'n', description: 'no user interaction' }),
    none: core_1.Flags.boolean({ char: 'N', description: 'Swap none: 256M' }),
    pve: core_1.Flags.boolean({ char: 'p', description: 'Proxmox VE install' }),
    random: core_1.Flags.boolean({ char: 'r', description: 'Add random to hostname, eg: colibri-ay412dt' }),
    small: core_1.Flags.boolean({ char: 's', description: 'Swap small: RAM' }),
    suspend: core_1.Flags.boolean({ char: 'S', description: 'Swap suspend: RAM x 2' }),
    unattended: core_1.Flags.boolean({ char: 'u', description: 'Unattended installation' }),
    verbose: core_1.Flags.boolean({ char: 'v', description: 'Verbose' }),
};
Install.aliases = ['krill'];
Install.description = 'krill: the CLI system installer - the egg became a penguin!';
Install.examples = [
    'sudo eggs install',
    'sudo eggs install --unattended --halt',
    'sudo eggs install --custom it',
];
exports.default = Install;
