"use strict";
/**
 * penguins-eggs
 * krill modules: packages.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 * https://stackoverflow.com/questions/23876782/how-do-i-split-a-typescript-class-into-multiple-files
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const utils_1 = require("../../lib/utils");
const utils_2 = tslib_1.__importDefault(require("../../classes/utils"));
const pacman_1 = tslib_1.__importDefault(require("../../classes/pacman"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
/**
 *
 * @param this
 */
async function packages() {
    const echoYes = utils_2.default.setEcho(true);
    let modulePath = '/etc/penguins-eggs.d/krill/';
    if (pacman_1.default.packageIsInstalled('calamares')) {
        modulePath = '/etc/calamares/';
    }
    const config_file = `${this.installTarget}${modulePath}modules/packages.conf`;
    let remove = [];
    let install = [];
    if (fs_1.default.existsSync(config_file)) {
        const packages = js_yaml_1.default.load(fs_1.default.readFileSync(config_file, 'utf-8'));
        const operations = JSON.parse(JSON.stringify(packages.operations));
        remove = operations[0].remove;
        if (operations.length > 1) {
            install = operations[1].install;
        }
        if (operations !== undefined) {
            if (packages.backend === 'apt') {
                /**
                         * apt: Debian/Devuan/Ubuntu
                         */
                if (remove.length > 0) {
                    let cmd = `chroot ${this.installTarget} apt-get purge -y `;
                    for (const elem of remove) {
                        cmd += elem + ' ';
                    }
                    await (0, utils_1.exec)(`${cmd} ${this.toNull}`, this.echo);
                }
                if (install.length > 0) {
                    let cmd = `chroot ${this.installTarget} apt-get install -y `;
                    for (const elem of install) {
                        cmd += elem + ' ';
                    }
                    await (0, utils_1.exec)(`chroot ${this.installTarget} apt-get update ${this.toNull}`, this.echo);
                    await (0, utils_1.exec)(`${cmd} ${this.toNull}`, this.echo);
                }
            }
            else if (packages.backend === 'pacman') {
                /**
                         * pacman: arch/manjaro
                         */
                if (remove.length > 0) {
                    let cmd = `chroot ${this.installTarget} pacman -R\n`;
                    for (const elem of remove) {
                        cmd += elem + ' ';
                    }
                    await (0, utils_1.exec)(`${cmd} ${echoYes}`, this.echo);
                }
                if (install.length > 0) {
                    for (const elem of install) {
                        await (0, utils_1.exec)(`chroot ${this.installTarget} pacman -S ${elem}`, echoYes);
                    }
                }
            }
        }
    }
}
exports.default = packages;
