"use strict";
/**
 * penguins-eggs
 * class: pacman.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const shelljs_1 = tslib_1.__importDefault(require("shelljs"));
const utils_1 = tslib_1.__importDefault(require("./utils"));
const distro_1 = tslib_1.__importDefault(require("./distro"));
const settings_1 = tslib_1.__importDefault(require("./settings"));
const node_child_process_1 = require("node:child_process");
const utils_2 = require("../lib/utils");
const debian_1 = tslib_1.__importDefault(require("./families/debian"));
const fedora_1 = tslib_1.__importDefault(require("./families/fedora"));
const archlinux_1 = tslib_1.__importDefault(require("./families/archlinux"));
const suse_1 = tslib_1.__importDefault(require("./families/suse"));
const config_file = '/etc/penguins-eggs.d/eggs.yaml';
const config_tools = '/etc/penguins-eggs.d/tools.yaml';
/**
 * Utils: general porpouse utils
 * @remarks all the utilities
 */
class Pacman {
    constructor() {
        this.distro = {};
        this.remix = {};
    }
    /**
     *
     * @returns
     */
    static distro() {
        const remix = {};
        const distro = new distro_1.default(remix);
        return distro;
    }
    /**
     *
     * @returns grub
     */
    static whichGrubIsInstalled() {
        let grubInstalled = '';
        if (this.distro().familyId === 'debian') {
            if (this.packageIsInstalled('grub-common')) {
                grubInstalled = 'grub';
            }
        }
        else if (this.distro().familyId === 'fedora') {
            if (this.packageIsInstalled('grub2-common.noarch')) {
                grubInstalled = 'grub2';
            }
        }
        else if (this.distro().familyId === 'archlinux') {
            if (this.packageIsInstalled('grub')) {
                grubInstalled = 'grub';
            }
        }
        else if (this.distro().familyId === 'suse' && this.packageIsInstalled('grub2')) {
            grubInstalled = 'grub2';
        }
        return grubInstalled;
    }
    /**
     * check if it's installed xorg
     * @returns
     */
    static isInstalledXorg() {
        let installed = false;
        if (this.distro().familyId === 'debian') {
            if (debian_1.default.packageIsInstalled('xserver-xorg-core')) {
                installed = true;
            }
        }
        else if (this.distro().familyId === 'fedora') {
            if (fedora_1.default.packageIsInstalled('xorg-x11-server-Xorg.x86_64')) {
                installed = true;
            }
        }
        else if (this.distro().familyId === 'archlinux') {
            if (archlinux_1.default.packageIsInstalled('xorg-server-common')) {
                installed = true;
            }
        }
        else if (this.distro().familyId === 'suse') {
            if (suse_1.default.packageIsInstalled('xorg-x11-server')) {
                installed = true;
            }
        }
        return installed;
    }
    /**
     * check if it's installed wayland
     * @returns true if wayland
     */
    static isInstalledWayland() {
        let installed = false;
        if (this.distro().familyId === 'debian') {
            if (debian_1.default.packageIsInstalled('xwayland')) {
                installed = true;
            }
        }
        else if (this.distro().familyId === 'fedora') {
            if (fedora_1.default.packageIsInstalled('xorg-x11-server-Xwayland*')) {
                installed = true;
            }
        }
        else if (this.distro().familyId === 'archlinux') {
            if (archlinux_1.default.packageIsInstalled('xwayland')) {
                installed = true;
            }
        }
        else if (this.distro().familyId === 'suse') {
            if (suse_1.default.packageIsInstalled('xwayland*')) {
                installed = true;
            }
        }
        return installed;
    }
    /**
     * Check se la macchina ha grub adatto ad efi
     * Forse conviene spostarlo in pacman
     */
    static isUefi() {
        let isUefi = false;
        if (this.distro().familyId === 'debian') {
            if (utils_1.default.uefiArch() !== 'i386') {
                if (this.packageIsInstalled('grub-efi-' + utils_1.default.uefiArch() + '-bin')) {
                    isUefi = true;
                }
            }
        }
        else if (Pacman.distro().familyId === 'fedora') {
            isUefi = true;
        }
        else if (Pacman.distro().familyId === 'archlinux') {
            isUefi = true;
        }
        else if (Pacman.distro().familyId === 'suse') {
            isUefi = true;
        }
        return isUefi;
    }
    /**
     *
     * @returns true se GUI
     */
    static isInstalledGui() {
        return this.isInstalledXorg() || this.isInstalledWayland();
    }
    /**
     * controlla se è operante xserver-xorg-core
     */
    static isRunningXorg() {
        return process.env.XDG_SESSION_TYPE === 'x11';
    }
    /**
     * Constrolla se è operante wayland
     */
    static isRunningWayland() {
        return process.env.XDG_SESSION_TYPE === 'wayland';
    }
    /**
     * Check if the system is GUI able
     */
    static isRunningGui() {
        return this.isRunningXorg() || this.isRunningWayland();
    }
    /**
     * Check if the system is just CLI
     */
    static isRunningCli() {
        return !this.isRunningGui();
    }
    /**
     * Crea array packages dei pacchetti da installare
     *
     * probabilmente non usata
     *
     */
    static packages(remove = false, verbose = false) {
        let packages = [];
        /*
        if (this.distro().familyId === 'debian') {
          packages = Debian.packages(remove, verbose)
        } else if (this.distro().familyId === 'fedora') {
          packages = Fedora.packages(remove, verbose)
        } else if (this.distro().familyId === 'archlinux') {
          packages = Archlinux.packages(remove, verbose)
        } else if (this.distro().familyId === 'suse') {
          packages = Suse.packages(remove, verbose)
        }
        */
        return packages;
    }
    /**
     * Restituisce VERO se i prerequisiti sono installati
     */
    static async prerequisitesCheck(verbose = false) {
        let installed = true;
        const packages = this.packages(false, verbose);
        if (packages.length > 0) {
            installed = false;
        }
        return installed;
    }
    /**
     *
     */
    static async prerequisitesInstall(verbose = true) {
        let retVal = false;
        if (this.distro().familyId === 'debian') {
            retVal = await debian_1.default.prerequisitesInstall(verbose);
        }
        else if (this.distro().familyId === 'fedora') {
            retVal = await fedora_1.default.prerequisitesInstall(verbose);
        }
        else if (this.distro().familyId === 'archlinux') {
            retVal = await archlinux_1.default.prerequisitesInstall(verbose);
        }
        else if (this.distro().familyId === 'suse') {
            retVal = await suse_1.default.prerequisitesInstall(verbose);
        }
        return retVal;
    }
    /**
     * return true if calamares is installed
     */
    static calamaresExists() {
        return this.commandIsInstalled('calamares');
    }
    /**
     * Controlla se calamares è installabile
     * @returns
     */
    static isCalamaresAvailable() {
        let result = this.distro().isCalamaresAvailable;
        if (process.arch === 'arm' || process.arch === 'arm64') {
            result = false;
        }
        return result;
    }
    /**
     *
     */
    static async calamaresInstall(verbose = false) {
        if (this.isInstalledGui()) {
            if (this.distro().familyId === 'debian') {
                await debian_1.default.calamaresInstall(verbose);
            }
            else if (this.distro().familyId === 'fedora') {
                await fedora_1.default.calamaresInstall(verbose);
            }
            else if (this.distro().familyId === 'archlinux') {
                if (this.distro().distroId === 'ManjaroLinux') {
                    const cmd = `pacman -Sy --noconfirm calamares`;
                    try {
                        await (0, utils_2.exec)(cmd, utils_1.default.setEcho(true));
                    }
                    catch {
                        utils_1.default.error(`manjaro: ${cmd}`); // + e.error)
                    }
                }
                else {
                    await archlinux_1.default.calamaresInstall(verbose);
                }
            }
            else if (this.distro().familyId === 'suse') {
                await suse_1.default.calamaresInstall(verbose);
            }
            // remove others calamares links
            await (0, utils_2.exec)('rm -f /usr/share/applications/calamares-eggs-debugging.desktop');
            await (0, utils_2.exec)('rm -f /usr/share/applications/calamares-eggs.desktop');
            await (0, utils_2.exec)('rm -f /usr/share/applications/calamares.desktop');
        }
    }
    /**
     * calamaresPolicies
     */
    static async calamaresPolicies() {
        if (this.distro().familyId === 'debian') {
            await debian_1.default.calamaresPolicies();
        }
        else if (this.distro().familyId === 'fedora') {
            await fedora_1.default.calamaresPolicies();
        }
        else if (this.distro().familyId === 'archlinux') {
            await archlinux_1.default.calamaresPolicies();
        }
        else if (this.distro().familyId === 'suse') {
            await suse_1.default.calamaresPolicies();
        }
    }
    /**
     *
     */
    static async calamaresRemove(verbose = true) {
        let retVal = false;
        if (this.distro().familyId === 'debian') {
            retVal = await debian_1.default.calamaresRemove(verbose);
        }
        else if (this.distro().familyId === 'fedora') {
            retVal = await fedora_1.default.calamaresRemove(verbose);
        }
        else if (this.distro().familyId === 'archlinux') {
            retVal = await archlinux_1.default.calamaresRemove(verbose);
        }
        else if (this.distro().familyId === 'suse') {
            retVal = await suse_1.default.calamaresRemove(verbose);
        }
        return retVal;
    }
    /**
     * Restituisce VERO se i file di configurazione SONO presenti
     */
    static configurationCheck() {
        const confExists = node_fs_1.default.existsSync(config_file);
        return confExists;
    }
    /**
     * Ritorna vero se machine-id è uguale
     */
    static async configurationMachineNew(verbose = false) {
        const settings = new settings_1.default();
        await settings.load();
        const result = utils_1.default.machineId() !== settings.config.machine_id;
        if (verbose && result) {
            console.log('configurationMachineNew: True');
        }
        return result;
    }
    /**
     *
     */
    static async configurationFresh() {
        const config = {};
        config.version = utils_1.default.getPackageVersion();
        config.snapshot_dir = '/home/eggs';
        config.snapshot_prefix = '';
        config.snapshot_excludes = '/etc/penguins-eggs.d/exclude.list';
        config.snapshot_basename = ''; // before default was hostname
        config.user_opt = 'live';
        config.user_opt_passwd = 'evolution';
        config.root_passwd = 'evolution';
        config.theme = 'eggs';
        config.force_installer = true;
        config.make_efi = true;
        config.make_md5sum = false;
        config.make_isohybrid = true;
        config.compression = 'xz';
        config.ssh_pass = true;
        config.timezone = 'America/New_York';
        const env = process.env;
        config.locales_default = env.LANG !== undefined ? env.LANG : 'en_US.UTF-8';
        config.locales = config.locales_default === 'en_US.UTF-8' ? ['en_US.UTF-8'] : [config.locales_default, 'en_US.UTF-8'];
        config.pmount_fixed = false;
        if (!this.calamaresExists()) {
            config.force_installer = false;
            console.log('Due the lacks of calamares package set force_installer = false');
        }
        if (!Pacman.isUefi() && utils_1.default.uefiArch() !== "i386") {
            config.make_efi = false;
            console.log('Due the lacks of grub-efi-' + utils_1.default.uefiArch() + '-bin package set make_efi = false');
        }
        /**
         * Salvo la configurazione di eggs.yaml
         */
        config.machine_id = utils_1.default.machineId();
        config.vmlinuz = utils_1.default.vmlinuz();
        config.initrd_img = utils_1.default.initrdImg();
        const settings = new settings_1.default();
        await settings.save(config);
    }
    /**
     * Creazione del file di configurazione /etc/penguins-eggs
     */
    static async configurationInstall(links = true, verbose = true) {
        const confRoot = '/etc/penguins-eggs.d';
        if (!node_fs_1.default.existsSync(confRoot)) {
            (0, node_child_process_1.execSync)(`mkdir ${confRoot}`);
        }
        const addons = `${confRoot}/addons`;
        if (node_fs_1.default.existsSync(addons)) {
            (0, node_child_process_1.execSync)(`rm -rf ${addons}`);
        }
        const distros = `${confRoot}/distros`;
        if (node_fs_1.default.existsSync(distros)) {
            (0, node_child_process_1.execSync)(`rm -rf ${distros}`);
        }
        (0, node_child_process_1.execSync)(`mkdir -p ${distros}`);
        /**
         * We use /etc/penguins-eggs.d/init for our init scripts:
         * # unattended.sh -> eggs install --unattended
         * # cuckoo ->        eggs cuckoo
         */
        const init = `${confRoot}/init`;
        if (node_fs_1.default.existsSync(init)) {
            (0, node_child_process_1.execSync)(`rm -rf ${init}`);
        }
        (0, node_child_process_1.execSync)(`mkdir -p ${init}`);
        shelljs_1.default.ln('-s', node_path_1.default.resolve(__dirname, '../../addons'), addons);
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/README.md'), confRoot);
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/tools.yaml'), config_tools);
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/krill.yaml'), confRoot);
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/derivatives.yaml'), confRoot);
        // init
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/init/unattended.sh'), '/etc/penguins-eggs.d/init');
        shelljs_1.default.chmod('+x', '/etc/penguins-eggs.d/init/unattended.sh');
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/init/cuckoo.sh'), '/etc/penguins-eggs.d/init');
        shelljs_1.default.chmod('+x', '/etc/penguins-eggs.d/init/cuckoo.sh');
        // creazione cartella exclude.list.d
        (0, node_child_process_1.execSync)(`mkdir -p /etc/penguins-eggs.d/exclude.list.d`);
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/exclude.list.template'), '/etc/penguins-eggs.d/exclude.list.d');
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/exclude.list.custom'), '/etc/penguins-eggs.d/exclude.list.d');
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/exclude.list.homes'), '/etc/penguins-eggs.d/exclude.list.d');
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../conf/exclude.list.usr'), '/etc/penguins-eggs.d/exclude.list.d');
        await this.configurationFresh();
    }
    /**
     * Rimozione dei file di configurazione
     */
    static async configurationRemove(verbose = true) {
        const echo = utils_1.default.setEcho(verbose);
        if (node_fs_1.default.existsSync('/etc/penguins-eggs.d')) {
            await (0, utils_2.exec)('rm /etc/penguins-eggs.d -rf', echo);
        }
        if (node_fs_1.default.existsSync('/etc/calamares')) {
            await (0, utils_2.exec)('rm /etc/calamares -rf', echo);
        }
    }
    /**
     *
     * @param verbose
     */
    static async autocompleteInstall(verbose = false) {
        if (this.distro().familyId === 'debian') {
            if (Pacman.packageIsInstalled('bash-completion')) {
                if (node_fs_1.default.existsSync('/usr/share/bash-completion/completions/')) {
                    await (0, utils_2.exec)(`cp ${__dirname}/../../scripts/eggs.bash /usr/share/bash-completion/completions/`);
                }
                else if (node_fs_1.default.existsSync('/etc/bash_completion.d/')) {
                    await (0, utils_2.exec)(`cp ${__dirname}/../../scripts/eggs.bash /etc/bash_completion.d/`);
                }
            }
        }
        else if (this.distro().familyId === 'archlinux' && Pacman.packageIsInstalled('bash-completion')) {
            await (0, utils_2.exec)(`cp ${__dirname}/../../scripts/eggs.bash /usr/share/bash-completion/completions/`);
        }
    }
    /**
     * Installa manPage
     */
    static async manPageInstall(verbose = false) {
        const manPageSrc = node_path_1.default.resolve(__dirname, '../../manpages/doc/man/eggs.roll.gz');
        if (node_fs_1.default.existsSync(manPageSrc)) {
            const man1Dir = '/usr/share/man/man1/';
            if (!node_fs_1.default.existsSync(man1Dir)) {
                (0, utils_2.exec)(`mkdir ${man1Dir} -p`);
            }
            const manPageDest = man1Dir + 'eggs.1.gz';
            (0, utils_2.exec)(`cp ${manPageSrc} ${manPageDest}`);
            if (shelljs_1.default.exec('which mandb', { silent: true }).stdout.trim() !== '') {
                await (0, utils_2.exec)('mandb > /dev/null');
                if (verbose) {
                    console.log('manPage eggs installed...');
                }
            }
        }
    }
    /**
     * distroTemplateCheck
     */
    static distroTemplateCheck() {
        const codenameLikeId = this.distro().codenameLikeId;
        return node_fs_1.default.existsSync(`/etc/penguins-eggs.d/distros/${codenameLikeId}`);
    }
    /**
     *
     */
    static async distroTemplateInstall(verbose = false) {
        if (verbose) {
            console.log('distroTemplateInstall');
        }
        const echo = utils_1.default.setEcho(verbose);
        const rootPen = utils_1.default.rootPenguin();
        await (0, utils_2.exec)(`mkdir /etc/penguins-eggs.d/distros/${this.distro().codenameLikeId}`);
        /**
         * Debian 10 - Buster: è il master per tutte le distro
         */
        const buster = `${rootPen}/conf/distros/buster`;
        /**
         * Debian 8 jessie:  eredita grub, isolinux e locales da buster, contiene krill al posto di calamares
         */
        if (this.distro().codenameLikeId === 'jessie') {
            const dest = '/etc/penguins-eggs.d/distros/jessie';
            await (0, utils_2.exec)(`cp -r ${rootPen}/conf/distros/jessie/krill ${dest}/krill`, echo);
            /**
             * Debian 9 stretch:  eredita grub, isolinux e locales da buster, contiene krill al posto di calamares
             */
        }
        else if (this.distro().codenameLikeId === 'stretch') {
            const dest = '/etc/penguins-eggs.d/distros/stretch';
            await (0, utils_2.exec)(`cp -r ${rootPen}/conf/distros/jessie/krill ${dest}/krill`, echo);
            /**
             * Debian 10 buster: eredita tutto da buster
             */
        }
        else if (this.distro().codenameLikeId === 'buster') {
            const dest = '/etc/penguins-eggs.d/distros/buster';
            await (0, utils_2.exec)(`cp -r ${buster}/calamares ${dest}/calamares`, echo);
            /**
             * Debian 11 bullseye: eredita tutto da buster
             */
        }
        else if (this.distro().codenameLikeId === 'bullseye') {
            const dest = '/etc/penguins-eggs.d/distros/bullseye';
            await (0, utils_2.exec)(`cp -r ${buster}/calamares ${dest}/calamares`, echo);
            /**
             * Debian 12 bookworm: eredita tutto da buster
             */
        }
        else if (this.distro().codenameLikeId === 'bookworm') {
            const dest = '/etc/penguins-eggs.d/distros/bookworm';
            await (0, utils_2.exec)(`cp -r ${buster}/calamares ${dest}/calamares`, echo);
            /**
             * Debian 13 trixie: eredita tutto da buster
             */
        }
        else if (this.distro().codenameLikeId === 'trixie') {
            const dest = '/etc/penguins-eggs.d/distros/trixie';
            await (0, utils_2.exec)(`cp -r ${buster}/calamares ${dest}/calamares`, echo);
            /***********************************************************************************
             * Devuan
             **********************************************************************************/
            /**
             * Devuan beowulf: eredita tutto da buster
             */
        }
        else if (this.distro().codenameLikeId === 'beowulf') {
            const dest = '/etc/penguins-eggs.d/distros/beowulf';
            await (0, utils_2.exec)(`cp -r ${buster}/calamares ${dest}/calamares`, echo);
            /**
             * Devuan chimaera: eredita tutto da buster
             */
        }
        else if (this.distro().codenameLikeId === 'chimaera') {
            const dest = '/etc/penguins-eggs.d/distros/chimaera';
            await (0, utils_2.exec)(`cp -r ${buster}/calamares ${dest}/calamares`, echo);
            /**
             * Devuan daedalus: eredita tutto da buster
             */
        }
        else if (this.distro().codenameLikeId === 'daedalus') {
            const dest = '/etc/penguins-eggs.d/distros/daedalus';
            await (0, utils_2.exec)(`cp -r ${buster}/calamares ${dest}/calamares`, echo);
            /***********************************************************************************
             * Ubuntu
             **********************************************************************************/
            /**
             * Ubuntu 10.04 bionic: eredita da bionic, focal grub ed isolinux, da buster i seguenti
             */
        }
        else if (this.distro().codenameLikeId === 'bionic') {
            const dest = '/etc/penguins-eggs.d/distros/bionic';
            const bionic = `${rootPen}/conf/distros/bionic/*`;
            await (0, utils_2.exec)(`cp -r ${bionic} ${dest}`, echo);
            // Poi da buster
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/cleanup ${dest}/calamares/calamares-modules/cleanup`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk ${dest}/calamares/calamares-modules/sources-yolk`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk-undo ${dest}/calamares/calamares-modules/sources-yolk-undo`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/packages.yml ${dest}/calamares/modules/packages.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/removeuser.yml ${dest}/calamares/modules/removeuser.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/unpackfs.yml ${dest}/calamares/modules/unpackfs.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/displaymanager.yml ${dest}/calamares/modules/displaymanager.yml`, echo);
            /**
             * Ubuntu focal: eredita da focal e buster
             */
        }
        else if (this.distro().codenameLikeId === 'focal') {
            const dest = '/etc/penguins-eggs.d/distros/focal';
            const focal = `${rootPen}/conf/distros/focal/*`;
            await (0, utils_2.exec)(`cp -r ${focal} ${dest}`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/cleanup ${dest}/calamares/calamares-modules/cleanup`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk ${dest}/calamares/calamares-modules/sources-yolk`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk-undo ${dest}/calamares/calamares-modules/sources-yolk-undo`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/packages.yml ${dest}/calamares/modules/packages.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/removeuser.yml ${dest}/calamares/modules/removeuser.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/unpackfs.yml ${dest}/calamares/modules/unpackfs.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/displaymanager.yml ${dest}/calamares/modules/displaymanager.yml`, echo);
            /**
             * Ubuntu 21.10 impish: eredita da focal e buster
             */
        }
        else if (this.distro().codenameLikeId === 'impish') {
            const dest = '/etc/penguins-eggs.d/distros/impish';
            const focal = `${rootPen}/conf/distros/focal/*`;
            await (0, utils_2.exec)(`cp -r ${focal} ${dest}`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/cleanup ${dest}/calamares/calamares-modules/cleanup`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk ${dest}/calamares/calamares-modules/sources-yolk`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk-undo ${dest}/calamares/calamares-modules/sources-yolk-undo`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/packages.yml ${dest}/calamares/modules/packages.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/removeuser.yml ${dest}/calamares/modules/removeuser.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/unpackfs.yml ${dest}/calamares/modules/unpackfs.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/displaymanager.yml ${dest}/calamares/modules/displaymanager.yml`, echo);
            /**
             * Ubuntu 22.04 jammy: eredita da focal e buster
             */
        }
        else if (this.distro().codenameLikeId === 'jammy') {
            const dest = '/etc/penguins-eggs.d/distros/jammy';
            const focal = `${rootPen}/conf/distros/focal/*`;
            await (0, utils_2.exec)(`cp -r ${focal} ${dest}`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/cleanup ${dest}/calamares/calamares-modules/cleanup`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk ${dest}/calamares/calamares-modules/sources-yolk`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk-undo ${dest}/calamares/calamares-modules/sources-yolk-undo`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/packages.yml ${dest}/calamares/modules/packages.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/removeuser.yml ${dest}/calamares/modules/removeuser.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/unpackfs.yml ${dest}/calamares/modules/unpackfs.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/displaymanager.yml ${dest}/calamares/modules/displaymanager.yml`, echo);
            /**
             * Ubuntu lunar/rhino: eredita da devel e buster
             *
             */
        }
        else if (this.distro().codenameLikeId === 'devel') {
            const dest = '/etc/penguins-eggs.d/distros/devel';
            const focal = `${rootPen}/conf/distros/devel/*`;
            await (0, utils_2.exec)(`cp -r ${focal} ${dest}`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/cleanup ${dest}/calamares/calamares-modules/cleanup`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk ${dest}/calamares/calamares-modules/sources-yolk`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/calamares-modules/sources-yolk-undo ${dest}/calamares/calamares-modules/sources-yolk-undo`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/packages.yml ${dest}/calamares/modules/packages.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/removeuser.yml ${dest}/calamares/modules/removeuser.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/unpackfs.yml ${dest}/calamares/modules/unpackfs.yml`, echo);
            await (0, utils_2.exec)(`cp -r ${buster}/calamares/modules/displaymanager.yml ${dest}/calamares/modules/displaymanager.yml`, echo);
            /***********************************************************************************
             * Fedora
             **********************************************************************************/
            /**
             * Fedora 35 ThirtyFive: eredita da ThirtyFive
             */
        }
        else if (this.distro().codenameLikeId === 'thirtyfive') {
            const dest = '/etc/penguins-eggs.d/distros/thirtyfive/';
            const thirtytive = `${rootPen}/conf/distros/thirtyfive/*`;
            await (0, utils_2.exec)(`cp -r ${thirtytive} ${dest}`, echo);
            /***********************************************************************************
             * Arch Linux
             **********************************************************************************/
            /**
             * Endeavour rolling: eredita da rolling
             */
        }
        else if (this.distro().codenameLikeId === 'rolling') {
            const dest = '/etc/penguins-eggs.d/distros/rolling/';
            const rolling = `${rootPen}/conf/distros/rolling/*`;
            await (0, utils_2.exec)(`cp -r ${rolling} ${dest}`, echo);
            /***********************************************************************************
             * openSuse
             **********************************************************************************/
            /**
             * openSUSE tumbleweed: eredita da tumbleweed
             */
        }
        else if (this.distro().codenameLikeId === 'tumbleweed') {
            const dest = '/etc/penguins-eggs.d/distros/tumbleweed/';
            const tumbleweed = `${rootPen}/conf/distros/tumbleweed/*`;
            await (0, utils_2.exec)(`cp -r ${tumbleweed} ${dest}`, echo);
        }
    }
    /**
     * restuisce VERO se il pacchetto è installato
     * @param debPackage
     */
    static packageIsInstalled(packageName) {
        let installed = false;
        if (this.distro().familyId === 'debian') {
            installed = debian_1.default.packageIsInstalled(packageName);
        }
        else if (this.distro().familyId === 'fedora') {
            installed = fedora_1.default.packageIsInstalled(packageName);
        }
        else if (this.distro().familyId === 'archlinux') {
            installed = archlinux_1.default.packageIsInstalled(packageName);
        }
        else if (this.distro().familyId === 'suse') {
            installed = suse_1.default.packageIsInstalled(packageName);
        }
        return installed;
    }
    /**
     * restuisce VERO se il pacchetto è installato
     * @param debPackage
     */
    static async packageAptAvailable(packageName) {
        let available = false;
        if (this.distro().familyId === 'debian') {
            available = debian_1.default.packageIsInstalled(packageName);
        }
        return available;
    }
    /**
     *
     * @param debPackage
     * @returns
     */
    static async packageAptLast(debPackage) {
        let version = '';
        if (this.distro().familyId === 'debian') {
            version = await debian_1.default.packageAptLast(debPackage);
        }
        return version;
    }
    static async packageNpmLast(packageNpm = 'penguins-eggs') {
        return shelljs_1.default.exec('npm show ' + packageNpm + ' version', { silent: true }).stdout.trim();
    }
    /**
     *
     * @param cmd
     */
    static commandIsInstalled(cmd) {
        let installed = false;
        // if (shx.exec(`command -V ${cmd} &>/dev/null`).code == 0) {
        // remove output
        if (shelljs_1.default.exec(`command -V ${cmd} >/dev/null 2>&1`).code == 0) {
            installed = true;
        }
        return installed;
    }
    /**
     * Install the package packageName
     * @param packageName {string} Pacchetto Debian da installare
     * @returns {boolean} True if success
     */
    static async packageInstall(packageName) {
        let retVal = false;
        if (this.distro().familyId === 'debian') {
            retVal = await debian_1.default.packageInstall(packageName);
        }
        else if (this.distro().familyId === 'archlinux') {
            retVal = await archlinux_1.default.packageInstall(packageName);
        }
        else if (this.distro().familyId === 'fedora') {
            retVal = await fedora_1.default.packageInstall(packageName);
        }
        return retVal;
    }
    /**
     *
     * @param packages array packages
     *
     * Probabilmente da rimuovere, viene usata solo da prerequisitesRemove
     *
     */
    static filterInstalled(packages) {
        const installed = [];
        for (const i in packages) {
            if (Pacman.packageIsInstalled(packages[i])) {
                installed.push(packages[i]);
            }
        }
        return installed;
    }
}
Pacman.debs4calamares = ['calamares', 'qml-module-qtquick2', 'qml-module-qtquick-controls'];
exports.default = Pacman;
