"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs: incubator.ts
 *
 * author: Piero Proietti
 * mail: piero.proietti@gmail.com
 */
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 jessie_1 = require("./distros/jessie");
const buster_1 = require("./distros/buster");
const focal_1 = require("./distros/focal");
const bionic_1 = require("./distros/bionic");
const rolling_1 = require("./distros/rolling");
const pacman_1 = tslib_1.__importDefault(require("../pacman"));
const installer_1 = require("./installer");
// partition
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
/**
 *
 */
class Incubator {
    /**
     *
     * @param remix
     * @param distro
     * @param verbose
     */
    constructor(remix, distro, user_opt = 'live', theme = 'eggs', isClone = false, verbose = false) {
        this.verbose = false;
        this.installer = {};
        this.installer = (0, installer_1.installer)();
        this.remix = remix;
        this.distro = distro;
        this.user_opt = user_opt;
        this.theme = theme;
        this.verbose = verbose;
        this.remix.branding = theme;
        this.isClone = isClone;
        // branding è solo il basename
        this.remix.branding = node_path_1.default.basename(theme);
    }
    /**
     * config
     */
    async config(release = false) {
        const verbose = true;
        const echo = utils_1.default.setEcho(verbose);
        this.createInstallerDirs();
        this.createBranding();
        // DEBIAN
        switch (this.distro.codenameLikeId) {
            case 'jessie': {
                const jessie = new jessie_1.Jessie(this.installer, this.remix, this.distro, this.user_opt, release, this.verbose);
                await jessie.create();
                break;
            }
            case 'stretch': {
                const stretch = new jessie_1.Jessie(this.installer, this.remix, this.distro, this.user_opt, release, this.verbose);
                await stretch.create();
                break;
            }
            case 'buster': {
                const buster = new buster_1.Buster(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await buster.create();
                break;
            }
            case 'bullseye': {
                const bullseye = new buster_1.Buster(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await bullseye.create();
                break;
            }
            case 'bookworm': {
                const bookworm = new buster_1.Buster(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await bookworm.create();
                break;
            }
            case 'trixie': {
                const trixie = new buster_1.Buster(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await trixie.create();
                // DEVUAN
                break;
            }
            /**
               * DEVUAN
               */
            case 'beowulf': {
                const beowulf = new buster_1.Buster(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await beowulf.create();
                break;
            }
            case 'chimaera': {
                const chimaera = new buster_1.Buster(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await chimaera.create();
                break;
            }
            case 'daedalus': {
                const daedalus = new buster_1.Buster(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await daedalus.create();
                break;
            }
            /**
               * UBUNTU
               */
            case 'bionic': {
                const bionic = new bionic_1.Bionic(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await bionic.create();
                break;
            }
            case 'focal': {
                const focal = new focal_1.Focal(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await focal.create();
                break;
            }
            case 'jammy': {
                const jammy = new focal_1.Focal(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await jammy.create();
                break;
            }
            case 'devel': {
                const devel = new focal_1.Focal(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await devel.create();
                break;
            }
            /**
               * Arch
               */
            case 'rolling': {
                const rolling = new rolling_1.Rolling(this.installer, this.remix, this.distro, this.user_opt, release, this.theme, this.isClone, this.verbose);
                await rolling.create();
                break;
            }
        }
        if (pacman_1.default.calamaresExists()) {
            partitionCustomize();
        }
    }
    /**
     *
     */
    createInstallerDirs() {
        if (this.installer.name !== 'calamares') {
            // Remove krill configuration and multiarc if present
            try {
                shelljs_1.default.exec('rm ' + this.installer.configRoot + ' -rf');
            }
            catch (error) {
                console.log('error: ' + error + ' removing ' + this.installer.configRoot + ' -rf');
            }
            try {
                shelljs_1.default.exec('rm ' + this.installer.multiarch + ' -rf');
            }
            catch (error) {
                console.log('error: ' + error + ' removing ' + this.installer.multiarch + ' -rf');
            }
        }
        // rootConfiguration krill/calamares
        if (!node_fs_1.default.existsSync(this.installer.configRoot)) {
            try {
                node_fs_1.default.mkdirSync(this.installer.configRoot);
            }
            catch (error) {
                console.log('error: ' + error + ' creating ' + this.installer.configRoot);
            }
        }
        if (!node_fs_1.default.existsSync(this.installer.configRoot + 'branding')) {
            try {
                node_fs_1.default.mkdirSync(this.installer.configRoot + 'branding');
            }
            catch (error) {
                console.log('error: ' + error + ' creating ' + this.installer.configRoot + 'branding');
            }
        }
        if (!node_fs_1.default.existsSync(this.installer.configRoot + 'branding/eggs')) {
            try {
                node_fs_1.default.mkdirSync(this.installer.configRoot + 'branding/eggs');
            }
            catch (error) {
                console.log('error: ' + error + ' creating ' + this.installer.configRoot + 'branding/eggs');
            }
        }
        if (!node_fs_1.default.existsSync(this.installer.configRoot + 'modules')) {
            try {
                node_fs_1.default.mkdirSync(this.installer.configRoot + 'modules');
            }
            catch (error) {
                console.log('error: ' + error + ' creating ' + this.installer.configRoot + 'modules');
            }
        }
        if (!node_fs_1.default.existsSync(this.installer.multiarch)) {
            try {
                node_fs_1.default.mkdirSync(this.installer.multiarch);
            }
            catch (error) {
                console.log('error: ' + error + ' creating ' + this.installer.multiarch);
            }
        }
        if (!node_fs_1.default.existsSync(this.installer.multiarchModules)) {
            try {
                node_fs_1.default.mkdirSync(this.installer.multiarchModules);
            }
            catch (error) {
                console.log('error: ' + error + ' creating ' + this.installer.multiarchModules);
            }
        }
        /**
         * themes krill/calamares
         */
        let calamaresBranding = node_path_1.default.resolve(__dirname, `../../../addons/eggs/theme/calamares/branding`);
        if (this.theme.includes('/')) {
            calamaresBranding = `${this.theme}/theme/calamares/branding`;
        }
        // console.log(`calamaresBranding: ${calamaresBranding}`)
        if (node_fs_1.default.existsSync(calamaresBranding)) {
            if (!node_fs_1.default.existsSync(this.installer.configRoot + `branding/${this.remix.branding}`)) {
                try {
                    node_fs_1.default.mkdirSync(this.installer.configRoot + `branding/${this.remix.branding}`);
                }
                catch (error) {
                    console.log('error: ' + error + ' creating ' + this.installer.configRoot + `branding/${this.remix.branding}`);
                }
            }
            // patch quirinux
            shelljs_1.default.cp('-r', calamaresBranding + '/*', this.installer.configRoot + `branding/${this.remix.branding}/`);
        }
        else {
            console.log(calamaresBranding);
            console.log(`${calamaresBranding} branding not found!`);
            process.exit();
        }
        let calamaresIcon = node_path_1.default.resolve(__dirname, `../../../addons/${this.remix.branding}/theme/artwork/install-system.png`);
        if (this.theme.includes('/')) {
            calamaresIcon = `${this.theme}/theme/artwork/install-system.png`;
        }
        // se non esiste non copio la icona
        if (pacman_1.default.calamaresExists()) {
            if (node_fs_1.default.existsSync(calamaresIcon)) {
                shelljs_1.default.cp(calamaresIcon, '/usr/share/icons/');
            }
            else {
                console.log(`${calamaresIcon} icon not found!`);
                process.exit();
            }
        }
        let calamaresLauncher = node_path_1.default.resolve(__dirname, `../../../addons/${this.remix.branding}/theme/applications/install-system.desktop`);
        if (this.theme.includes('/')) {
            calamaresLauncher = `${this.theme}/theme/applications/install-system.desktop`;
        }
        if (node_fs_1.default.existsSync(calamaresLauncher)) {
            shelljs_1.default.cp(calamaresLauncher, '/usr/share/applications/');
        }
        else {
            console.log(`${calamaresLauncher} launcher not found!`);
            process.exit();
        }
        // script di avvio
        shelljs_1.default.cp(node_path_1.default.resolve(__dirname, '../../../assets/calamares/install-system.sh'), '/usr/sbin/install-system.sh');
        shelljs_1.default.chmod('+x', '/usr/sbin/install-system.sh');
    }
    /**
     *
     */
    createBranding() {
        const branding = require('./branding').branding;
        const dir = this.installer.configRoot + 'branding/' + this.remix.branding + '/';
        if (!node_fs_1.default.existsSync(dir)) {
            shelljs_1.default.exec(`mkdir ${dir} -p`);
        }
        const file = dir + 'branding.desc';
        const content = branding(this.remix, this.distro, this.verbose);
        write(file, content, this.verbose);
    }
}
exports.default = Incubator;
/**
 *
 * @param file
 * @param content
 * @param verbose
 */
function write(file, content, verbose = false) {
    if (verbose) {
        console.log(`calamares: create ${file}`);
    }
    node_fs_1.default.writeFileSync(file, content, 'utf8');
}
/**
 *
 */
function partitionCustomize() {
    const filePartition = '/etc/calamares/modules/partition.conf';
    let partition = js_yaml_1.default.load(node_fs_1.default.readFileSync(filePartition, 'utf-8'));
    partition.defaultFileSystemType = 'ext4';
    partition.availableFileSystemTypes = ['ext4'];
    if (pacman_1.default.packageIsInstalled('btrfs-progs')) {
        partition.availableFileSystemTypes.push('btrfs');
    }
    if (pacman_1.default.packageIsInstalled('xfsprogs')) {
        partition.availableFileSystemTypes.push('xfs');
    }
    if (pacman_1.default.packageIsInstalled('f2fs-tools')) {
        partition.availableFileSystemTypes.push('f2fs');
    }
    node_fs_1.default.writeFileSync(filePartition, js_yaml_1.default.dump(partition), 'utf-8');
}
