"use strict";
/**
 * penguins-eggs
 * class: initrd.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 * Schema from "refractasnapshot-10.2.10 (20191218)"
 * Copyright: fsmithred@gmail.com 2011-2019
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
// packages
const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
const shelljs_1 = tslib_1.__importDefault(require("shelljs"));
/**
 * initrd
 * Controlla e rimuove, se necessario cryptroot, resume
 * cryptsetup
 */
class Initrd {
    constructor(initrdSrc = '/initrd.img', initrdDest = '/home/eggs/mnt/ovarium/iso/live/initrd.img', fsLive = '/home/eggs/ovarium/filesystem') {
        this.workDir = '/tmp/initrd-extracted';
        this.compression = 'gzip';
        this.initrdSrc = '/initrd.img';
        this.initrdDest = '/home/eggs/ovarium/iso/live';
        this.fsLive = '/home/eggs/ovarium/filesystem';
        this.initrdSrc = initrdSrc;
        this.initrdDest = initrdDest;
        this.fsLive = fsLive;
        console.log('sorgente: ' + this.initrdSrc);
        console.log('destinazione: ' + this.initrdDest);
        console.log('fsLive: ' + this.fsLive);
    }
    /**
     * clean
     */
    clean() {
        this.extract();
        this.edit();
        this.rebuild();
    }
    /**
     * Check initrd for cryptroot, resume, cryptsetup.
     */
    check() {
        const initrd = {};
        const cmdCheck = `lsinitramfs ${this.initrdSrc} | egrep 'conf/conf.d/cryptroot|cryptroot/crypttab|conf/conf.d/resume|conf/conf.d/zz-resume-auto'`;
        const check = shelljs_1.default.exec(cmdCheck, { silent: true });
        if (check.includes('conf/conf.d/cryptroot')) {
            initrd.cryptoroot = true;
        }
        if (check.includes('cryptroot/crypttab')) {
            initrd.crypttab = true;
        }
        if (check.includes('conf/conf.d/resume')) {
            initrd.resume = true;
        }
        if (check.includes('conf/conf.d/zz-resume-auto')) {
            initrd.zz_resume_auto = true;
        }
        return initrd;
    }
    /**
     * extract
     */
    async extract(initrd = '/initrd.img', verbose = false) {
        let echo = { echo: false, ignore: false };
        if (verbose) {
            echo = { echo: true, ignore: false };
        }
        const savedState = process.cwd();
        // cancello e ricreo la cartella di lavore
        if (node_fs_1.default.existsSync(this.workDir)) {
            shelljs_1.default.exec(`rm ${this.workDir} -rf`);
        }
        shelljs_1.default.mkdir(this.workDir);
        process.chdir(this.workDir);
        // Verifico il tipo di compressione
        const cmd = `file -L ${this.initrdSrc} | egrep -o 'gzip compressed|XZ compressed|cpio archive'`;
        const compressionShell = shelljs_1.default.exec(cmd).trimStart().trimEnd();
        switch (compressionShell) {
            case 'gzip compressed': {
                break;
            }
            case 'xz compressed': {
                this.compression = 'xz';
                break;
            }
            case 'cpio compressed': {
                this.compression = 'cpio';
                break;
            }
            // No default
        }
        // Estratto initrd nella cartella di lavoro
        console.log('compression: [' + this.compression + ']');
        if ((this.compression = 'gzip')) {
            shelljs_1.default.exec(`zcat ${initrd} | cpio -i`);
        }
        else if (this.compression === 'xz') {
            shelljs_1.default.exec(`xzcat ${initrd} | cpio -d -i -m`);
        }
        else if (this.compression === 'cpio') {
            shelljs_1.default.exec(`(cpio -i ; zcat | cpio -i) < ${initrd}`);
        }
        process.chdir(savedState);
    }
    /**
     * edit
     */
    edit(verbose = true) {
        // Analizzo per cryptroot
        if (node_fs_1.default.existsSync(`${this.workDir}/conf/conf.d/cryptroot`)) {
            console.log('Removing cryptroot');
            shelljs_1.default.exec(`rm -f ${this.workDir}/conf/conf.d/cryptroot`);
        }
        else if (node_fs_1.default.existsSync(`${this.workDir}/cryptroot/crypttab`)) {
            console.log('Removing crypttab');
            shelljs_1.default.exec(`rm -f ${this.workDir}/cryptroot/crypttab`);
        }
        // Analizzo per resume
        if (node_fs_1.default.existsSync(`${this.workDir}/conf/conf.d/resume`)) {
            console.log('Removing resume');
            shelljs_1.default.exec(`rm -f ${this.workDir}/conf/conf.d/resume`);
            shelljs_1.default.exec(`rm -f ${this.fsLive}/etc/initramfs-tools/conf.d/resume`);
        }
        else if (`${this.workDir}/conf/conf.d/zz-resume-auto`) {
            shelljs_1.default.exec(`rm -f ${this.workDir}/rm -f conf/conf.d/zz-resume-auto`);
            shelljs_1.default.exec(`rm -f ${this.fsLive}/etc/initramfs-tools/conf.d/resume`);
        }
    }
    /**
     * rebuild
     */
    rebuild(initrd = '/initrd.img', verbose = true) {
        let echo = { echo: false, ignore: false };
        if (verbose) {
            echo = { echo: true, ignore: false };
        }
        const savedState = process.cwd();
        console.log('Compression: ' + this.compression);
        process.chdir(this.workDir);
        console.log('work_dir: ' + this.workDir);
        if (this.compression === 'gzip') {
            shelljs_1.default.exec(`find . -print0 | cpio -0 -H newc -o | gzip -c > ${this.initrdDest}`);
        }
        else if (this.compression === 'xz') {
            shelljs_1.default.exec(`find . | cpio -o -H newc | xz --check=crc32 --x86 --lzma2=dict=512KiB > ${this.initrdDest}`);
        }
        process.chdir(savedState);
    }
}
exports.default = Initrd;
