/* eslint-disable no-console */
/**
 * penguins-eggs
 * class: distro.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
'use strict';
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 js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
const utils_1 = tslib_1.__importDefault(require("./utils"));
/**
 * Classe
 */
class Distro {
    constructor(remix = {}) {
        this.familyId = 'debian';
        this.distroId = '';
        this.distroLike = '';
        this.codenameId = '';
        this.codenameLikeId = '';
        this.releaseId = '';
        this.releaseLike = '';
        this.usrLibPath = '/usr/lib';
        this.isolinuxPath = '';
        this.syslinuxPath = '';
        this.pxelinuxPath = '';
        this.memdiskPath = '';
        this.liveMediumPath = '/run/live/medium/';
        this.squashfs = 'live/filesystem.squashfs';
        this.homeUrl = '';
        this.supportUrl = '';
        this.bugReportUrl = '';
        this.isCalamaresAvailable = true;
        const file = '/etc/os-release';
        let data;
        if (node_fs_1.default.existsSync(file)) {
            data = node_fs_1.default.readFileSync(file, 'utf8');
        }
        // inizio
        let info;
        (function (info) {
            info[info["HOME_URL"] = 0] = "HOME_URL";
            info[info["SUPPORT_URL"] = 1] = "SUPPORT_URL";
            info[info["BUG_REPORT_URL"] = 2] = "BUG_REPORT_URL";
        })(info || (info = {}));
        const os = [];
        os[info.HOME_URL] = 'HOME_URL=';
        os[info.SUPPORT_URL] = 'SUPPORT_URL=';
        os[info.BUG_REPORT_URL] = 'BUG_REPORT_URL=';
        for (const temp in data) {
            if (!data[temp].search(os[info.HOME_URL])) {
                this.homeUrl = data[temp].slice(os[info.HOME_URL].length).replace(/"/g, '');
            }
            if (!data[temp].search(os[info.SUPPORT_URL])) {
                this.supportUrl = data[temp].slice(os[info.SUPPORT_URL].length).replace(/"/g, '');
            }
            if (!data[temp].search(os[info.BUG_REPORT_URL])) {
                this.bugReportUrl = data[temp].slice(os[info.BUG_REPORT_URL].length).replace(/"/g, '');
            }
        }
        /**
         * lsb_release -cs per codename (version)
         * lsb_release -is per distribuzione
         * lsb_release -rs per release
         */
        this.codenameId = shelljs_1.default.exec('lsb_release -cs', { silent: true }).stdout.toString().trim();
        this.releaseId = shelljs_1.default.exec('lsb_release -rs', { silent: true }).stdout.toString().trim();
        this.distroId = shelljs_1.default.exec('lsb_release -is', { silent: true }).stdout.toString().trim();
        /**
         * releaseLike = releaseId
         */
        this.releaseLike = this.releaseId;
        /**
         * Per casi equivoci conviene normalizzare codenameId
         *  -i, --id           show distributor ID
         *  -r, --release      show release number of this distribution
         *  -c, --codename     show code name of this distribution
         */
        if (this.distroId === 'Debian' && this.releaseId === 'unstable' && this.codenameId === 'sid') {
            this.codenameId = 'trixie';
        }
        else if (this.distroId === 'Debian' && this.releaseId === 'testing/unstable') {
            this.codenameId = 'trixie';
            this.releaseLike = 'unstable';
        }
        /**
         * Analisi: codenameId
         */
        switch (this.codenameId) {
            case 'jessie': {
                // Debian 8 jessie
                this.distroLike = 'Debian';
                this.codenameLikeId = 'jessie';
                this.liveMediumPath = '/lib/live/mount/medium/';
                this.isCalamaresAvailable = false;
                break;
            }
            case 'stretch': {
                // Debian 9 stretch
                this.distroLike = 'Debian';
                this.codenameLikeId = 'stretch';
                this.liveMediumPath = '/lib/live/mount/medium/';
                this.isCalamaresAvailable = false;
                break;
            }
            case 'buster': {
                // Debian 10 buster
                this.distroLike = 'Debian';
                this.codenameLikeId = 'buster';
                break;
            }
            case 'bullseye': {
                // Debian 11 bullseye
                this.distroLike = 'Debian';
                this.codenameLikeId = 'bullseye';
                break;
            }
            case 'bookworm': {
                // Debian 12 bookworm
                this.distroLike = 'Debian';
                this.codenameLikeId = 'bookworm';
                break;
            }
            case 'trixie': {
                // Debian 13 trixie
                this.distroLike = 'Debian';
                this.codenameLikeId = 'trixie';
                break;
            }
            case 'beowulf': {
                // Devuab 3 beowulf
                this.distroLike = 'Devuan';
                this.codenameLikeId = 'beowulf';
                break;
            }
            case 'chimaera': {
                // Devuab 4 chimaera
                this.distroLike = 'Devuan';
                this.codenameLikeId = 'chimaera';
                break;
            }
            case 'daedalus': {
                // Devuan 5 daedalus
                this.distroLike = 'Devuan';
                this.codenameLikeId = 'daedalus';
                break;
            }
            /**
               * Ubuntu LTS + actual
               */
            case 'bionic': {
                // Ubuntu 18.04 bionic LTS eol aprile 2023
                this.distroLike = 'Ubuntu';
                this.codenameLikeId = 'bionic';
                this.liveMediumPath = '/lib/live/mount/medium/';
                break;
            }
            case 'focal': {
                // Ubuntu 20.04 focal LTS
                this.distroLike = 'Ubuntu';
                this.codenameLikeId = 'focal';
                break;
            }
            case 'jammy': {
                // Ubuntu 22.04 jammy LTS
                this.distroLike = 'Ubuntu';
                this.codenameLikeId = 'jammy';
                break;
            }
            case 'devel': {
                // Ubuntu rhino
                this.distroLike = 'Ubuntu';
                this.codenameLikeId = 'devel';
                break;
            }
            /**
               * Arch Linux
               */
            case 'n/a':
            case 'rolling': {
                // Arch, BlendOS, RebornOS, EndeavourOS, ArcoLinux
                this.familyId = 'archlinux';
                this.distroLike = 'Arch';
                this.codenameId = 'rolling';
                this.codenameLikeId = 'rolling';
                this.liveMediumPath = '/run/archiso/bootmnt/';
                this.squashfs = `arch/x86_64/airootfs.sfs`;
                break;
            }
            default: {
                /**
                 * patch per Roy VERIFICARE
                 */
                let found = false;
                let file = node_path_1.default.resolve(__dirname, '../../conf/derivatives.yaml');
                if (node_fs_1.default.existsSync('/etc/penguins-eggs.d/derivatives.yaml')) {
                    file = '/etc/penguins-eggs.d/derivatives.yaml';
                }
                const content = node_fs_1.default.readFileSync(file, 'utf8');
                const distros = js_yaml_1.default.load(content);
                for (const distro of distros) {
                    if (distro.ids !== undefined) {
                        for (let n = 0; n < distro.ids.length; n++) {
                            if (this.codenameId === distro.ids[n]) {
                                found = true;
                                this.distroLike = distro.distroLike;
                                this.codenameLikeId = distro.id;
                                this.familyId = distro.family;
                            }
                        }
                    }
                }
                if (!found) {
                    console.log(`This distro ${this.distroId}/${this.codenameId} is not yet recognized!`);
                    console.log('');
                    console.log('You can edit /usr/lib/penguins-eggs/conf/derivatives.yaml to add it -');
                    console.log('after that - run: sudo eggs dad -d to re-configure eggs.');
                    console.log('If you can create your new iso, you can contribute to the project');
                    console.log('by suggesting your modification.');
                    process.exit(0);
                }
            }
        }
        /**
         * setting paths: syslinux, isolinux, usrLibPath
         */
        switch (this.familyId) {
            case 'debian': {
                this.isolinuxPath = '/usr/lib/ISOLINUX/';
                this.syslinuxPath = '/usr/lib/syslinux/modules/bios/';
                this.pxelinuxPath = '/usr/lib/PXELINUX/';
                this.memdiskPath = '/usr/lib/syslinux/';
                this.usrLibPath = '/usr/lib/' + utils_1.default.usrLibPath();
                break;
            }
            case 'archlinux': {
                this.syslinuxPath = '/usr/lib/syslinux/bios/';
                this.pxelinuxPath = this.syslinuxPath;
                this.usrLibPath = '/usr/lib/';
                this.memdiskPath = this.syslinuxPath;
                this.isolinuxPath = this.syslinuxPath;
                break;
            }
            // No default
        } // Fine analisi codenameId
        /**
         * if lsb-release exists
         */
        const lsbConfig = '/etc/lsb-release';
        if (node_fs_1.default.existsSync(lsbConfig)) {
            this.distroId = utils_1.default.searchOnFile(lsbConfig, `DISTRIB_ID`);
            this.codenameId = utils_1.default.searchOnFile(lsbConfig, `DISTRIB_CODENAME`);
        }
        /**
          * ManjaroLinux
          */
        if (this.distroId === 'ManjaroLinux') {
            this.liveMediumPath = '/run/miso/bootmnt/';
            this.squashfs = 'manjaro/x86_64/livefs.sfs';
        }
    }
}
exports.default = Distro;
