"use strict";
/* eslint-disable no-console */
/**
 * penguins-eggs
 * classes/families: archilinux.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");
/**
 * Archlinux
 * @remarks all the utilities
 */
class Archlinux {
    /**
     * Archlinux: isInstalledXorg
     * @returns true if xorg is installed
     */
    static isInstalledXorg() {
        return this.packageIsInstalled('xorg-server-common');
    }
    /**
     * Archlinux: isInstalledWayland
     * @returns true if wayland is installed
     */
    static isInstalledWayland() {
        return this.packageIsInstalled('xwayland');
    }
    /**
     * Archlinux: packages
     * Create array packages to install/remove
     */
    static packages(remove = false, verbose = false) {
        const packages = ['']; // 'arch-install-scripts',  'e2fsprogs', 'erofs-utils', 'findutils', 'gzip', 'libarchive', 'libisoburn', 'mtools', 'openssl', 'rsync', 'sed', 'syslinux', 'squashfs-tools']
        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;
    }
    /**
     * Archlinux: prerequisitesInstall
     */
    static async prerequisitesInstall(verbose = true) {
        const echo = utils_1.default.setEcho(verbose);
        const retVal = false;
        await (0, utils_2.exec)(`pacman -Sy ${(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;
    }
    /**
     * Archlinux: calamaresInstall
     */
    static async calamaresInstall(verbose = false) {
        verbose = true; // serve per pacman
        const echo = utils_1.default.setEcho(verbose);
        // const cal_eggs = 'calamares-eggs-3.3.0.r10616.11e1659ca-1-x86_64.pkg.tar.zst'
        // const cal_eggs = 'calamares-eggs-3.3.0.r10707.4b3278058-1-x86_64.pkg.tar.zst' // 2023-09-04 
        // const cal_eggs = 'arco-calamares-git-3.3.0.r10680.9becd7d1b-1-x86_64.pkg.tar.zst' // 2023-09-20 
        //const cal_eggs = 'arco-calamares-3.3.0.alpha5-02-x86_64.pkg.tar.zst' // 2023-10-25
        const cal_eggs = 'arco-calamares-3.3.0.alpha10-03-x86_64.pkg.tar.zst'; // 2023-12-23
        let cmd = `wget -O /tmp/${cal_eggs} https://sourceforge.net/projects/penguins-eggs/files/PKGBUILD/${cal_eggs}/download`;
        try {
            await (0, utils_2.exec)(cmd, echo);
            cmd = `pacman -U /tmp/${cal_eggs}`;
            try {
                await (0, utils_2.exec)(cmd, echo);
            }
            catch {
                utils_1.default.error(`Cannot install /tmp/${cal_eggs}`); // + e.error)
            }
        }
        catch {
            utils_1.default.error(`Cannot download ${cal_eggs}`); // + e.error)
        }
    }
    /**
     * Archlinux: calamaresPolicies
     */
    static async calamaresPolicies() {
        const policyFile = '/usr/share/polkit-1/actions/com.github.calamares.calamares.policy';
        await (0, utils_2.exec)(`sed -i 's/auth_admin/yes/' ${policyFile}`);
    }
    /**
     * Archlinux: calamaresRemove
     */
    static async calamaresRemove(verbose = true) {
        verbose = true; // serve per pacman
        let removed = false;
        const echo = utils_1.default.setEcho(verbose);
        let calPKGs = [
            'arco-calamares-git',
            'calamares-eggs',
            'calamares-git',
            'calamares',
        ];
        for (const calPKG of calPKGs) {
            if (await this.packagePacmanAvailable(calPKG)) {
                await (0, utils_2.exec)(`pacman -R ${calPKG}`, echo);
                removed = true;
            }
        }
        if (removed) {
            if (node_fs_1.default.existsSync('/etc/calamares')) {
                await (0, utils_2.exec)('rm /etc/calamares -rf', echo);
            }
        }
        return removed;
    }
    /**
     * Archlinux: packageIsInstalled
     * restuisce VERO se il pacchetto è installato
     * @param packageName
     */
    static packageIsInstalled(packageName) {
        let installed = false;
        const cmd = `/usr/bin/pacman -Qi ${packageName}`;
        const stdout = shelljs_1.default.exec(cmd, { silent: true }).stdout.trim();
        if (stdout.includes(packageName)) {
            installed = true;
        }
        return installed;
    }
    /**
     * Archlinux: packageInstall
     * Install the package packageName
     * @param packageName {string} Pacchetto Debian da installare
     * @returns {boolean} True if success
     */
    static async packageInstall(packageName) {
        let retVal = false;
        if (shelljs_1.default.exec(`/usr/bin/pacman -Si ${packageName}`, { silent: true }) === '0') {
            retVal = true;
        }
        return retVal;
    }
    /**
     * Archlinux: packagePacmanAvailable
     * restuisce VERO se il pacchetto è installato
     * @param packageName
     */
    static async packagePacmanAvailable(packageName) {
        let available = false;
        const cmd = `/usr/bin/pacman -Q ${packageName} | awk '{ print $1 }'`;
        const stdout = shelljs_1.default.exec(cmd, { silent: true }).stdout.trim();
        if (stdout == packageName) {
            available = true;
        }
        return available;
    }
    /**
     * Archlinux: packagePacmanLast
     * @param packageName
     * @returns
     */
    static async packagePacmanLast(packageName) {
        let version = '';
        const cmd = `/usr/bin/pacman -Q ${packageName} | grep Version:`;
        const stdout = shelljs_1.default.exec(cmd, { silent: true }).stdout.trim();
        version = stdout.slice(9);
        // console.log('===================================')
        // console.log('[' + version + ']')
        // console.log('===================================')
        return version;
    }
}
Archlinux.packs4calamares = ['calamares-eggs']; // , 'calamares', 'calamares-git']
exports.default = Archlinux;
