/**
 * penguins-eggs
 * lib: kill_me_softly.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 utils_1 = tslib_1.__importDefault(require("../classes/utils"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const utils_2 = require("./utils");
/**
 *
 */
async function killMeSoftly(eggsRoot = `/home/eggs`, eggsMnt = '/home/eggs/.mnt', isos = false) {
    const echo = utils_1.default.setEcho(false);
    const liveFs = `${eggsMnt}filesystem.squashfs`;
    /**
     * refuse if /home/eggs/mnt/filesystem.squashfs
     */
    if (haveBindedDirs(liveFs)) {
        utils_1.default.warning(`You have binded dirs under ${liveFs}, kill is not possible!`);
        process.exit(1);
    }
    /**
     * if eggsMnt is mountpoint
     */
    if (utils_1.default.isMountpoint(eggsMnt)) {
        // Just delete
        await (0, utils_2.exec)(`rm -rf ${eggsMnt}efi-work`);
        await (0, utils_2.exec)(`rm -rf ${eggsMnt}iso`);
        await (0, utils_2.exec)(`rm -rf ${eggsMnt}memdiskDir`);
        await (0, utils_2.exec)(`rm -rf ${eggsRoot}ovarium`);
        // Remove all ISOs if --isos
        if (isos) {
            await (0, utils_2.exec)(`rm -rf ${eggsMnt}*.iso`);
        }
        // double check !haveBindedDirs
        if (!haveBindedDirs(liveFs)) {
            await (0, utils_2.exec)(`rm -rf ${liveFs}`);
        }
        process.exit(0);
    }
    await (0, utils_2.exec)(`rm ${eggsRoot} -rf`, echo);
}
exports.default = killMeSoftly;
/**
 *
 * @param path
 * @returns
 */
function haveBindedDirs(path) {
    let retVal = false;
    const dirs = [
        'bin',
        'boot',
        'etc',
        'lib',
        'lib32',
        'lib64',
        'libx32',
        'opt',
        'root',
        'sbin',
        'srv',
        'usr',
        'var'
    ];
    for (const dir of dirs) {
        const dirToCheck = `${path}/${dir}`;
        if (fs_1.default.existsSync(dirToCheck)) {
            if (utils_1.default.isMountpoint(dirToCheck)) {
                console.log(`Warning: ${dirToCheck}, is a mountpoint!`);
                retVal = true;
            }
        }
    }
    return retVal;
}
