"use strict";
/**
 * penguins-eggs
 * classes/families: suse.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 shelljs_1 = tslib_1.__importDefault(require("shelljs"));
const utils_1 = tslib_1.__importDefault(require("../utils"));
const pacman_1 = tslib_1.__importDefault(require("../pacman"));
const dependencies_1 = require("../../lib/dependencies");
const utils_2 = require("../../lib/utils");
/**
 * Utils: general porpourse utils
 * @remarks all the utilities
 */
class Suse {
    /**
     * check if it's installed xorg
     * @returns true if xorg is installed
     */
    static isInstalledXorg() {
        return this.packageIsInstalled('xorg-x11-server');
    }
    /**
     * check if it's installed wayland
     * @returns true if wayland
     */
    static isInstalledWayland() {
        return this.packageIsInstalled('xwayland*');
    }
    /**
     * Crea array pacchetti da installare/rimuovere
     */
    static packages(remove = false, verbose = false) {
        // Preso qualcosa da livecd-tools
        const packages = ['xorriso', 'syslinux', 'squashfs'];
        const toInstall = [];
        const toRemove = [];
        for (const elem of packages) {
            if (!this.packageIsInstalled(elem)) {
                toInstall.push(elem);
            }
            else {
                toRemove.push(elem);
            }
        }
        if (remove) {
            return toRemove;
        }
        return toInstall;
    }
    /**
     *
     */
    static async prerequisitesInstall(verbose = true) {
        const echo = utils_1.default.setEcho(verbose);
        const retVal = false;
        await (0, utils_2.exec)(`dnf install ${(0, dependencies_1.array2spaced)(this.packages(false, verbose))}`, echo);
        if (!pacman_1.default.isInstalledGui()) {
            /**
             * live-config-getty-generator
             *
             * Viene rimosso in naked, altrimenti non funziona il login
             * generando un errore getty.
             * Sarebbe utile individuarne le ragioni, forse rompe anche sul desktop
             * non permettendo di cambiare terminale e loggarsi
             *
             * A che serve?
             */
            const fileToRemove = '/lib/systemd/system-generators/live-config-getty-generator';
            if (node_fs_1.default.existsSync(fileToRemove)) {
                await (0, utils_2.exec)(`rm ${fileToRemove}`);
            }
        }
        return retVal;
    }
    /**
     *
     */
    static async calamaresInstall(verbose = true) {
        const echo = utils_1.default.setEcho(verbose);
        try {
            await (0, utils_2.exec)(`zypper install ${(0, dependencies_1.array2spaced)(this.packs4calamares)}`, echo);
        }
        catch {
            utils_1.default.error(`Fedora.calamaresInstall() pacman -Sy ${(0, dependencies_1.array2spaced)(this.packs4calamares)}`); // + e.error)
        }
    }
    /**
     * calamaresPolicies
     */
    static async calamaresPolicies() {
        const policyFile = '/usr/share/polkit-1/actions/com.github.calamares.calamares.policy';
        /**
         * Su fedora occorre vedere come fare!
         */
        // await exec(`sed -i 's/auth_admin/yes/' ${policyFile}`)
    }
    /**
     *
     */
    static async calamaresRemove(verbose = true) {
        const echo = utils_1.default.setEcho(verbose);
        const retVal = false;
        if (node_fs_1.default.existsSync('/etc/calamares')) {
            await (0, utils_2.exec)('rm /etc/calamares -rf', echo);
        }
        await (0, utils_2.exec)('zypper remove calamares', echo);
        return retVal;
    }
    /**
     * restuisce VERO se il pacchetto è installato
     * @param packageName
     */
    static packageIsInstalled(packageName) {
        let installed = false;
        // rpm -qa | grep -i nano
        const cmd = `/usr/bin/zypper search --installed-only ${packageName}`;
        const stdout = shelljs_1.default.exec(cmd, { silent: true }).stdout.trim();
        if (stdout.includes(packageName)) {
            installed = true;
        }
        return installed;
    }
    /**
     * Install the package packageName
     * @param packageName {string} Pacchetto da installare
     * @returns {boolean} True if success
     */
    static async packageInstall(packageName) {
        let retVal = false;
        if (shelljs_1.default.exec(`/usr/bin/zypper install ${packageName}`, { silent: true }) === '0') {
            retVal = true;
        }
        return retVal;
    }
    /**
     * restuisce VERO se il pacchetto è installato
     * @param packageName
     */
    static async packageAvailable(packageName) {
        let available = false;
        const cmd = `/usr/bin/zypper --not-installed-only ${packageName} | grep Package:`;
        const stdout = shelljs_1.default.exec(cmd, { silent: true }).stdout.trim();
        if (stdout.includes(packageName)) {
            available = true;
        }
        return available;
    }
}
Suse.packs4calamares = ['calamares'];
exports.default = Suse;
