"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: analyze.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
const core_1 = require("@oclif/core");
const fs_1 = tslib_1.__importDefault(require("fs"));
const utils_1 = tslib_1.__importDefault(require("../classes/utils"));
const promises_1 = require("fs/promises");
const fs_2 = require("fs");
const users_1 = tslib_1.__importDefault(require("../classes/users"));
const systeminformation_1 = tslib_1.__importDefault(require("systeminformation"));
class Analyze extends core_1.Command {
    constructor() {
        super(...arguments);
        this.config_file = '/etc/penguins-eggs.d/eggs.yaml';
        this.snapshot_dir = '';
        this.work_dir = {};
    }
    async run() {
        utils_1.default.titles(this.id + ' ' + this.argv);
        const { flags } = await this.parse(Analyze);
        let verbose = false;
        if (flags.verbose) {
            verbose = true;
        }
        const echo = utils_1.default.setEcho(verbose);
        let totalSize = 0;
        if (utils_1.default.isRoot(this.id)) {
            const audio = await systeminformation_1.default.audio();
            const bios = await systeminformation_1.default.bios();
            const blockDevices = await systeminformation_1.default.blockDevices();
            const chassis = await systeminformation_1.default.chassis();
            const cpu = await systeminformation_1.default.cpu();
            const diskLayout = await systeminformation_1.default.diskLayout();
            const mem = await systeminformation_1.default.mem();
            const usb = await systeminformation_1.default.usb();
            console.log(`chassis: ${chassis.manufacturer} model: ${chassis.manufacturer}`);
            console.log(`bios vendor: ${bios.vendor} version: ${bios.version} revision: ${bios.revision}`);
            console.log(`processor: ${cpu.brand} core: ${cpu.cores} `);
            if (diskLayout[0].device !== undefined) {
                console.log(`disk0: ${diskLayout[0].device}`);
            }
            if (diskLayout[1] !== undefined) {
                console.log(`disk1: ${diskLayout[1].device}`);
            }
            if (diskLayout[2] !== undefined) {
                console.log(`disk1: ${diskLayout[2].device}`);
            }
            console.log(`name: ${blockDevices[0].name} fs: ${blockDevices[0].fsType}`);
            console.log(`name: ${blockDevices[1].name} fs: ${blockDevices[1].fsType}`);
            console.log(`name: ${blockDevices[2].name} fs: ${blockDevices[2].fsType}`);
            console.log(`name: ${blockDevices[3].name} fs: ${blockDevices[3].fsType}`);
            console.log(`usb: ${usb[0].name}`);
            /**
             * Windows: "/Documents and Settings", "/Programs", "/AppData"
             * MacOS: "/Users"
             * Linux: "/home"
             */
            utils_1.default.warning('eggs will analyze your system, to get users and servers data');
            const users = await this.fill();
            for (let i = 0; i < users.length; i++)
                if (users[i].saveIt) {
                    console.log(`user: ${users[i].login} \thome: ${users[i].home.padEnd(16)} \tsize: ${utils_1.default.formatBytes(users[i].size)} \tBytes: ${users[i].size} `);
                    // console.log(`user: ${users[i].login} \thome: ${users[i].home} \tsize: ${users[i].size}`)
                    totalSize += users[i].size;
                }
            console.log(`Total\t\t\t\t\tSize: ${utils_1.default.formatBytes(totalSize)} \tBytes: ${totalSize}`);
        }
        else {
            utils_1.default.useRoot(this.id);
        }
    }
    /**
     * fill
     */
    async fill() {
        try {
            const usersArray = [];
            await (0, promises_1.access)('/etc/passwd', fs_2.constants.R_OK | fs_2.constants.W_OK);
            const passwd = fs_1.default.readFileSync('/etc/passwd', 'utf-8').split('\n');
            for (const element of passwd) {
                const line = element.split(':');
                const users = new users_1.default(line[0], line[1], line[2], line[3], line[4], line[5], line[6]);
                await users.getValues();
                if (users.password !== undefined) {
                    usersArray.push(users);
                }
            }
            return usersArray;
        }
        catch {
            console.error("can't read /etc/passwd");
            process.exit(1);
        }
    }
}
Analyze.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    verbose: core_1.Flags.boolean({ char: 'v', description: 'verbose' }),
};
Analyze.description = 'analyze for syncto';
Analyze.examples = ['sudo eggs analyze'];
exports.default = Analyze;
