"use strict";
/* eslint-disable no-console */
/**
 * penguins-eggs
 * class: bleach.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 utils_1 = tslib_1.__importDefault(require("./utils"));
// libraries
const utils_2 = require("../lib/utils");
const distro_1 = tslib_1.__importDefault(require("./distro"));
/**
 * Bleach:
 */
class Bleach {
    /**
     * clean
     * @param verbose
     */
    async clean(verbose = false) {
        if (verbose) {
            utils_1.default.warning('cleaning the system');
        }
        const distro = new distro_1.default();
        if (distro.familyId === 'debian') {
            await this.cleanApt(verbose);
        }
        else if (distro.familyId === 'archlinux') {
            await (0, utils_2.exec)('pacman -Scc', utils_1.default.setEcho(true));
        }
        await this.cleanHistory(verbose);
        await this.cleanJournal(verbose);
        await this.cleanSystemCache(verbose);
    }
    /**
     * cleanApt
     * @param verbose
     */
    async cleanApt(verbose = false) {
        let echo = { echo: false, ignore: true, capture: false };
        if (verbose) {
            echo = { echo: true, ignore: true, capture: false };
        }
        await (0, utils_2.exec)('apt-get clean', echo);
        await (0, utils_2.exec)('apt-get autoclean', echo);
        const dest = '/var/lib/apt/lists/';
        rmdir(dest, verbose);
    }
    /**
     * cleanHistory
     * @param verbose
     */
    async cleanHistory(verbose = false) {
        if (verbose) {
            utils_1.default.warning('cleaning bash history');
        }
        const dest = '/root/.bash_history';
        if (node_fs_1.default.existsSync(dest)) {
            await rm(dest, verbose);
        }
    }
    /**
     * cleanJournal
     * @param verbose
     */
    async cleanJournal(verbose = false) {
        let echo = { echo: false, ignore: true, capture: false };
        if (verbose) {
            echo = { echo: true, ignore: true, capture: false };
            utils_1.default.warning('cleaning journald');
        }
        if (utils_1.default.isSystemd()) {
            try {
                await (0, utils_2.exec)('journalctl --rotate', echo);
                await (0, utils_2.exec)('journalctl --vacuum-time=1s', echo);
            }
            catch (error) {
                utils_1.default.error(error);
            }
        }
        else {
            // Truncate logs, remove archived logs.
            await (0, utils_2.exec)('find /var/log -name "*gz" -print0 | xargs -0r rm -f', echo);
            await (0, utils_2.exec)('find /var/log/ -type f -exec truncate -s 0 {} \\;', echo);
        }
    }
    /**
     * cleanSystemCache
     * @param verbose
     */
    async cleanSystemCache(verbose = false) {
        let echo = { echo: false, ignore: true, capture: false };
        if (verbose) {
            echo = { echo: true, ignore: true, capture: false };
            utils_1.default.warning('cleaning system cache');
        }
        // Clear PageCache only.
        await (0, utils_2.exec)('sync; echo 1 > /proc/sys/vm/drop_caches', echo);
        // Clear dentries and inodes.
        await (0, utils_2.exec)('sync; echo 2 > /proc/sys/vm/drop_caches', echo);
        // Clear PageCache, dentries and inodes.
        await (0, utils_2.exec)('sync; echo 3 > /proc/sys/vm/drop_caches', echo);
    }
}
exports.default = Bleach;
/**
 *
 * @param dest
 */
async function rm(dest = '', verbose = false) {
    let echo = { echo: false, ignore: true, capture: false };
    if (verbose) {
        echo = { echo: true, ignore: true, capture: false };
    }
    if (node_fs_1.default.existsSync(dest)) {
        await (0, utils_2.exec)(`rm ${dest} -rf`, echo);
    }
    else {
        console.log(`non esiste ${dest}`);
    }
}
async function rmdir(dest = '', verbose = false) {
    let echo = { echo: false, ignore: true, capture: false };
    if (verbose) {
        echo = { echo: true, ignore: true, capture: false };
    }
    const result = node_fs_1.default.readdirSync(dest);
    if (result.length > 0) {
        await (0, utils_2.exec)(`rm ${dest} -rf`, echo);
    }
}
/**
 * Elenco pulitori bleachbit
 * Solo quelli pertinenti root
 */
/*

# apt
# bash.history

// deepscan.backup
// deepscan.ds_store
// deepscan.thumbs_db
// deepscan.tmp

gnome.run
gnome.search_history

journald.clean

// kde.cache
// kde.recent_documents
// kde.tmp

# system.cache
system.clipboard // xclip
system.custom
system.desktop_entry
system.free_disk_space
system.localizations
system.memory
system.recent_documents
system.rotated_logs
system.tmp
system.trash

wine.tmp
winetricks.temporary_files

x11.debug_logs

yum.clean_all
yum.vacuum
*/
